CSES - BAPC 2015 - Results
Submission details
Task:Freight Train
Sender:Antti Röyskö
Submission time:2017-10-17 19:49:16 +0300
Language:C++
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.03 sdetails
#2ACCEPTED0.44 sdetails
#3ACCEPTED0.14 sdetails
#4ACCEPTED0.04 sdetails

Code

#include <iostream>
#include <vector>

void solve() {
	int n, w, l;
	std::cin >> n >> w >> l;
	std::vector<int> move;
	int prev = 0;
	for (int i = 0; i < w; ++i) {
		int ind;
		std::cin >> ind;
		if (ind - prev - 1 > 0) {
			move.push_back(-(ind - prev - 1));
		}
		move.push_back(ind);
		prev = ind;
	}
	if (prev != n) {
		move.push_back(prev - n);
	}
	// binary search answer
	int low = 1;
	int high = n;
	while(low != high) {
		int mid = (low + high) / 2;
		std::vector<int> clone;
		for (int i = move.size() - 1; i >= 0; --i) {
			clone.push_back(move[i]);
		}
		int used = 0;
		while(! clone.empty()) {
			++used;
			if (clone.back() < -mid) {
				clone.pop_back();
			} else {
				int left = mid;
				while((! clone.empty()) && (left > 0)) {
					int next = clone.back();
					if (next < 0) {
						left += next;
						if (left >= 0) {
							clone.pop_back();
						} else {
							clone.back() = left;
							break;
						}
					} else {
						--left;
						clone.pop_back();
					}
				}
			}
		}
		if (used <= l) {
			high = mid;
		} else {
			low = mid + 1;
		}
	}
	std::cout << low << '\n';
}

int main() {
	int T;
	std::cin >> T;
	for (int i = 0; i < T; ++i) {
		solve();
	}
}

Test details

Test 1

Verdict: ACCEPTED

input
6
6 2 2
1 2
8 3 3
1 4 7
...

correct output
2
3
2
3
3
...

user output
2
3
2
3
3
...

Test 2

Verdict: ACCEPTED

input
100
10000000 100 84
313750 383426 490027 515434 66...

correct output
88356
110708
101990
246895
471637
...

user output
88356
110708
101990
246895
471637
...

Test 3

Verdict: ACCEPTED

input
20
840187717 3944 6178
39387 340773 645007 692489 945...

correct output
40670
211835
1080572
139232
48241
...

user output
40670
211835
1080572
139232
48241
...

Test 4

Verdict: ACCEPTED

input
8
80 7 5
2 4 7 47 49 52 53
90 9 9
11 14 17 47 49 52 53 76 82
...

correct output
7
7
13
13
1
...

user output
7
7
13
13
1
...