# |
Submission time |
Handle |
Problem |
Language |
Result |
Execution time |
Memory |
369269 |
2021-02-21T05:24:14 Z |
Berted |
Feast (NOI19_feast) |
C++17 |
|
0 ms |
0 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.tiie(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:14:42: error: 'std::ostream' {aka 'class std::basic_ostream<char>'} has no member named 'tiie'; did you mean 'tie'?
14 | ios::sync_with_stdio(0);cin.tie(0);cout.tiie(0);
| ^~~~
| tie
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)
| ~~~~~~~~^~