# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
168802 | Akashi | Cake 3 (JOI19_cake3) | C++14 | 4029 ms | 14396 KiB |
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;
multiset <long long> s1, s2;
long long S = 0;
void add(int i){
if(s2.size() < m){
s2.insert(a[i].v);
S += a[i].v;
}
else{
if(*s2.begin() < a[i].v){
s1.insert(*s2.begin());
S -= *s2.begin();
s2.erase(s2.begin());
s2.insert(a[i].v);
S += a[i].v;
}
else s1.insert(a[i].v);
}
}
void rem(int i){
if(s2.find(a[i].v) != s2.end()){
s2.erase(s2.find(a[i].v));
S -= a[i].v;
if(!s1.empty()){
s2.insert(*s1.rbegin());
S += *s1.rbegin();
s1.erase(s1.find(*s1.rbegin()));
}
}
else s1.erase(s1.find(a[i].v));
}
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 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;
}
}
if(st == dr || sol == -INF) return sol;
return max(sol, max(solve(st, mij, down, best), solve(mij + 1, dr, best, up)));
}
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);
printf("%lld", solve(1, n - m + 1, m, n));
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |