This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct cake{
int v, c;
bool operator < (const cake &aux)const{
if(c != aux.c) return c < aux.c;
return v > aux.v;
}
};
cake a[200005];
int n, m;
set <pair <long long, int> > s1, s2;
long long S = 0;
void add(int i){
if(s2.size() < m){
s2.insert({a[i].v, i});
S += a[i].v;
}
else{
if(s2.begin()->first < a[i].v){
s1.insert({s2.begin()->first, s2.begin()->second});
S -= s2.begin()->first;
s2.erase(s2.begin());
s2.insert({a[i].v, i});
S += a[i].v;
}
else s1.insert({a[i].v, i});
}
}
void rem(int i){
if(s2.find({a[i].v, i}) != s2.end()){
s2.erase({a[i].v, i});
S -= a[i].v;
if(!s1.empty()){
s2.insert({s1.rbegin()->first, s1.rbegin()->second});
S += s1.rbegin()->first;
s1.erase({s1.rbegin()->first, s1.rbegin()->second});
}
}
else s1.erase({a[i].v, i});
}
int l = 1, r = 0;
long long calc(int st, int dr){
while(r < dr) add(++r);
while(l > st) add(--l);
while(l < st) rem(l++);
while(r > dr) rem(r--);
if(dr - st + 1 < m) return -INF;
return S;
}
long long ans = -INF;
void solve(int st, int dr, int down, int up){
int mij = (st + dr) / 2;
long long sol = -INF, best = 0;
for(int i = max(down, mij); i <= up ; ++i){
long long val = calc(mij, i) - (a[i].c - a[mij].c);
if(val > sol){
sol = val;
best = i;
}
}
ans = max(ans, sol);
if(st == dr) return ;
solve(mij + 1, dr, best, up);
solve(st, mij, down, best);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n ; ++i){
scanf("%d%d", &a[i].v, &a[i].c);
a[i].c *= 2;
}
sort(a + 1, a + n + 1);
solve(1, n - m + 1, m, n);
printf("%lld", ans);
return 0;
}
Compilation message (stderr)
cake3.cpp: In function 'void add(int)':
cake3.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(s2.size() < m){
~~~~~~~~~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:83:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:86:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i].v, &a[i].c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |