結果
問題 | No.348 カゴメカゴメ |
ユーザー | threepipes_s |
提出日時 | 2016-02-27 01:41:17 |
言語 | Java21 (openjdk 21) |
結果 |
WA
|
実行時間 | - |
コード長 | 5,610 bytes |
コンパイル時間 | 2,883 ms |
コンパイル使用メモリ | 86,848 KB |
実行使用メモリ | 99,564 KB |
最終ジャッジ日時 | 2023-10-24 18:09:35 |
合計ジャッジ時間 | 22,613 ms |
ジャッジサーバーID (参考情報) |
judge11 / judge12 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 723 ms
67,160 KB |
testcase_01 | AC | 63 ms
53,856 KB |
testcase_02 | WA | - |
testcase_03 | AC | 745 ms
67,244 KB |
testcase_04 | AC | 59 ms
53,856 KB |
testcase_05 | AC | 83 ms
54,432 KB |
testcase_06 | AC | 51 ms
53,836 KB |
testcase_07 | AC | 51 ms
53,812 KB |
testcase_08 | AC | 50 ms
53,832 KB |
testcase_09 | AC | 52 ms
53,816 KB |
testcase_10 | WA | - |
testcase_11 | AC | 51 ms
53,828 KB |
testcase_12 | AC | 183 ms
61,752 KB |
testcase_13 | AC | 149 ms
57,356 KB |
testcase_14 | AC | 81 ms
54,444 KB |
testcase_15 | AC | 799 ms
99,564 KB |
testcase_16 | AC | 52 ms
53,816 KB |
testcase_17 | AC | 50 ms
53,812 KB |
testcase_18 | AC | 51 ms
53,804 KB |
testcase_19 | AC | 51 ms
53,828 KB |
testcase_20 | AC | 55 ms
53,868 KB |
testcase_21 | AC | 52 ms
53,820 KB |
testcase_22 | AC | 49 ms
51,792 KB |
testcase_23 | WA | - |
testcase_24 | AC | 844 ms
75,556 KB |
testcase_25 | AC | 942 ms
74,100 KB |
testcase_26 | AC | 1,004 ms
74,572 KB |
testcase_27 | AC | 1,111 ms
76,304 KB |
testcase_28 | AC | 848 ms
76,160 KB |
testcase_29 | AC | 862 ms
73,704 KB |
testcase_30 | AC | 979 ms
75,208 KB |
testcase_31 | AC | 912 ms
75,688 KB |
testcase_32 | AC | 975 ms
75,488 KB |
testcase_33 | AC | 256 ms
60,252 KB |
testcase_34 | AC | 247 ms
61,580 KB |
testcase_35 | AC | 262 ms
60,716 KB |
testcase_36 | AC | 238 ms
61,124 KB |
testcase_37 | AC | 254 ms
61,168 KB |
testcase_38 | AC | 255 ms
61,044 KB |
testcase_39 | AC | 241 ms
60,904 KB |
testcase_40 | AC | 243 ms
61,008 KB |
testcase_41 | WA | - |
testcase_42 | AC | 254 ms
61,032 KB |
testcase_43 | WA | - |
testcase_44 | WA | - |
testcase_45 | WA | - |
testcase_46 | AC | 157 ms
58,940 KB |
testcase_47 | AC | 141 ms
57,016 KB |
testcase_48 | WA | - |
testcase_49 | WA | - |
testcase_50 | AC | 154 ms
59,580 KB |
testcase_51 | WA | - |
testcase_52 | WA | - |
ソースコード
import java.io.BufferedWriter; import java.io.FileInputStream; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.BitSet; import java.util.HashMap; import java.util.List; import java.util.Queue; import java.util.Stack; public class Main { public static void main(String[] args) throws NumberFormatException, IOException {Solve solve = new Solve();solve.solve();} } class Solve{ void dump(int[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();} void dump(int[]a,int n){for(int i=0;i<a.length;i++)System.out.printf("%"+n+"d",a[i]);System.out.println();} void dump(long[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");System.out.println();} void dump(char[]a){for(int i=0;i<a.length;i++)System.out.print(a[i]);System.out.println();} String itob(int a,int l){return String.format("%"+l+"s",Integer.toBinaryString(a)).replace(' ','0');} int n, m; void solve() throws NumberFormatException, IOException{ final ContestScanner in = new ContestScanner(); Writer out = new Writer(); n = in.nextInt(); m = in.nextInt(); BitSet map = new BitSet(n*m); int allx = 0; for(int i=0; i<n; i++){ char[] line = in.nextToken().toCharArray(); for(int j=0; j<m; j++){ if(line[j]=='x'){ map.set(i*m+j); allx++; } } } List<Node> node = new ArrayList<>(); final int[] dx = {1, 1, 1, 0, 0, -1, -1, -1}; final int[] dy = {1, 0, -1, 1, -1, 1, -1, 0}; final int[] dx4 = {1,0,-1,0}; final int[] dy4 = {0,1,0,-1}; BitSet used = new BitSet(); Stack<Long> oq = new Stack<>(); Stack<Integer> qu = new Stack<>(); oq.add(0L); int mask = (1<<30)-1; int id = 0; node.add(new Node(0, 0)); while(!oq.isEmpty()){ long ot = oq.pop(); final int p = (int)ot&mask; ot >>= 30; if(used.get(p)) continue; used.set(p); final int y = p/m; final int x = p%m; if(!map.get(p)){ for(int i=0; i<4; i++){ final int ny = y+dy4[i]; final int nx = x+dx4[i]; final int np = ny*m+nx; if(out(ny, nx) || used.get(np)) continue; oq.add(np|ot<<30); } }else{ id++; int left = m; int ly = 0; int size = 0; qu.clear(); qu.add(p); while(!qu.isEmpty()){ final int ip = qu.pop(); size++; final int iy = ip/m; final int ix = ip%m; if(ix<left && !map.get(iy*m+ix+1)){ left = ix; ly = iy; } for(int i=0; i<8; i++){ final int ny = iy+dy[i]; final int nx = ix+dx[i]; final int np = ny*m+nx; if(out(ny, nx) || !map.get(np) || used.get(np)) continue; used.set(np); qu.add(np); } } // p <- node if(size>3) oq.add(ly*m+left+1 | (long)id<<30); Node newnode = new Node(id, size); Node parent = node.get((int)ot); node.add(newnode); newnode.append(parent); parent.append(newnode); } } dp = new int[id+1][2]; int x1 = dfs(node.get(0), -1, true); System.out.println(Math.max(x1, allx-x1)); } int[][] dp; int dfs(Node v, int par, boolean f){ if(dp[v.id][f?1:0]>0) return dp[v.id][f?1:0]; int sumT = 0; int sumF = 0; for(Node nv: v.edge){ if(nv.id == par) continue; if(f){ // not use sumF += dfs(nv, v.id, false); }else{ // use sumT += dfs(nv, v.id, true); // not use sumF += dfs(nv, v.id, false); } } int ans = f ? sumF : Math.max(sumF, sumT + v.size); return dp[v.id][f?1:0] = ans; } boolean out(int y, int x){ return y<0||y>=n||x<0||x>=m; } } class Node{ int id; int size; List<Node> edge = new ArrayList<>(); Node(int id, int s){ this.id = id; size = s; } void append(Node v){ edge.add(v); } } class MultiSet<T> extends HashMap<T, Integer>{ @Override public Integer get(Object key){return containsKey(key)?super.get(key):0;} public void add(T key,int v){put(key,get(key)+v);} public void add(T key){put(key,get(key)+1);} public void sub(T key) {final int v=get(key);if(v==1)remove(key);else put(key,v-1);} } class Timer{ long time; public void set(){time=System.currentTimeMillis();} public long stop(){return time=System.currentTimeMillis()-time;} public void print() {System.out.println("Time: "+(System.currentTimeMillis()-time)+"ms");} @Override public String toString(){return"Time: "+time+"ms";} } class Writer extends PrintWriter{ public Writer(String filename)throws IOException {super(new BufferedWriter(new FileWriter(filename)));} public Writer()throws IOException{super(System.out);} } class ContestScanner { private InputStreamReader in;private int c=-2; public ContestScanner()throws IOException {in=new InputStreamReader(System.in);} public ContestScanner(String filename)throws IOException {in=new InputStreamReader(new FileInputStream(filename));} public String nextToken()throws IOException { StringBuilder sb=new StringBuilder(); while((c=in.read())!=-1&&Character.isWhitespace(c)); while(c!=-1&&!Character.isWhitespace(c)){sb.append((char)c);c=in.read();} return sb.toString(); } public String readLine()throws IOException{ StringBuilder sb=new StringBuilder();if(c==-2)c=in.read(); while(c!=-1&&c!='\n'&&c!='\r'){sb.append((char)c);c=in.read();} return sb.toString(); } public long nextLong()throws IOException,NumberFormatException {return Long.parseLong(nextToken());} public int nextInt()throws NumberFormatException,IOException {return(int)nextLong();} public double nextDouble()throws NumberFormatException,IOException {return Double.parseDouble(nextToken());} }