#include<bits/stdc++.h>
#include"holiday.h"
#define MAXN 500007
using namespace std;
int n,a[MAXN],start;
vector<int> w;
unordered_map<int,int> mp;
int ret[MAXN],cnt;
long long ans;
struct ST{
pair<long long,int> tree[4*MAXN];
void init(){
for(int i=1;i<=4*cnt;i++){
tree[i]={0,0};
}
}
pair<long long,int> combine(pair<long long,int> fr,pair<long long,int> sc){
return {fr.first+sc.first,fr.second+sc.second};
}
void update(int v,int l,int r,int pos,int val){
if(l==r){
tree[v].second+=val;
tree[v].first+=ret[l]*val;
}else{
int tt=(l+r)/2;
if(pos<=tt)update(2*v,l,tt,pos,val);
else update(2*v+1,tt+1,r,pos,val);
tree[v]=combine(tree[2*v],tree[2*v+1]);
}
}
long long getsum(int v,int l,int r,long long k){
if(k<0)return 0;
if(l==r){
if(tree[v].second<=k)return tree[v].first;
return k*ret[l];
}else{
int tt=(l+r)/2;
if(tree[2*v+1].second>=k){
return getsum(2*v+1,tt+1,r,k);
}else{
return getsum(2*v,l,tt,k-tree[2*v+1].second) + tree[2*v+1].first;
}
}
}
}seg;
long long pref[MAXN],suff[MAXN];
void solveopt_right(int dl,int dr,int optl,int optr){
if(dl>dr)return;
int mid=(dl+dr)/2,opt;
suff[mid]=0;
for(int i=optl;i<=optr;i++){
seg.update(1,1,cnt,a[i],1);
long long curr=seg.getsum(1,1,cnt,mid-(i-start));
if(curr>=suff[mid]){
suff[mid]=curr;
opt=i;
}
}
if(suff[mid]==0)opt=optl;
for(int i=optl;i<=optr;i++)seg.update(1,1,cnt,a[i],-1);
solveopt_right(dl,mid-1,optl,min(optr,opt+1));
for(int i=optl;i<max(opt-1,optl);i++)seg.update(1,1,cnt,a[i],1);
solveopt_right(mid+1,dr,max(opt-1,optl),optr);
for(int i=optl;i<max(opt-1,optl);i++)seg.update(1,1,cnt,a[i],-1);
}
void solveopt_left(int dl,int dr,int optl,int optr){
if(dl>dr)return;
int mid=(dl+dr)/2,opt;
pref[mid]=0;
for(int i=optr;i>=optl;i--){
seg.update(1,1,cnt,a[i],1);
long long curr=seg.getsum(1,1,cnt,mid-2*(start-1-i));
if(curr>=pref[mid]){
pref[mid]=curr;
opt=i;
}
}
if(pref[mid]==0)opt=optr;
for(int i=optl;i<=optr;i++)seg.update(1,1,cnt,a[i],-1);
solveopt_left(dl,mid-1,opt,optr);
for(int i=opt+1;i<=optr;i++)seg.update(1,1,cnt,a[i],1);
solveopt_left(mid+1,dr,optl,opt);
for(int i=opt+1;i<=optr;i++)seg.update(1,1,cnt,a[i],-1);
}
long long int findMaxAttraction(int N, int st, int d, int attraction[]) {
//long long int findMaxAttraction(int N, int st, int d, vector<int> attraction) {
n=N; start=st+1;
for(int i=1;i<=n;i++){
a[i]=attraction[i-1];
w.push_back(a[i]);
}
sort(w.begin(),w.end());
for(int i=0;i<w.size();i++){
if(i==0 or w[i]!=w[i-1])cnt++;
mp[w[i]]=cnt; ret[cnt]=w[i];
}
for(int i=1;i<=n;i++)a[i]=mp[a[i]];
seg.init();
solveopt_right(0,d,start,n);
seg.init();
solveopt_left(0,d,1,start-1);
ans=max(ans,suff[d]);
for(int i=0;i<=d-2;i++){
ans=max(ans,pref[i]+suff[d-2-i]);
}
reverse(a+1,a+n+1);
start=n-start+1;
seg.init();
solveopt_right(0,d,start,n);
seg.init();
solveopt_left(0,d,1,start-1);
ans=max(ans,suff[d]);
for(int i=0;i<=d-2;i++){
ans=max(ans,pref[i]+suff[d-2-i]);
}
return ans;
}
/*int main(){
cout<<findMaxAttraction(5,2,7,{10,2,20,30,1})<<"\n";
return 0;
}*/
Compilation message
holiday.cpp: In function 'long long int findMaxAttraction(int, int, int, int*)':
holiday.cpp:127:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
127 | for(int i=0;i<w.size();i++){
| ~^~~~~~~~~
holiday.cpp: In function 'void solveopt_right(int, int, int, int)':
holiday.cpp:83:26: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
83 | for(int i=optl;i<max(opt-1,optl);i++)seg.update(1,1,cnt,a[i],1);
| ~~~^~
holiday.cpp: In function 'void solveopt_left(int, int, int, int)':
holiday.cpp:111:10: warning: 'opt' may be used uninitialized in this function [-Wmaybe-uninitialized]
111 | for(int i=opt+1;i<=optr;i++)seg.update(1,1,cnt,a[i],1);
| ^
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
9040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
14024 KB |
Output is correct |
2 |
Correct |
29 ms |
14112 KB |
Output is correct |
3 |
Correct |
57 ms |
14096 KB |
Output is correct |
4 |
Correct |
49 ms |
14100 KB |
Output is correct |
5 |
Correct |
341 ms |
14004 KB |
Output is correct |
6 |
Correct |
151 ms |
13384 KB |
Output is correct |
7 |
Correct |
192 ms |
13508 KB |
Output is correct |
8 |
Correct |
223 ms |
13656 KB |
Output is correct |
9 |
Correct |
145 ms |
13384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
28 ms |
9040 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
38 ms |
14036 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |