이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define all(x) x.begin(),x.end()
#define pii pair<int,int>
#define pb push_back
#define sz(x) (int)(x.size())
#define chmin(x,y) x=min(x,y)
#define chmax(x,y) x=max(x,y)
#define vi vector<int>
#define vp vector<pii>
#define vvi vector<vi>
#define ykh mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count())
#define __lg(x) 63-__builtin_clzll(x)
#define pow2(x) (1LL<<x)
void __print(int x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifdef local
void setio(){freopen("/Users/iantsai/Library/Mobile Documents/com~apple~CloudDocs/cpp/Empty.md","r",stdin);}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
void setio(){}
#define debug(x...)
#endif
const long long mxn=1e5+5;
multiset<long long>bg,sm;
long long n,st,d,L,R;
long long cur,v[mxn];
void sir(long l,long r,long k){
while(L>l){
bg.insert(v[--L]);
cur+=v[L];
}
while(R<r){
bg.insert(v[++R]);
cur+=v[R];
}
while(L<l){
if(bg.find(v[L])!=bg.end()){
cur-=v[L];
bg.erase(bg.find(v[L]));
}
else{
sm.erase(sm.find(v[L]));
}
L++;
}
while(R>r){
if(bg.find(v[R])!=bg.end()){
cur-=v[R];
bg.erase(bg.find(v[R]));
}
else{
sm.erase(sm.find(v[R]));
}
R--;
}
while(sz(bg)>k){
cur-=*bg.begin();
sm.insert(*bg.begin());
bg.erase(bg.begin());
}
while(sz(bg)<k){
cur+=*prev(sm.end());
bg.insert(*prev(sm.end()));
sm.erase(prev(sm.end()));
}
}
long long ans;
void solve(long long l,long long r,long long ql,long long qr){
if(l>r)return;
long long mm=(l+r)/2;
long long mx=0;
long long best=ql;
for(long long i=ql;i<=qr;i++){
long long x=min(i-mm+1,d-(min(i-st,st-mm)+i-mm));
if(x<=0)continue;
sir(mm,i,x);
if(cur>mx){
best=i;
mx=cur;
}
}
chmax(ans,mx);
solve(l,mm-1,ql,best);
solve(mm+1,r,best,qr);
}
long long findMaxAttraction(int N,int ST,int D,int a[]){
n=N,st=ST,d=D,cur=0,L=0,R=n-1;
for(long long i=0;i<n;i++){
v[i]=a[i];
bg.insert(v[i]);
cur+=v[i];
}
solve(0,st,st,n-1);
return ans;
}
#ifdef local
int main(){
setio();
int a,b,c;
cin>>a>>b>>c;
int x[n];
for(int i=0;i<a;i++){
cin>>x[i];
}
cout<<findMaxAttraction(a,b,c,x)<<'\n';
}
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
/*
input:
*/
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |