Home AtCoder. 001 Yokan Party(4)
Post
Cancel

AtCoder. 001 Yokan Party(4)

We are given a stick of length L and N possible cut positions. Choose exactly K of those positions, dividing the stick into K + 1 pieces, and maximize the length of the shortest piece.

For a candidate minimum length d, scan the possible cut positions from left to right. Greedily make a cut as soon as it is at least d from the previous cut (starting at position 0), and stop after K cuts. This places each cut as early as possible, leaving the most room for later pieces; consequently, if any selection of K cuts works, these earliest greedy cuts work too. The candidate is feasible exactly when K cuts can be made and the remaining suffix from the last selected cut to L is at least d. When K = 0, no cuts are needed, so feasibility is simply L >= d.

Feasibility is monotone: if a minimum length d is feasible, every smaller positive length is feasible too. Binary search the largest feasible d in [0, L]. The greedy scan costs O(N) per check, so the total time is O(N log L) and the extra space is O(N) for the input positions.

Problem link

Java

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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.io.BufferedInputStream;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        FastScanner input = new FastScanner();
        int n = input.nextInt();
        int length = input.nextInt();
        int k = input.nextInt();

        int[] positions = new int[n];
        for (int i = 0; i < n; i++) {
            positions[i] = input.nextInt();
        }

        int low = 0;
        int high = length + 1;
        while (high - low > 1) {
            int middle = low + (high - low) / 2;
            if (canAchieve(middle, positions, k, length)) {
                low = middle;
            } else {
                high = middle;
            }
        }
        System.out.println(low);
    }

    private static boolean canAchieve(int minimumLength, int[] positions, int k, int length) {
        if (k == 0) {
            return length >= minimumLength;
        }

        int previousCut = 0;
        int cuts = 0;
        for (int position : positions) {
            if (position - previousCut >= minimumLength) {
                previousCut = position;
                if (++cuts == k) {
                    return length - previousCut >= minimumLength;
                }
            }
        }
        return false;
    }

    private static final class FastScanner {
        private final BufferedInputStream input = new BufferedInputStream(System.in);
        private final byte[] buffer = new byte[1 << 16];
        private int length;
        private int position;

        private int read() throws IOException {
            if (position == length) {
                length = input.read(buffer);
                position = 0;
                if (length == -1) {
                    return -1;
                }
            }
            return buffer[position++];
        }

        int nextInt() throws IOException {
            int c;
            do {
                c = read();
            } while (c <= ' ' && c != -1);

            int value = 0;
            while (c > ' ') {
                value = value * 10 + c - '0';
                c = read();
            }
            return value;
        }
    }
}
This post is licensed under CC BY 4.0 by the author.

Buy me a coffee