# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
921789 | 1075508020060209tc | Cake 3 (JOI19_cake3) | C++14 | 0 ms | 0 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.
#pragma GCC optimize("O3")
//#include "meetings.h"
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define X first
#define Y second
#define SZ(x) (int)(x).size()
int K;
int ksum;
multiset<int>tmst;
multiset<int>bmst;
void mINS(int v){
tmst.insert(v);
ksum+=v;
if(tmst.size()>K){
ksum-=*(tmst.begin());
bmst.insert(*tmst.begin());
tmst.erase(tmst.begin());
}
}
void mDEL(int v){
if(bmst.find(v)!=bmst.end()){
bmst.erase(bmst.find(v));
return;
}
ksum-=v;
tmst.erase(tmst.find(v));
if(tmst.size()<K){
if(bmst.size()){
tmst.insert(*(bmst.rbegin()));
ksum+=*bmst.rbegin();
bmst.erase(prev(bmst.end()));
}
}
}
int mlit;int mrit;
int n;
pair<int,int>ar[200005];
int ans[200005];
vector<pair<int,int>>targ;vector<pair<int,int>>tbrg;
void solve(vector<pair<int,int>>arg,vector<pair<int,int>>brg){
vector<int>apt;
for(int i=0;i<arg.size();i++){
apt.push_back((arg[i].first+arg[i].second)/2);
}
int Q=arg.size();
mlit=n;
mrit=n;
mINS(ar[n].second);
for(int i=Q-1;i>=0;i--){
int qmi=apt[i];
while(mlit>min(qmi-K,brg[i].second+1)){
mINS(ar[--mlit].second);
}
while(mrit>qmi){
mDEL(ar[mrit--].second);
}
int opt;
ans[qmi]=-1e12;
while(mlit>brg[i].first){
int cal=ar[qmi].secod-ar[qmi].first+ar[mlit-1].first+ar[mlit-1].second+ksum;
if(cal>ans[qmi]){
ans[qmi]=cal;
opt=mlit-1;
}
if(mlit-1==brg[i].first){break;}
mlit--;
mINS(ar[mlit].second);
}
}
}
signed main(){
cin>>n>>K;
for(int i=1;i<=n;i++){
cin>>ar[i].second>>ar[i].first;
ar[i].first*=2;
}
K-=2;
sort(ar+1,ar+n+1);
vector<pair<int,int>>vc;vector<pair<int,int>>vc2;
vc.push_back({K+2,n});
vc2.push_back({1,n-K-3});
solve(vc,vc2);
}