結果

問題 No.608 God's Maze
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-08 02:45:11
言語 Java21
(openjdk 21)
結果
AC  
実行時間 174 ms / 2,000 ms
コード長 3,443 bytes
コンパイル時間 2,478 ms
コンパイル使用メモリ 77,924 KB
実行使用メモリ 57,244 KB
最終ジャッジ日時 2023-08-19 22:29:44
合計ジャッジ時間 10,900 ms
ジャッジサーバーID
(参考情報)
judge10 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 74 ms
50,620 KB
testcase_01 AC 75 ms
52,384 KB
testcase_02 AC 74 ms
52,412 KB
testcase_03 AC 75 ms
51,076 KB
testcase_04 AC 75 ms
52,556 KB
testcase_05 AC 73 ms
50,636 KB
testcase_06 AC 74 ms
52,392 KB
testcase_07 AC 73 ms
50,808 KB
testcase_08 AC 77 ms
52,248 KB
testcase_09 AC 85 ms
52,396 KB
testcase_10 AC 74 ms
52,720 KB
testcase_11 AC 75 ms
53,128 KB
testcase_12 AC 75 ms
51,532 KB
testcase_13 AC 86 ms
53,500 KB
testcase_14 AC 77 ms
53,156 KB
testcase_15 AC 87 ms
53,196 KB
testcase_16 AC 72 ms
50,476 KB
testcase_17 AC 79 ms
53,196 KB
testcase_18 AC 75 ms
50,956 KB
testcase_19 AC 75 ms
52,532 KB
testcase_20 AC 85 ms
52,380 KB
testcase_21 AC 78 ms
52,416 KB
testcase_22 AC 74 ms
50,808 KB
testcase_23 AC 75 ms
50,960 KB
testcase_24 AC 74 ms
52,012 KB
testcase_25 AC 74 ms
50,800 KB
testcase_26 AC 76 ms
52,700 KB
testcase_27 AC 76 ms
53,196 KB
testcase_28 AC 77 ms
53,184 KB
testcase_29 AC 174 ms
56,860 KB
testcase_30 AC 140 ms
54,708 KB
testcase_31 AC 129 ms
55,788 KB
testcase_32 AC 137 ms
56,272 KB
testcase_33 AC 156 ms
57,244 KB
testcase_34 AC 149 ms
56,204 KB
testcase_35 AC 90 ms
52,436 KB
testcase_36 AC 106 ms
53,680 KB
testcase_37 AC 112 ms
54,272 KB
testcase_38 AC 142 ms
56,316 KB
testcase_39 AC 89 ms
53,292 KB
testcase_40 AC 112 ms
54,248 KB
testcase_41 AC 114 ms
54,144 KB
testcase_42 AC 155 ms
56,416 KB
testcase_43 AC 109 ms
53,924 KB
testcase_44 AC 162 ms
56,104 KB
testcase_45 AC 149 ms
56,300 KB
testcase_46 AC 91 ms
53,340 KB
testcase_47 AC 76 ms
52,176 KB
testcase_48 AC 151 ms
56,312 KB
testcase_49 AC 107 ms
53,688 KB
testcase_50 AC 74 ms
52,136 KB
testcase_51 AC 78 ms
52,572 KB
testcase_52 AC 85 ms
52,672 KB
testcase_53 AC 72 ms
50,508 KB
testcase_54 AC 74 ms
52,584 KB
testcase_55 AC 83 ms
51,976 KB
testcase_56 AC 73 ms
52,356 KB
testcase_57 AC 75 ms
52,288 KB
testcase_58 AC 75 ms
51,328 KB
testcase_59 AC 75 ms
50,812 KB
testcase_60 AC 75 ms
52,404 KB
testcase_61 AC 77 ms
52,540 KB
testcase_62 AC 77 ms
52,176 KB
testcase_63 AC 76 ms
52,512 KB
testcase_64 AC 74 ms
52,576 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import java.io.*;
import java.util.*;


class Main {
    static String s;
    static int[]zero,one;
    static int g(int[]x,int a){
        int ans=0;
        int[]oldx=x.clone();
        int n=x.length;
        if(x[a]==1)
            if(a==n-1)return 0x3fffffff;
            else{
                x[a]=0;
                x[a+1]=1-x[a+1];
                ans+=2;
            }
        int p=a;
        int last=-1;
        for(int i=a;i<n;++i)
            if(x[i]==1)last=i;
        while(true){
            int p2=p+1;
            while(p2<=last&&x[p2]!=0)p2++;
            if(p2>last){
                ans+=Math.max(last-p,0);
                break;
            }
            x[p2]=1;
            x[p2+1]=1-x[p2+1];
            if(last==p2+1)last=p2;
            ans+=p2-p+2;
            p=p2;
        }
        //System.err.println("g("+Arrays.toString(oldx)+","+a+")="+ans);
        return ans;
    }
    public static void main(String[] args) {
        MyScanner sc = new MyScanner();
        out = new PrintWriter(new BufferedOutputStream(System.out));
        int n=sc.nextInt()-1;
        s=sc.next();
        int l=s.length();
        int p=0,q=0;
        zero=new int[s.length()];
        one=new int[s.length()];
        for(int i=0;i<s.length();++i)
            if(s.charAt(i)=='.')
                zero[p++]=i;
            else
                one[q++]=i;
        zero=Arrays.copyOf(zero,p);
        one=Arrays.copyOf(one,q);
        int[]t=new int[l];
        for(int i=0;i<l;++i)
            t[i]=s.charAt(i)=='#'?1:0;
        int[]u=t.clone();
        int x=one[0],y=one[q-1];
        System.err.println("x="+x+" y="+y);
        int m=Integer.MAX_VALUE;
        int st=-1;
        if(x<n){
            for(int i=x;i<n;++i)u[i]=1-u[i];
            st=x;
        }else
            st=n;
        m=Math.min(m,Math.max(0,n-x)+g(u,st));
        u=t.clone();
        if(n<y){
            for(int i=n+1;i<=y;++i)u[i]=1-u[i];
            st=y;
        }else
            st=n;
        for(int i=0;i<l/2;++i){
            int temp=u[i];
            u[i]=u[l-1-i];
            u[l-1-i]=temp;
        }
        m=Math.min(m,Math.max(0,y-n)+g(u,l-1-st));
        out.println(m);
        out.close();
    }
    // http://codeforces.com/blog/entry/7018
    //-----------PrintWriter for faster output---------------------------------
    public static PrintWriter out;
    //-----------MyScanner class for faster input----------
    public static class MyScanner {
        BufferedReader br;
        StringTokenizer st;
        public MyScanner() {
            br = new BufferedReader(new InputStreamReader(System.in));
        }
        String next() {
            while (st == null || !st.hasMoreElements()) {
                try {
                    st = new StringTokenizer(br.readLine());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return st.nextToken();
        }
        int nextInt() {
            return Integer.parseInt(next());
        }
        long nextLong() {
            return Long.parseLong(next());
        }
        double nextDouble() {
            return Double.parseDouble(next());
        }
        String nextLine(){
            String str = "";
            try {
                str = br.readLine();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return str;
        }
    }
}
0