Submission #914592


Source Code Expand

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashMap ;

/**
 * http://arc060.contest.atcoder.jp/tasks/arc060_c
 * 
 * @author Cummin
 */
public class Main {
        static int N ;  // 2 <= N <= 10^5
        static long L ; // 1 <= L <= 10^9
        static int Q ;  // 2 <= Q <= 10^5
        static int X[] ;// 1 <= X <= 10^9, X[0..N-1]
        
        // static int DP[][] ; // DP[from][to]の日数
        static HashMap <Integer, Integer> DP = new HashMap <>() ;
        public static void main(String[] args) {
//        System.out.printf("At Coder ARC060 問題E \n") ;
long start = System.currentTimeMillis();         
        /** /    
        Scanner sc = new Scanner(System.in);
        N = sc.nextInt() ;
        for (int i=0 ; i<N; i++) {
            // X[i]= sc.nextInt() ;
            X[i]  = Integer.parseInt(sc.next()); // この一行(10^5要素)で、113秒
        }
        */
        X = fast_read(1) ;
        N = X[0] ;
        X = fast_read(N) ;
//System.out.printf("N=%d\n", N) ;
long end = System.currentTimeMillis();
//System.out.println((end - start)  + "ms");
start = System.currentTimeMillis();          
        int Y[] ;
        Y = fast_read(2) ;
        L = Y[0] ;
        Q = Y[1] ;
//        L = sc.nextLong() ;
//        Q = sc.nextInt() ;
//System.out.printf("L=%d, Q=%d\n", L, Q) ;
        int Qs[][] = new int[Q][2] ; // Qs[][from] < Qs[][to]
        for (int i=0 ; i<Q ; i++) {
            int from, to ;
            /*   読み込みが遅いので変更 (これで、10^5*3個が213秒)
            from = sc.nextInt() ;
            to   = sc.nextInt() ;
            */
//            from = Integer.parseInt(sc.next()); //  (これで、10^5*3個が201秒)
//            to   = Integer.parseInt(sc.next()); //  この二行(10^5行 2数値)で、92.5秒
            Y = fast_read(2) ;
            from = Y[0] ;
            to   = Y[1] ;
            if (from>to) {
                int t ;
                t = from; from = to ; to = t ;
            }
            Qs[i][0] = from - 1 ;
            Qs[i][1] = to - 1 ;
        }
end = System.currentTimeMillis();
//System.out.println((end - start)  + "ms");

        //DP = new int[N][N] ;
/*
        for (int i = 0 ; i<Q ; i++) {
            System.out.println(Solve(Qs[i][0], Qs[i][1])) ;
//            System.out.println("-----") ;
        }
        */
        ////// 出力も高速に ////////////////
        PrintWriter out = new PrintWriter(System.out);
        for (int i = 0; i < Q; i++) {
            out.println(Solve2(Qs[i][0], Qs[i][1]));
        }
        out.flush();
    }
    static int Solve2(int from, int to) {
        int cnt = 1 ;
        int ptr = from+1 ;
        while (ptr<=to) {
            if ((X[ptr]-X[from])<=L) {
                ptr++ ;
            } else {
                from = ptr -1 ;
                cnt ++ ;
            }
        }
        return cnt ;
    }
    static int Solve(int from, int to) {
        if (DP.containsKey(from*100000+to)) return DP.get(from*100000+to) ;
        //if (DP[from][to] != 0) return DP[from][to] ;
        int max = from ;
        for (int i=from+1 ; i<=to; i++){
            if ((X[i]-X[from])<=L) {
                max = i ;
            } else break ;           
        }
        if (max == to) {
            //DP[from][to] = 1 ;
            DP.put(from*100000+to,1) ;
        } else {
            //DP[from][to] = Solve(max, to) +1 ;
            DP.put(from*100000+to, Solve(max, to)+1) ; 
        }
        // return DP[from][to] ;
        return DP.get(from*100000+to) ;
    }

    //////////////// 高速読み込み //////////////////////
    
    static int[] fast_read(int N) {
        int elements[] = new int[N];
        int e_cnt = 0;
        while (e_cnt < N) {
            char c;
            c = get_char();
            while ((c < '0') || ('9' < c)) { // Skip 非数値文字
                c=get_char() ;
            }
            elements[e_cnt] = 0;
            while (('0' <= c) && (c <= '9')) {  // 数値文字を変換する
                elements[e_cnt] = elements[e_cnt] * 10 + (int) c - '0';
                c = get_char();
            }
//            System.out.printf("\nelement[%d] = %d \n", e_cnt + 1, elements[e_cnt]);
            e_cnt++;
        }
        return elements;
    }
    
    ///////////////// get_char() /////////////////////////////////
    static char c_buf[] = new char[1024*1024];
    static int ptr = 0 ;
    static int c_len = 0 ;
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

    static char get_char() {
        char ret;

        if (ptr >= c_len) {
            try {
                c_len = br.read(c_buf);
            } catch (IOException ex) {
               ex.printStackTrace();
               System.exit(-1); // 終了する
            }
            ptr = 0;
        }
        ret = c_buf[ptr];
        ptr++;
        return ret;
    }
}

Submission Info

Submission Time
Task D - Digit Sum
User Cummin
Language Java7 (OpenJDK 1.7.0)
Score 0
Code Size 5160 Byte
Status TLE
Exec Time 2106 ms
Memory 52232 KiB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 0 / 500
Status
TLE × 4
RE × 1
TLE × 14
RE × 33
Set Name Test Cases
Sample subtask1_87654_30.txt, subtask1_87654_138.txt, subtask1_87654_45678.txt, subtask1_31415926535_1.txt, subtask1_1_31415926535.txt
All subtask1_100000000000_1.txt, subtask1_100000000000_100000000000.txt, subtask1_100000000000_2.txt, subtask1_100000000000_3.txt, subtask1_100000000000_50000000000.txt, subtask1_100000000000_50000000001.txt, subtask1_100000000000_99999999999.txt, subtask1_16983563041_1.txt, subtask1_1_1.txt, subtask1_1_2.txt, subtask1_1_31415926535.txt, subtask1_239484768_194586924.txt, subtask1_2_1.txt, subtask1_2_2.txt, subtask1_31415926535_1.txt, subtask1_49234683534_2461734011.txt, subtask1_4_1.txt, subtask1_58640129658_232122496.txt, subtask1_68719476735_35.txt, subtask1_68719476735_36.txt, subtask1_68719476735_37.txt, subtask1_68719476736_1.txt, subtask1_68719476736_2.txt, subtask1_72850192441_16865701.txt, subtask1_79285169301_27.txt, subtask1_82914867733_1676425945.txt, subtask1_8594813796_75700.txt, subtask1_87654_12345.txt, subtask1_87654_138.txt, subtask1_87654_30.txt, subtask1_87654_4294967308.txt, subtask1_87654_45678.txt, subtask1_97822032312_49157112.txt, subtask1_98750604051_977728851.txt, subtask1_99999515529_1.txt, subtask1_99999515529_316226.txt, subtask1_99999515529_316227.txt, subtask1_99999515529_316228.txt, subtask1_99999515529_49999757765.txt, subtask1_99999515529_49999757766.txt, subtask1_99999515530_2.txt, subtask1_99999999977_1.txt, subtask1_99999999977_2.txt, subtask1_99999999977_49999999989.txt, subtask1_99999999977_49999999990.txt, subtask1_99999999999_1.txt, subtask1_99999999999_100000000000.txt
Case Name Status Exec Time Memory
subtask1_100000000000_1.txt RE 127 ms 15524 KiB
subtask1_100000000000_100000000000.txt RE 127 ms 15500 KiB
subtask1_100000000000_2.txt RE 127 ms 15496 KiB
subtask1_100000000000_3.txt RE 131 ms 15504 KiB
subtask1_100000000000_50000000000.txt RE 127 ms 15500 KiB
subtask1_100000000000_50000000001.txt RE 132 ms 15524 KiB
subtask1_100000000000_99999999999.txt RE 127 ms 15524 KiB
subtask1_16983563041_1.txt RE 103 ms 10068 KiB
subtask1_1_1.txt TLE 2105 ms 33304 KiB
subtask1_1_2.txt TLE 2105 ms 33048 KiB
subtask1_1_31415926535.txt TLE 2105 ms 33248 KiB
subtask1_239484768_194586924.txt RE 132 ms 15524 KiB
subtask1_2_1.txt TLE 2105 ms 32976 KiB
subtask1_2_2.txt TLE 2105 ms 33160 KiB
subtask1_31415926535_1.txt RE 128 ms 15524 KiB
subtask1_49234683534_2461734011.txt RE 127 ms 15524 KiB
subtask1_4_1.txt TLE 2105 ms 32780 KiB
subtask1_58640129658_232122496.txt RE 103 ms 9940 KiB
subtask1_68719476735_35.txt RE 103 ms 9940 KiB
subtask1_68719476735_36.txt RE 103 ms 10068 KiB
subtask1_68719476735_37.txt RE 103 ms 10068 KiB
subtask1_68719476736_1.txt TLE 2105 ms 33176 KiB
subtask1_68719476736_2.txt TLE 2105 ms 33184 KiB
subtask1_72850192441_16865701.txt RE 103 ms 9940 KiB
subtask1_79285169301_27.txt RE 128 ms 15524 KiB
subtask1_82914867733_1676425945.txt RE 128 ms 15524 KiB
subtask1_8594813796_75700.txt TLE 2106 ms 52232 KiB
subtask1_87654_12345.txt TLE 2105 ms 34144 KiB
subtask1_87654_138.txt TLE 2105 ms 34040 KiB
subtask1_87654_30.txt TLE 2105 ms 33664 KiB
subtask1_87654_4294967308.txt TLE 2105 ms 34168 KiB
subtask1_87654_45678.txt TLE 2105 ms 33728 KiB
subtask1_97822032312_49157112.txt RE 104 ms 10068 KiB
subtask1_98750604051_977728851.txt RE 104 ms 10068 KiB
subtask1_99999515529_1.txt RE 128 ms 15524 KiB
subtask1_99999515529_316226.txt RE 130 ms 15508 KiB
subtask1_99999515529_316227.txt RE 128 ms 15524 KiB
subtask1_99999515529_316228.txt RE 128 ms 15652 KiB
subtask1_99999515529_49999757765.txt RE 129 ms 15524 KiB
subtask1_99999515529_49999757766.txt RE 128 ms 15524 KiB
subtask1_99999515530_2.txt RE 127 ms 15632 KiB
subtask1_99999999977_1.txt RE 127 ms 15504 KiB
subtask1_99999999977_2.txt RE 128 ms 15652 KiB
subtask1_99999999977_49999999989.txt RE 127 ms 15504 KiB
subtask1_99999999977_49999999990.txt RE 130 ms 15508 KiB
subtask1_99999999999_1.txt RE 127 ms 15504 KiB
subtask1_99999999999_100000000000.txt RE 130 ms 15504 KiB