3453 | 2023-02-28 09:42:57 | Ancsa | Szemetessor | cpp11 | Accepted 100/100 | 284ms | 31748 KiB |
// greedy_linear solution O(N+K)
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>
#include <numeric>
/*
5 11
66 73 68 79 78
2 50
0 1
0 12
2 20
0 23
1 44
1 30
4 12
4 3
4 63
0 10
kimenet: 158
*/
using namespace std;
constexpr int INF = 1e9;
int N, K;
vector<int> C, T, Q;
int main() {
cin >> N >> K;
C.resize(N);
for (auto &x : C)
cin >> x;
T.resize(K);
Q.resize(K);
for (int i = 0; i < K; i++)
cin >> T[i] >> Q[i];
/* for each bin, calculate the first moment you can empty */
vector <vector<int>>empty_times(N);
vector <int> bins(N, 0);
long long ans = 0;
for (int i = 0; i < K; ++i) {
auto &idx = T[i];
auto &qty = Q[i];
if (bins[idx] == 0 || bins[idx] + qty > C[idx]) {
/* add index to empty times */
if (bins[idx] != 0) ans += C[idx] - bins[idx];
empty_times[idx].push_back(i);
bins[idx] = qty;
} else {
/* substitute the previous index */
empty_times[idx].back() = i;
bins[idx] += qty;
}
}
/* add last index */
for (int i = 0; i < N; ++i) {
if (empty_times[i].size() > 0) {
ans += C[i] - bins[i];
}
}
cout << ans << endl;
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1808 KiB | ||||
2 | Accepted | 3ms | 2060 KiB | ||||
subtask2 | 17/17 | ||||||
3 | Accepted | 3ms | 2312 KiB | ||||
4 | Accepted | 3ms | 2512 KiB | ||||
5 | Accepted | 3ms | 2596 KiB | ||||
6 | Accepted | 3ms | 2812 KiB | ||||
7 | Accepted | 3ms | 2980 KiB | ||||
8 | Accepted | 3ms | 3192 KiB | ||||
9 | Accepted | 3ms | 3404 KiB | ||||
10 | Accepted | 3ms | 3512 KiB | ||||
11 | Accepted | 3ms | 3516 KiB | ||||
subtask3 | 25/25 | ||||||
12 | Accepted | 123ms | 8084 KiB | ||||
13 | Accepted | 123ms | 8140 KiB | ||||
14 | Accepted | 136ms | 8320 KiB | ||||
15 | Accepted | 143ms | 8632 KiB | ||||
16 | Accepted | 157ms | 9780 KiB | ||||
17 | Accepted | 216ms | 19788 KiB | ||||
18 | Accepted | 284ms | 31620 KiB | ||||
19 | Accepted | 284ms | 31620 KiB | ||||
20 | Accepted | 284ms | 31748 KiB | ||||
subtask4 | 20/20 | ||||||
21 | Accepted | 128ms | 7844 KiB | ||||
22 | Accepted | 153ms | 12828 KiB | ||||
23 | Accepted | 128ms | 8364 KiB | ||||
24 | Accepted | 152ms | 10420 KiB | ||||
25 | Accepted | 136ms | 8336 KiB | ||||
26 | Accepted | 208ms | 18416 KiB | ||||
27 | Accepted | 270ms | 26740 KiB | ||||
28 | Accepted | 270ms | 27360 KiB | ||||
29 | Accepted | 270ms | 26988 KiB | ||||
30 | Accepted | 237ms | 26944 KiB | ||||
subtask5 | 38/38 | ||||||
31 | Accepted | 3ms | 4348 KiB | ||||
32 | Accepted | 3ms | 4428 KiB | ||||
33 | Accepted | 3ms | 4552 KiB | ||||
34 | Accepted | 3ms | 4572 KiB | ||||
35 | Accepted | 14ms | 5380 KiB | ||||
36 | Accepted | 143ms | 9440 KiB | ||||
37 | Accepted | 266ms | 23624 KiB | ||||
38 | Accepted | 263ms | 23680 KiB | ||||
39 | Accepted | 273ms | 24772 KiB | ||||
40 | Accepted | 275ms | 26996 KiB | ||||
41 | Accepted | 279ms | 28256 KiB | ||||
42 | Accepted | 277ms | 27160 KiB |