結果

問題 No.608 God's Maze
ユーザー 夕叢霧香(ゆうむらきりか)夕叢霧香(ゆうむらきりか)
提出日時 2017-12-08 02:35:02
言語 Java21
(openjdk 21)
結果
WA  
実行時間 -
コード長 5,515 bytes
コンパイル時間 2,981 ms
コンパイル使用メモリ 81,312 KB
実行使用メモリ 57,028 KB
最終ジャッジ日時 2023-08-19 12:43:37
合計ジャッジ時間 10,669 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 AC 70 ms
50,892 KB
testcase_05 AC 73 ms
52,440 KB
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 AC 73 ms
52,728 KB
testcase_10 AC 75 ms
52,576 KB
testcase_11 WA -
testcase_12 AC 73 ms
52,236 KB
testcase_13 AC 80 ms
52,528 KB
testcase_14 WA -
testcase_15 AC 81 ms
52,748 KB
testcase_16 AC 71 ms
51,008 KB
testcase_17 AC 72 ms
51,116 KB
testcase_18 AC 84 ms
52,664 KB
testcase_19 AC 74 ms
52,140 KB
testcase_20 AC 71 ms
50,956 KB
testcase_21 AC 74 ms
52,380 KB
testcase_22 WA -
testcase_23 WA -
testcase_24 AC 87 ms
53,120 KB
testcase_25 AC 72 ms
51,208 KB
testcase_26 AC 70 ms
50,928 KB
testcase_27 AC 74 ms
53,392 KB
testcase_28 AC 74 ms
52,840 KB
testcase_29 WA -
testcase_30 AC 145 ms
56,612 KB
testcase_31 AC 126 ms
56,324 KB
testcase_32 AC 135 ms
54,028 KB
testcase_33 AC 158 ms
56,352 KB
testcase_34 AC 146 ms
56,596 KB
testcase_35 AC 87 ms
53,932 KB
testcase_36 AC 106 ms
53,856 KB
testcase_37 AC 111 ms
53,688 KB
testcase_38 AC 139 ms
56,460 KB
testcase_39 AC 89 ms
53,520 KB
testcase_40 AC 114 ms
53,464 KB
testcase_41 AC 111 ms
53,668 KB
testcase_42 AC 152 ms
56,452 KB
testcase_43 AC 108 ms
53,692 KB
testcase_44 AC 157 ms
56,192 KB
testcase_45 AC 149 ms
56,312 KB
testcase_46 AC 93 ms
53,568 KB
testcase_47 AC 78 ms
52,472 KB
testcase_48 AC 152 ms
55,984 KB
testcase_49 AC 108 ms
54,176 KB
testcase_50 WA -
testcase_51 WA -
testcase_52 WA -
testcase_53 AC 75 ms
50,628 KB
testcase_54 WA -
testcase_55 RE -
testcase_56 AC 88 ms
52,628 KB
testcase_57 WA -
testcase_58 WA -
testcase_59 WA -
testcase_60 WA -
testcase_61 WA -
testcase_62 WA -
testcase_63 AC 88 ms
50,724 KB
testcase_64 AC 79 ms
52,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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


class Main {
    static String s;
    static int[]zero,one;
    static int f(int a,int l,int r,int fs,int fe){
        System.err.printf("enter f(%d,%d,%d,%d,%d)\n",a,l,r,fs,fe);
        if(fs>=fe){
            fs=-1;fe=-1;
        }
        if(fs==-1){
            int m=Integer.MAX_VALUE;
            int i1=Arrays.binarySearch(one,r);
            if(i1<0)i1=-i1-1;
            int i2=Arrays.binarySearch(one,l);
            if(i2<0)i2=-i2-1;
            if(i1==i2){
                return 0;
            }
            int x=one[i2],y=one[i1-1];
            if(x<a)m=Math.min(m,a-x+f(x,x+1,r,x+1,a));
            if(a<y)m=Math.min(m,y-a+f(y,l,y,a+1,y));
            System.err.println("f("+a+","+l+","+r+")="+m);
            return m;
        }
        int ans;
        if(a<l){
            int nxt=-1;
            System.err.println(Arrays.toString(one) + " r = " + r + " fe = " + fe);
            int i1=Arrays.binarySearch(one,r);
            if(i1<0)i1=-i1-1;
            int i2=Arrays.binarySearch(one,fe);
            if(i2<0)i2=-i2-1;
            System.err.println("i1="+i1+" i2="+i2);
            if(i1>i2){
                nxt=one[i1-1];
                System.err.println("nxt = " + nxt);
                ans=nxt-a+f(nxt,l,nxt,fe,nxt);
            }else{
                int i3=Arrays.binarySearch(zero,fe);
                if(i3<0)i3=-i3-1;
                int i4=Arrays.binarySearch(zero,fs);
                if(i4<0)i4=-i4-1;
                if(i3>i4){
                    nxt=zero[i3-1];
                    ans=nxt-a+f(nxt,l,nxt,-1,-1);
                }else
                    ans=0;
            }
        }else{
            int nxt=-1;
            int i1=Arrays.binarySearch(one,r);
            if(i1<0)i1=-i1-1;
            int i2=Arrays.binarySearch(one,l);
            if(i2<0)i2=-i2-1;
            System.err.println("i1="+i1+" i2="+i2);
            if(i1>i2){
                nxt=one[i2];
                ans=a-nxt+f(nxt,nxt+1,r,nxt+1,fs);
            }else{
                int i3=Arrays.binarySearch(zero,r);
                if(i3<0)i3=-i3-1;
                int i4=Arrays.binarySearch(zero,fs);
                if(i4<0)i4=-i4-1;
                if(i3>i4){
                    nxt=zero[i4];
                    ans=a-nxt+f(nxt,nxt+1,r,-1,-1);
                }else
                    ans=0;
            }
        }
        System.err.printf("f(%d,%d,%d,%d,%d)=%d\n",a,l,r,fs,fe,ans);
        return ans;
    }
    static int g(int[]x,int a){
        int ans=0;
        int[]oldx=x.clone();
        int n=x.length;
        int p=a;
        int last=0;
        for(int i=0;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+1){
                ans+=last-p;
                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;
        if(x<n)
            for(int i=x;i<n;++i)u[i]=1-u[i];
        m=Math.min(m,Math.max(0,n-x)+g(u,x));
        u=t.clone();
        if(n<y)
            for(int i=n+1;i<=y;++i)u[i]=1-u[i];
        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-y));
        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