# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
369271 |
2021-02-21T05:25:44 Z |
Berted |
Feast (NOI19_feast) |
C++17 |
|
905 ms |
15588 KB |
#include <iostream>
#include <queue>
#include <set>
#define ll long long
#define pii pair<int,int>
#define fst first
#define snd second
#define ppp pair<pair<ll,int>,pii>
using namespace std;
int n,k,l=-1;ll ar[300001]={},to=0;set<pii> s;
priority_queue<ppp> pq;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>k;
for (int i=1;i<=n;i++)
{
cin>>ar[i];
if (ar[i]>=0)
{
if (l==-1) {l = i;}
}
else
{
if (l!=-1) {to+=ar[i-1] - ar[l-1];s.insert({l,i-1});l=-1;}
}
ar[i] += ar[i-1];
}
if (l!=-1) {s.insert({l,n});to+=ar[n] - ar[l-1];}
l = -1;
for (auto dt:s)
{
pq.push({{-(ar[dt.snd] - ar[dt.fst-1]),0},{dt.fst,dt.snd}});
if (l!=-1)
{
pq.push({{ar[dt.fst-1] - ar[l],1},{l+1,dt.fst-1}});
}
l = dt.snd;
}
while (s.size()>k)
{
while (pq.size())
{
if (pq.top().fst.snd)
{
auto it = s.lower_bound({pq.top().snd.snd,pq.top().snd.snd});
pii dt = {-1,-1},dt2 = {-1,-1};
if (it!=s.begin()) {dt2 = *prev(it);}
if (it!=s.end()) {dt = *it;}
if (make_pair(dt2.snd+1,dt.fst-1)!=pq.top().snd) {pq.pop();}
else {break;}
}
else
{
auto it = s.lower_bound({pq.top().snd.fst,pq.top().snd.snd});
pii dt = {-1,-1};
if (it!=s.end()) {dt = *it;}
if (dt!=pq.top().snd) {pq.pop();}
else {break;}
}
}
to += pq.top().fst.fst;
if (pq.top().fst.snd)
{
auto it = s.lower_bound(pq.top().snd);
int l=prev(it)->fst,r=it->snd;
s.erase(prev(it));s.erase(it);pq.pop();
s.insert({l,r});
pq.push({{-(ar[r] - ar[l-1]),0},{l,r}});
}
else
{
auto it = s.lower_bound(pq.top().snd);
int l=pq.top().snd.fst,r=pq.top().snd.snd;
if (next(it)!=s.end()) {r = next(it)->fst-1;}
if (it!=s.begin()) {l = prev(it)->snd+1;}
s.erase(pq.top().snd);pq.pop();
pq.push({{ar[r] - ar[l-1],1},{l,r}});
}
}
cout<<to<<"\n";
return 0;
}
Compilation message
feast.cpp: In function 'int main()':
feast.cpp:40:17: warning: comparison of integer expressions of different signedness: 'std::set<std::pair<int, int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
40 | while (s.size()>k)
| ~~~~~~~~^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
5484 KB |
Output is correct |
2 |
Correct |
36 ms |
5484 KB |
Output is correct |
3 |
Correct |
36 ms |
5612 KB |
Output is correct |
4 |
Correct |
36 ms |
5484 KB |
Output is correct |
5 |
Correct |
35 ms |
5484 KB |
Output is correct |
6 |
Correct |
35 ms |
5484 KB |
Output is correct |
7 |
Correct |
35 ms |
5484 KB |
Output is correct |
8 |
Correct |
44 ms |
5512 KB |
Output is correct |
9 |
Correct |
35 ms |
5484 KB |
Output is correct |
10 |
Correct |
47 ms |
5484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
24 ms |
3820 KB |
Output is correct |
2 |
Correct |
28 ms |
3820 KB |
Output is correct |
3 |
Correct |
23 ms |
3692 KB |
Output is correct |
4 |
Correct |
24 ms |
3948 KB |
Output is correct |
5 |
Correct |
35 ms |
5484 KB |
Output is correct |
6 |
Correct |
25 ms |
3948 KB |
Output is correct |
7 |
Correct |
24 ms |
3820 KB |
Output is correct |
8 |
Correct |
905 ms |
5640 KB |
Output is correct |
9 |
Correct |
34 ms |
5356 KB |
Output is correct |
10 |
Correct |
425 ms |
3820 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
242 ms |
15444 KB |
Output is correct |
2 |
Correct |
226 ms |
15328 KB |
Output is correct |
3 |
Correct |
220 ms |
15580 KB |
Output is correct |
4 |
Correct |
219 ms |
15324 KB |
Output is correct |
5 |
Correct |
220 ms |
15452 KB |
Output is correct |
6 |
Correct |
231 ms |
15580 KB |
Output is correct |
7 |
Correct |
251 ms |
15452 KB |
Output is correct |
8 |
Correct |
230 ms |
15456 KB |
Output is correct |
9 |
Correct |
234 ms |
15588 KB |
Output is correct |
10 |
Correct |
239 ms |
15584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
35 ms |
364 KB |
Output is correct |
9 |
Correct |
30 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
35 ms |
364 KB |
Output is correct |
9 |
Correct |
30 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
30 ms |
364 KB |
Output is correct |
13 |
Correct |
58 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
67 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
492 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
35 ms |
364 KB |
Output is correct |
9 |
Correct |
30 ms |
364 KB |
Output is correct |
10 |
Correct |
1 ms |
364 KB |
Output is correct |
11 |
Correct |
1 ms |
364 KB |
Output is correct |
12 |
Correct |
30 ms |
364 KB |
Output is correct |
13 |
Correct |
58 ms |
396 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
364 KB |
Output is correct |
16 |
Correct |
1 ms |
364 KB |
Output is correct |
17 |
Correct |
1 ms |
364 KB |
Output is correct |
18 |
Correct |
67 ms |
364 KB |
Output is correct |
19 |
Correct |
1 ms |
364 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
68 ms |
640 KB |
Output is correct |
22 |
Correct |
1 ms |
492 KB |
Output is correct |
23 |
Correct |
2 ms |
492 KB |
Output is correct |
24 |
Correct |
1 ms |
492 KB |
Output is correct |
25 |
Correct |
1 ms |
492 KB |
Output is correct |
26 |
Correct |
103 ms |
520 KB |
Output is correct |
27 |
Correct |
2 ms |
492 KB |
Output is correct |
28 |
Correct |
2 ms |
492 KB |
Output is correct |
29 |
Correct |
70 ms |
492 KB |
Output is correct |
30 |
Correct |
1 ms |
492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
36 ms |
5484 KB |
Output is correct |
2 |
Correct |
36 ms |
5484 KB |
Output is correct |
3 |
Correct |
36 ms |
5612 KB |
Output is correct |
4 |
Correct |
36 ms |
5484 KB |
Output is correct |
5 |
Correct |
35 ms |
5484 KB |
Output is correct |
6 |
Correct |
35 ms |
5484 KB |
Output is correct |
7 |
Correct |
35 ms |
5484 KB |
Output is correct |
8 |
Correct |
44 ms |
5512 KB |
Output is correct |
9 |
Correct |
35 ms |
5484 KB |
Output is correct |
10 |
Correct |
47 ms |
5484 KB |
Output is correct |
11 |
Correct |
24 ms |
3820 KB |
Output is correct |
12 |
Correct |
28 ms |
3820 KB |
Output is correct |
13 |
Correct |
23 ms |
3692 KB |
Output is correct |
14 |
Correct |
24 ms |
3948 KB |
Output is correct |
15 |
Correct |
35 ms |
5484 KB |
Output is correct |
16 |
Correct |
25 ms |
3948 KB |
Output is correct |
17 |
Correct |
24 ms |
3820 KB |
Output is correct |
18 |
Correct |
905 ms |
5640 KB |
Output is correct |
19 |
Correct |
34 ms |
5356 KB |
Output is correct |
20 |
Correct |
425 ms |
3820 KB |
Output is correct |
21 |
Correct |
242 ms |
15444 KB |
Output is correct |
22 |
Correct |
226 ms |
15328 KB |
Output is correct |
23 |
Correct |
220 ms |
15580 KB |
Output is correct |
24 |
Correct |
219 ms |
15324 KB |
Output is correct |
25 |
Correct |
220 ms |
15452 KB |
Output is correct |
26 |
Correct |
231 ms |
15580 KB |
Output is correct |
27 |
Correct |
251 ms |
15452 KB |
Output is correct |
28 |
Correct |
230 ms |
15456 KB |
Output is correct |
29 |
Correct |
234 ms |
15588 KB |
Output is correct |
30 |
Correct |
239 ms |
15584 KB |
Output is correct |
31 |
Correct |
1 ms |
364 KB |
Output is correct |
32 |
Correct |
1 ms |
364 KB |
Output is correct |
33 |
Correct |
1 ms |
364 KB |
Output is correct |
34 |
Correct |
1 ms |
364 KB |
Output is correct |
35 |
Correct |
1 ms |
364 KB |
Output is correct |
36 |
Correct |
1 ms |
492 KB |
Output is correct |
37 |
Correct |
1 ms |
364 KB |
Output is correct |
38 |
Correct |
35 ms |
364 KB |
Output is correct |
39 |
Correct |
30 ms |
364 KB |
Output is correct |
40 |
Correct |
1 ms |
364 KB |
Output is correct |
41 |
Correct |
1 ms |
364 KB |
Output is correct |
42 |
Correct |
30 ms |
364 KB |
Output is correct |
43 |
Correct |
58 ms |
396 KB |
Output is correct |
44 |
Correct |
1 ms |
384 KB |
Output is correct |
45 |
Correct |
1 ms |
364 KB |
Output is correct |
46 |
Correct |
1 ms |
364 KB |
Output is correct |
47 |
Correct |
1 ms |
364 KB |
Output is correct |
48 |
Correct |
67 ms |
364 KB |
Output is correct |
49 |
Correct |
1 ms |
364 KB |
Output is correct |
50 |
Correct |
1 ms |
384 KB |
Output is correct |
51 |
Correct |
68 ms |
640 KB |
Output is correct |
52 |
Correct |
1 ms |
492 KB |
Output is correct |
53 |
Correct |
2 ms |
492 KB |
Output is correct |
54 |
Correct |
1 ms |
492 KB |
Output is correct |
55 |
Correct |
1 ms |
492 KB |
Output is correct |
56 |
Correct |
103 ms |
520 KB |
Output is correct |
57 |
Correct |
2 ms |
492 KB |
Output is correct |
58 |
Correct |
2 ms |
492 KB |
Output is correct |
59 |
Correct |
70 ms |
492 KB |
Output is correct |
60 |
Correct |
1 ms |
492 KB |
Output is correct |
61 |
Correct |
352 ms |
15456 KB |
Output is correct |
62 |
Correct |
75 ms |
15580 KB |
Output is correct |
63 |
Correct |
214 ms |
15328 KB |
Output is correct |
64 |
Correct |
105 ms |
15584 KB |
Output is correct |
65 |
Correct |
169 ms |
15452 KB |
Output is correct |
66 |
Correct |
151 ms |
15580 KB |
Output is correct |
67 |
Correct |
147 ms |
15328 KB |
Output is correct |
68 |
Correct |
570 ms |
15580 KB |
Output is correct |
69 |
Correct |
233 ms |
15324 KB |
Output is correct |
70 |
Correct |
226 ms |
15328 KB |
Output is correct |