|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | + |
| 7 | +class Main { |
| 8 | + |
| 9 | + // IO field |
| 10 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 11 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 12 | + static StringTokenizer st; |
| 13 | + |
| 14 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 15 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 16 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 17 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 18 | + |
| 19 | + // Additional field |
| 20 | + |
| 21 | + static int N, X; |
| 22 | + static int[] A, B, C; |
| 23 | + |
| 24 | + public static void main(String[] args) throws Exception { |
| 25 | + |
| 26 | + ready(); |
| 27 | + solve(); |
| 28 | + |
| 29 | + bwEnd(); |
| 30 | + |
| 31 | + } |
| 32 | + |
| 33 | + static void ready() throws Exception{ |
| 34 | + |
| 35 | + nextLine(); |
| 36 | + N = nextInt(); |
| 37 | + X = nextInt()-1; |
| 38 | + |
| 39 | + A = new int[N+1]; |
| 40 | + |
| 41 | + nextLine(); |
| 42 | + for(int i=1;i<=N;i++) A[i] = nextInt(); |
| 43 | + |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + static void solve() throws Exception{ |
| 48 | + |
| 49 | + fillB(); |
| 50 | + fillC(); |
| 51 | + |
| 52 | + |
| 53 | + long ans1 = 0; |
| 54 | + for(int i=1;i<=N;i++) ans1 += A[i] - C[i]; |
| 55 | + |
| 56 | + long ans2 = 0; |
| 57 | + for(int i=1;i<=N;) { |
| 58 | + int a = C[i], cnt = 0; |
| 59 | + while(i<=N && C[i] == a) { |
| 60 | + i++; |
| 61 | + cnt++; |
| 62 | + } |
| 63 | + ans2 += (cnt - 1) / (X+1) + 1; |
| 64 | + } |
| 65 | + |
| 66 | + bw.write(ans1 + "\n" + ans2); |
| 67 | + |
| 68 | + } |
| 69 | + |
| 70 | + static void fillB() { |
| 71 | + |
| 72 | + Deque<int[]> D = new ArrayDeque<>(); |
| 73 | + |
| 74 | + B = new int[N+1]; |
| 75 | + for(int i=N;i>=1;i--) { |
| 76 | + while(!D.isEmpty() && D.peekLast()[0] >= A[i]) D.pollLast(); |
| 77 | + D.offerLast(new int[] {A[i], i}); |
| 78 | + while(!D.isEmpty() && D.peekFirst()[1] - i > X) D.pollFirst(); |
| 79 | + if(B[i] <= N-X) B[i] = D.peekFirst()[0]; |
| 80 | + } |
| 81 | + for(int i=N-X+1;i<=N;i++) B[i] = B[i-1]; |
| 82 | + |
| 83 | + } |
| 84 | + |
| 85 | + static void fillC() { |
| 86 | + |
| 87 | + Deque<int[]> D = new ArrayDeque<>(); |
| 88 | + |
| 89 | + C = new int[N+1]; |
| 90 | + for(int i=1;i<=N;i++) { |
| 91 | + while(!D.isEmpty() && D.peekLast()[0] <= B[i]) D.pollLast(); |
| 92 | + D.offerLast(new int[] {B[i], i}); |
| 93 | + while(!D.isEmpty() && i - D.peekFirst()[1] > X) D.pollFirst(); |
| 94 | + C[i] = D.peekFirst()[0]; |
| 95 | + } |
| 96 | + |
| 97 | + } |
| 98 | + |
| 99 | +} |
| 100 | + |
| 101 | +``` |
0 commit comments