Home AtCoder. 001 Yokan Party(4)
Post
Cancel

AtCoder. 001 Yokan Party(4)

[Link] https://AtCoder.jp/contests/typical90/tasks/typical90_a


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import java.util.*;
import java.io.*;

public class Main {
  public static int[] garr;
  public static int k, n, l;

  public static void main(String[] args) throws IOException {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    StringTokenizer st = new StringTokenizer(br.readLine());
    n = Integer.parseInt(st.nextToken());
    l = Integer.parseInt(st.nextToken());
    k = Integer.parseInt(br.readLine());
    st = new StringTokenizer(br.readLine());
    garr = new int[n + 1];
    garr[0] = 0;

    for(int i = 1; i <= n; i++) {
      garr[i] = Integer.parseInt(st.nextToken());
    }
    int right = 1;
    int left = l;
    int mid;
    while(right < left) {
      mid = (right + left) / 2 + 1;
      if(statisfy(mid)) right = mid;
      else left = mid - 1;
    }
    System.out.println(left);
  }

  static boolean statisfy(int minL) {
    int end, preV, preIdx, cnt;
    end = preV = preIdx = cnt = 0;

    for(int j = preIdx + 1; j <= n; j++) {
      if(garr[j] - preV >= minL) {
        preV = garr[j];
        preIdx = j;
        if(++cnt == k) return l - preV >= minL ? true : false;
      }
    }
    return false;
  }
}
This post is licensed under CC BY 4.0 by the author.