# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
336427 |
2020-12-15T10:16:23 Z |
ncduy0303 |
Feast (NOI19_feast) |
C++17 |
|
127 ms |
13540 KB |
/* NOI 2019: Feast
Idea: Greedy (combine adjacent values with the same sign and remove negative values from the two ends)
Time Complexity: O(NlogN)
*/
#include <bits/stdc++.h>
using namespace std;
const int INF = 1 << 30;
const int MAX_N = 300000 + 5;
const int MAX_L = 20; // ~ Log N
const long long MOD = 1e9 + 7;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> ii;
typedef vector<ii> vii;
typedef vector<vi> vvi;
#define LSOne(S) (S & (-S))
#define isBitSet(S, i) ((S >> i) & 1)
struct block{
int id, l, r;
long long sum;
};
int N, K;
block b[MAX_N];
long long arr[MAX_N];
bool v[MAX_N];
priority_queue<pair<long long, int> > pq;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
cin >> N >> K;
for(int i = 0; i < N; i++) cin >> arr[i];
int j, idx = 0;
for(int i = 0; i < N; i = j) {
b[idx].sum = 0;
b[idx].l = idx - 1;
b[idx].r = idx + 1;
for(j = i; j < N && (arr[i] * arr[j] >= 0); j++) // arr[i] and arr[j] must have the same sign
b[idx].sum += arr[j];
if(!(b[idx].sum < 0 && idx == 0)) idx++; // remove the first block if it is negative
}
if(idx != 0 && b[idx - 1].sum < 0) idx--; // remove the last block if it is negative
if(idx == 0) { // if after remove, there's nothing left
cout << 0;
return 0;
}
b[0].l = b[idx - 1].r = -1; // extreme end points
int remaining = idx / 2 + 1;
for (int i = 0; i < idx; i++)
pq.push({-abs(b[i].sum), i}); // negative of absolute value to have a min priority queue
memset(v, 0, sizeof(v));
while(remaining > K) {
int i = pq.top().second; pq.pop();
if(v[i]) continue;
v[i] = true;
remaining--;
//merge it with left and right
if (b[i].l == -1) { //extreme left
v[b[i].r] = true;
b[b[b[i].r].r].l = -1;
}
else if (b[i].r == -1) { //extreme right
v[b[i].l] = true;
b[b[b[i].l].l].r = -1;
}
else {
v[b[i].r] = true;
v[b[i].l] = true;
b[i].sum += b[b[i].l].sum + b[b[i].r].sum;
if (b[b[i].l].l != -1) {
b[b[b[i].l].l].r = i;
}
b[i].l = b[b[i].l].l;
if (b[b[i].r].r != -1) {
b[b[b[i].r].r].l = i;
}
b[i].r = b[b[i].r].r;
v[i] = 0;
pq.push({-abs(b[i].sum), i});
}
}
long long ans = 0;
for(int i = 0; i < idx; i++)
if (!v[i] && b[i].sum > 0)
ans += b[i].sum;
cout << ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
5740 KB |
Output is correct |
2 |
Correct |
36 ms |
5868 KB |
Output is correct |
3 |
Correct |
47 ms |
5868 KB |
Output is correct |
4 |
Correct |
36 ms |
5868 KB |
Output is correct |
5 |
Correct |
36 ms |
5868 KB |
Output is correct |
6 |
Correct |
35 ms |
5760 KB |
Output is correct |
7 |
Correct |
35 ms |
5740 KB |
Output is correct |
8 |
Correct |
39 ms |
5996 KB |
Output is correct |
9 |
Correct |
35 ms |
5740 KB |
Output is correct |
10 |
Correct |
35 ms |
5740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
4204 KB |
Output is correct |
2 |
Correct |
24 ms |
4076 KB |
Output is correct |
3 |
Correct |
26 ms |
4076 KB |
Output is correct |
4 |
Correct |
29 ms |
4076 KB |
Output is correct |
5 |
Correct |
43 ms |
5740 KB |
Output is correct |
6 |
Correct |
24 ms |
4076 KB |
Output is correct |
7 |
Correct |
24 ms |
4076 KB |
Output is correct |
8 |
Correct |
45 ms |
5868 KB |
Output is correct |
9 |
Correct |
36 ms |
5868 KB |
Output is correct |
10 |
Correct |
30 ms |
4204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
107 ms |
13444 KB |
Output is correct |
2 |
Correct |
102 ms |
13284 KB |
Output is correct |
3 |
Correct |
105 ms |
13412 KB |
Output is correct |
4 |
Correct |
104 ms |
13284 KB |
Output is correct |
5 |
Correct |
109 ms |
13412 KB |
Output is correct |
6 |
Correct |
106 ms |
13412 KB |
Output is correct |
7 |
Correct |
97 ms |
13412 KB |
Output is correct |
8 |
Correct |
96 ms |
13412 KB |
Output is correct |
9 |
Correct |
102 ms |
13540 KB |
Output is correct |
10 |
Correct |
127 ms |
13540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
748 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
620 KB |
Output is correct |
2 |
Correct |
1 ms |
620 KB |
Output is correct |
3 |
Correct |
1 ms |
620 KB |
Output is correct |
4 |
Correct |
1 ms |
620 KB |
Output is correct |
5 |
Correct |
1 ms |
620 KB |
Output is correct |
6 |
Correct |
1 ms |
620 KB |
Output is correct |
7 |
Correct |
1 ms |
620 KB |
Output is correct |
8 |
Correct |
1 ms |
620 KB |
Output is correct |
9 |
Correct |
1 ms |
620 KB |
Output is correct |
10 |
Correct |
1 ms |
620 KB |
Output is correct |
11 |
Correct |
1 ms |
620 KB |
Output is correct |
12 |
Correct |
1 ms |
620 KB |
Output is correct |
13 |
Correct |
1 ms |
620 KB |
Output is correct |
14 |
Correct |
1 ms |
620 KB |
Output is correct |
15 |
Correct |
1 ms |
620 KB |
Output is correct |
16 |
Correct |
1 ms |
620 KB |
Output is correct |
17 |
Correct |
1 ms |
620 KB |
Output is correct |
18 |
Correct |
1 ms |
748 KB |
Output is correct |
19 |
Correct |
1 ms |
620 KB |
Output is correct |
20 |
Correct |
1 ms |
620 KB |
Output is correct |
21 |
Correct |
2 ms |
748 KB |
Output is correct |
22 |
Correct |
1 ms |
748 KB |
Output is correct |
23 |
Correct |
1 ms |
876 KB |
Output is correct |
24 |
Correct |
1 ms |
748 KB |
Output is correct |
25 |
Correct |
1 ms |
748 KB |
Output is correct |
26 |
Correct |
1 ms |
748 KB |
Output is correct |
27 |
Correct |
1 ms |
748 KB |
Output is correct |
28 |
Correct |
1 ms |
748 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
2 ms |
748 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
5740 KB |
Output is correct |
2 |
Correct |
36 ms |
5868 KB |
Output is correct |
3 |
Correct |
47 ms |
5868 KB |
Output is correct |
4 |
Correct |
36 ms |
5868 KB |
Output is correct |
5 |
Correct |
36 ms |
5868 KB |
Output is correct |
6 |
Correct |
35 ms |
5760 KB |
Output is correct |
7 |
Correct |
35 ms |
5740 KB |
Output is correct |
8 |
Correct |
39 ms |
5996 KB |
Output is correct |
9 |
Correct |
35 ms |
5740 KB |
Output is correct |
10 |
Correct |
35 ms |
5740 KB |
Output is correct |
11 |
Correct |
24 ms |
4204 KB |
Output is correct |
12 |
Correct |
24 ms |
4076 KB |
Output is correct |
13 |
Correct |
26 ms |
4076 KB |
Output is correct |
14 |
Correct |
29 ms |
4076 KB |
Output is correct |
15 |
Correct |
43 ms |
5740 KB |
Output is correct |
16 |
Correct |
24 ms |
4076 KB |
Output is correct |
17 |
Correct |
24 ms |
4076 KB |
Output is correct |
18 |
Correct |
45 ms |
5868 KB |
Output is correct |
19 |
Correct |
36 ms |
5868 KB |
Output is correct |
20 |
Correct |
30 ms |
4204 KB |
Output is correct |
21 |
Correct |
107 ms |
13444 KB |
Output is correct |
22 |
Correct |
102 ms |
13284 KB |
Output is correct |
23 |
Correct |
105 ms |
13412 KB |
Output is correct |
24 |
Correct |
104 ms |
13284 KB |
Output is correct |
25 |
Correct |
109 ms |
13412 KB |
Output is correct |
26 |
Correct |
106 ms |
13412 KB |
Output is correct |
27 |
Correct |
97 ms |
13412 KB |
Output is correct |
28 |
Correct |
96 ms |
13412 KB |
Output is correct |
29 |
Correct |
102 ms |
13540 KB |
Output is correct |
30 |
Correct |
127 ms |
13540 KB |
Output is correct |
31 |
Correct |
1 ms |
620 KB |
Output is correct |
32 |
Correct |
1 ms |
620 KB |
Output is correct |
33 |
Correct |
1 ms |
620 KB |
Output is correct |
34 |
Correct |
1 ms |
620 KB |
Output is correct |
35 |
Correct |
1 ms |
620 KB |
Output is correct |
36 |
Correct |
1 ms |
620 KB |
Output is correct |
37 |
Correct |
1 ms |
620 KB |
Output is correct |
38 |
Correct |
1 ms |
620 KB |
Output is correct |
39 |
Correct |
1 ms |
620 KB |
Output is correct |
40 |
Correct |
1 ms |
620 KB |
Output is correct |
41 |
Correct |
1 ms |
620 KB |
Output is correct |
42 |
Correct |
1 ms |
620 KB |
Output is correct |
43 |
Correct |
1 ms |
620 KB |
Output is correct |
44 |
Correct |
1 ms |
620 KB |
Output is correct |
45 |
Correct |
1 ms |
620 KB |
Output is correct |
46 |
Correct |
1 ms |
620 KB |
Output is correct |
47 |
Correct |
1 ms |
620 KB |
Output is correct |
48 |
Correct |
1 ms |
748 KB |
Output is correct |
49 |
Correct |
1 ms |
620 KB |
Output is correct |
50 |
Correct |
1 ms |
620 KB |
Output is correct |
51 |
Correct |
2 ms |
748 KB |
Output is correct |
52 |
Correct |
1 ms |
748 KB |
Output is correct |
53 |
Correct |
1 ms |
876 KB |
Output is correct |
54 |
Correct |
1 ms |
748 KB |
Output is correct |
55 |
Correct |
1 ms |
748 KB |
Output is correct |
56 |
Correct |
1 ms |
748 KB |
Output is correct |
57 |
Correct |
1 ms |
748 KB |
Output is correct |
58 |
Correct |
1 ms |
748 KB |
Output is correct |
59 |
Correct |
2 ms |
748 KB |
Output is correct |
60 |
Correct |
2 ms |
748 KB |
Output is correct |
61 |
Correct |
68 ms |
13284 KB |
Output is correct |
62 |
Correct |
59 ms |
13540 KB |
Output is correct |
63 |
Correct |
92 ms |
13496 KB |
Output is correct |
64 |
Correct |
58 ms |
13412 KB |
Output is correct |
65 |
Correct |
66 ms |
13432 KB |
Output is correct |
66 |
Correct |
69 ms |
13412 KB |
Output is correct |
67 |
Correct |
82 ms |
13284 KB |
Output is correct |
68 |
Correct |
97 ms |
13412 KB |
Output is correct |
69 |
Correct |
92 ms |
13304 KB |
Output is correct |
70 |
Correct |
92 ms |
13284 KB |
Output is correct |