이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
//#pragma GCC optimize("O3")
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pii pair<int,int>
#define f first
#define s second
#define all(x) x.begin(),x.end()
#define _ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
const int mxn=2e5+5;
const ll inf=1e18;
ll v[mxn],c[mxn];
ll pre[mxn];
ll sum;
multiset<int> S,T;
int n,m;
int L,R;
ll ans=-inf;
void sir(int l,int r){
while(L>l){
L--;
S.insert(v[L]);
sum+=v[L];
if((int)S.size()>m){
sum-=*S.begin();
T.insert(*S.begin());
S.erase(S.begin());
}
}
while(R<r){
R++;
S.insert(v[R]);
sum+=v[R];
if((int)S.size()>m){
sum-=*S.begin();
T.insert(*S.begin());
S.erase(S.begin());
}
}
while(L<l){
if(T.find(v[L])!=T.end()){
T.erase(T.find(v[L]));
}
else{
sum+=*T.rbegin();
S.insert(*T.rbegin());
T.erase(prev(T.end()));
sum-=v[L];
S.erase(S.find(v[L]));
}
L++;
}
while(R>r){
if(T.find(v[R])!=T.end()){
T.erase(T.find(v[R]));
}
else{
sum+=*T.rbegin();
S.insert(*T.rbegin());
T.erase(prev(T.end()));
sum-=v[R];
S.erase(S.find(v[R]));
}
R--;
}
}
void go(int l,int r,int tl,int tr){
if(l>r){
return;
}
int mid=(l+r)/2;
int best=tl;
ll cur=-inf;
for(int i=tl;i<=min(tr,mid-m+1);i++){
sir(i,mid);
ll x=sum-2*(c[mid]-c[i]);
if(x>cur){
cur=x;
best=i;
}
}
ans=max(ans,cur);
go(l,mid-1,tl,best);
go(mid+1,r,best,tr);
}
int main() {_
cin>>n>>m;
vector<pair<int,int>> vec(n);
for(int i=0;i<n;i++){
cin>>vec[i].s>>vec[i].f;
}
sort(all(vec));
for(int i=1;i<=n;i++){
v[i]=vec[i-1].s;
}
for(int i=1;i<=n;i++){
c[i]=vec[i-1].f;
}
L=1;
R=n;
for(int i=1;i<=n;i++){
S.insert(v[i]);
sum+=v[i];
if((int)S.size()>m){
sum-=*S.begin();
T.insert(*S.begin());
S.erase(S.begin());
}
}
go(m,n,1,n);
cout<<ans<<'\n';
return 0;
}
//maybe its multiset not set
//yeeorz
//laborz
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'void setIO(std::string)':
cake3.cpp:15:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
15 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cake3.cpp:16:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
16 | freopen((s + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |