이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
//#pragma GCC optimize("")
using namespace std;
typedef long long ll;
const ll N = 2e5,logN = 32,maxa = 1e9;
const ll inf64 = 9ll*1e9*1e9;
const ll inf32 = 2e9;
struct numbers{
ll v,c;
}v[N];
struct nod{
ll cnt = 0,sum = 0,l = 0,r = 0;
};
nod seg[logN*N];
ll root[N];
ll n,k;
ll cnt2 = 1;
void upd(ll old,ll cur,ll val,ll l = 0,ll r = maxa){
if(l == r){
seg[cur].sum+=val;
seg[cur].cnt++;
}else{
ll mij = (l + r)/2;
if(val <= mij){
seg[cur].r = seg[old].r;
seg[cur].l = cnt2++;
upd(seg[old].l,seg[cur].l,val,l,mij);
}else{
seg[cur].r = cnt2++;
seg[cur].l = seg[old].l;
upd(seg[old].r,seg[cur].r,val,mij + 1,r);
}
seg[cur].cnt = seg[seg[cur].l].cnt + seg[seg[cur].r].cnt;
seg[cur].sum = seg[seg[cur].l].sum + seg[seg[cur].r].sum;
}
}
ll get(ll old,ll cur,ll nr = k,ll l = 0,ll r = maxa){
//cout<<old<<' '<<cur<<' '<<nr<<' '<<l<<' '<<r<<'\n';
if(l == r){
//cout<<nr*l<<'\n';
return nr*l;
}else{
ll mij = (l + r)/2;
if(seg[seg[cur].r].cnt - seg[seg[old].r].cnt >= nr){
return get(seg[old].r,seg[cur].r,nr,mij + 1,r);
}else{
//cout<<seg[seg[cur].r].sum - seg[seg[old].r].sum<<'\n';
return seg[seg[cur].r].sum - seg[seg[old].r].sum + get(seg[old].l,seg[cur].l,nr - (seg[seg[cur].r].cnt - seg[seg[old].r].cnt),l,mij);
}
}
}
ll ans = -inf64;
void solve(ll l,ll r,ll l2,ll r2){
if(l > r)return;
///haha shitty greedy wooo yeah
ll score = -inf64,id = l2;
ll mij = (l + r)/2,i;
for(i = l2;i <= min(mij - k + 1,r2);i++){
ll nr = get((i==0?0:root[i-1]),root[mij]);
if(nr - v[mij].c + v[i].c > score){
score = nr - v[mij].c + v[i].c;
id = i;
}
}
ans = max(ans,score);
solve(l,mij - 1,l2,id);
solve(mij + 1,r,id,r2);
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll i,j;
cin>>n>>k;
for(i = 0;i < n;i++){
cin>>v[i].v>>v[i].c;
v[i].c*=2;
}
sort(v,v + n,[&](numbers a,numbers b){
return a.c < b.c;
});
for(i = 0;i < n;i++){
root[i] = cnt2++;
upd(i == 0?0:root[i-1],root[i],v[i].v);
}
solve(0,n - 1,0,n - 1);
/*for(i = 0;i < n;i++){
for(j = i + k - 1;j < n;j++){
cout<<get(i == 0?0:root[i - 1],root[j])<<' '<<i<<' '<<j<<'\n';
}
}*/
cout<<ans;
return 0;
}
/**
5 3
2 1
4 2
6 4
8 8
10 16
**/
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int main()':
cake3.cpp:72:10: warning: unused variable 'j' [-Wunused-variable]
72 | ll i,j;
| ^
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |