提出 #857007


ソースコード 拡げる

Copy
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.NoSuchElementException;

public class Main {
    public static void main(String[] args) {
        FastScanner sc = new FastScanner();
        int n = sc.nextInt();
        int[] x = new int[n + 1];
        for (int i = 1; i <= n; i++) {
            x[i] = sc.nextInt();
        }
        int l = sc.nextInt();
        int q = sc.nextInt();
        int[] a = new int[q + 1];
        int[] b = new int[q + 1];
        for (int i = 1; i <= q; i++) {
            a[i] = sc.nextInt();
            b[i] = sc.nextInt();
        }
        Map<Integer, Integer> reachable = new HashMap<>();
        for (int i = 1; i <= n; i++) {
            int pos = i;
            while (pos <= n && x[pos] <= x[i] + l) {
                pos++;
            }
            reachable.put(i, pos - 1);
        }
        Map<Integer, Integer> reachablerev = new HashMap<>();
        for (int i = n; i >= 1; i--) {
            int pos = i;
            while (pos >= 1 && x[pos] >= x[i] - l) {
                pos--;
            }
            reachablerev.put(i,pos + 1);
        }
        for (int i = 1; i <= q; i++) {
            int pos = a[i];
            int dir = a[i] <= b[i] ? 1 : -1;
            int days = 0;
            while (pos * dir < b[i] * dir) {
                pos = dir == 1 ? reachable.get(pos) : reachablerev.get(pos);
                days++;
            }
            System.out.println(days);
        }

    }
}

class FastScanner {
    private final InputStream in = System.in;
    private final byte[] buffer = new byte[1024];
    private int ptr = 0;
    private int buflen = 0;

    private boolean hasNextByte() {
        if (ptr < buflen) {
            return true;
        } else {
            ptr = 0;
            try {
                buflen = in.read(buffer);
            } catch (IOException e) {
                e.printStackTrace();
            }
            if (buflen <= 0) {
                return false;
            }
        }
        return true;
    }

    private int readByte() {
        if (hasNextByte()) return buffer[ptr++];
        else return -1;
    }

    private static boolean isPrintableChar(int c) {
        return 33 <= c && c <= 126;
    }

    public boolean hasNext() {
        while (hasNextByte() && !isPrintableChar(buffer[ptr])) ptr++;
        return hasNextByte();
    }

    public String next() {
        if (!hasNext()) throw new NoSuchElementException();
        StringBuilder sb = new StringBuilder();
        int b = readByte();
        while (isPrintableChar(b)) {
            sb.appendCodePoint(b);
            b = readByte();
        }
        return sb.toString();
    }

    public long nextLong() {
        if (!hasNext()) throw new NoSuchElementException();
        long n = 0;
        boolean minus = false;
        int b = readByte();
        if (b == '-') {
            minus = true;
            b = readByte();
        }
        if (b < '0' || '9' < b) {
            throw new NumberFormatException();
        }
        while (true) {
            if ('0' <= b && b <= '9') {
                n *= 10;
                n += b - '0';
            } else if (b == -1 || !isPrintableChar(b)) {
                return minus ? -n : n;
            } else {
                throw new NumberFormatException();
            }
            b = readByte();
        }
    }

    public int nextInt() {
        long nl = nextLong();
        if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE) throw new NumberFormatException();
        return (int) nl;
    }

    public double nextDouble() {
        return Double.parseDouble(next());
    }
}

提出情報

提出日時
問題 E - 高橋君とホテル
ユーザ nes_in_it
言語 Java8 (OpenJDK 1.8.0)
得点 200
コード長 3837 Byte
結果 TLE
実行時間 3165 ms
メモリ 73344 KB

ジャッジ結果

セット名 Sample Subtask1 All
得点 / 配点 0 / 0 200 / 200 0 / 500
結果
AC × 1
AC × 14
AC × 14
TLE × 13
セット名 テストケース
Sample example_01.txt
Subtask1 example_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt
All example_01.txt, subtask1_01.txt, subtask1_02.txt, subtask1_03.txt, subtask1_04.txt, subtask1_05.txt, subtask1_06.txt, subtask1_07.txt, subtask1_08.txt, subtask1_09.txt, subtask1_10.txt, subtask1_11.txt, subtask1_12.txt, subtask1_13.txt, subtask2_01.txt, subtask2_02.txt, subtask2_03.txt, subtask2_04.txt, subtask2_05.txt, subtask2_06.txt, subtask2_07.txt, subtask2_08.txt, subtask2_09.txt, subtask2_10.txt, subtask2_11.txt, subtask2_12.txt, subtask2_13.txt
ケース名 結果 実行時間 メモリ
example_01.txt AC 158 ms 7892 KB
subtask1_01.txt AC 215 ms 7888 KB
subtask1_02.txt AC 158 ms 7892 KB
subtask1_03.txt AC 214 ms 10068 KB
subtask1_04.txt AC 328 ms 15824 KB
subtask1_05.txt AC 224 ms 10400 KB
subtask1_06.txt AC 214 ms 10192 KB
subtask1_07.txt AC 199 ms 9300 KB
subtask1_08.txt AC 231 ms 9972 KB
subtask1_09.txt AC 231 ms 10580 KB
subtask1_10.txt AC 255 ms 13596 KB
subtask1_11.txt AC 242 ms 13772 KB
subtask1_12.txt AC 241 ms 13472 KB
subtask1_13.txt AC 239 ms 13076 KB
subtask2_01.txt TLE 3163 ms 51676 KB
subtask2_02.txt TLE 3161 ms 72152 KB
subtask2_03.txt TLE 3162 ms 38676 KB
subtask2_04.txt TLE 3159 ms 13080 KB
subtask2_05.txt TLE 3161 ms 30944 KB
subtask2_06.txt TLE 3157 ms 27932 KB
subtask2_07.txt TLE 3163 ms 46244 KB
subtask2_08.txt TLE 3165 ms 73344 KB
subtask2_09.txt TLE 3163 ms 50576 KB
subtask2_10.txt TLE 3165 ms 73016 KB
subtask2_11.txt TLE 3162 ms 36448 KB
subtask2_12.txt TLE 3162 ms 41924 KB
subtask2_13.txt TLE 3160 ms 18468 KB