# | Submission time | Handle | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
391074 | 2021-04-17T19:25:01 Z | yu_lim | A Huge Tower (CEOI10_tower) | Java 11 | 0 ms | 0 KB |
import java.util.*; import java.io.*; public class HugeTower { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer str = new StringTokenizer(br.readLine()); int N = Integer.parseInt(str.nextToken()); int D = Integer.parseInt(str.nextToken()); int M = 1_000_000_007; int[] blocks = new int[N]; str = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) blocks[i] = Integer.parseInt(str.nextToken()); br.close(); Arrays.sort(blocks); long[] towers = new long[N]; towers[0] = 1; int prev = 0; for (int i = 0; i < N; i++) { while (prev < i) { if (blocks[prev] + D < blocks[i]) prev++; else break; } // # of blocks <= i that can't stack on i if (i > 0) towers[i] = (((i - prev + 1) % M) * towers[i - 1]) % M; } System.out.println(towers[N - 1] % M); } }