#include"holiday.h"
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(v) v.begin(),v.end()
#define pb push_back
#define F first
#define S second
template<typename T>
void dbg(const T& t){
cout<<t<<endl;
}
template<typename T,typename... Args>
void dbg(const T& t,const Args&... args){
cout<<t<<" , ";
dbg(args...);
}
#define dbg(...) cout<<"("<<#__VA_ARGS__<<") : ";dbg(__VA_ARGS__);
int n,d;
const int maxn=3001;
ll tree[4*maxn][2];
vector<pair<int,int>> v;
map<int,int> mp;//key = position in initial array, val = position in sorted array
void update(int node,int l,int r,int ql,int x){
if(l==r){
if(!x){
tree[node][0]=0;
tree[node][1]=0;
}else{
tree[node][0]=1;
tree[node][1]=v[l].F;
}
return;
}
int mid=(l+r)/2;
if(ql<=mid) update(node<<1,l,mid,ql,x);
else{
update(node<<1|1,mid+1,r,ql,x);
}
tree[node][0]=tree[node<<1][0]+tree[node<<1|1][0];
tree[node][1]=tree[node<<1][1]+tree[node<<1|1][1];
}
ll query(int node,int l,int r,int k){
if(l==r) {
return (k?tree[node][1]:0);
}
int mid=(l+r)/2;
ll ans=0;
if(tree[node<<1][0]<=k){
k-=tree[node<<1][0];
ans+=tree[node<<1][1];
return ans+query(node<<1|1,mid+1,r,k);
}else{
return query(node<<1,l,mid,k);
}
}
ll findMaxAttraction(int _n, int start, int _d, int arr[]) {
auto calc = [&](int a,int b){
int cost=0;
if(a<=start&&start<=b){
int x=b-a+start-a;
int y=b-start+b-a;
cost=min(x,y);
}
if(start<=a&&a<=b){
cost=b-start;
}
if(a<=b&&b<=start){
cost=start-a;
}
return cost;
};
ll ans=0;
n=_n;
d=_d;
for(int i=0;i<n;i++){
v.pb({arr[i],i});
}
sort(all(v),greater<pair<int,int>>());
for(int i=0;i<n;i++){
mp[v[i].S]=i;
}
for(int i=0;i<n;i++){
for(int j=i;j<n;j++){
int cost=calc(i,j);
int k=d-cost;
update(1,0,n-1,mp[j],1);
if(k<0) continue;
ans=max(ans,query(1,0,n-1,k));
}
for(int j=i;j<n;j++){
update(1,0,n-1,mp[j],0);
}
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
600 KB |
Output is correct |
2 |
Correct |
0 ms |
604 KB |
Output is correct |
3 |
Correct |
0 ms |
604 KB |
Output is correct |
4 |
Correct |
0 ms |
604 KB |
Output is correct |
5 |
Correct |
0 ms |
604 KB |
Output is correct |
6 |
Correct |
1 ms |
604 KB |
Output is correct |
7 |
Correct |
0 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
23 ms |
12496 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1137 ms |
1116 KB |
Output is correct |
2 |
Correct |
942 ms |
1116 KB |
Output is correct |
3 |
Correct |
936 ms |
1136 KB |
Output is correct |
4 |
Correct |
953 ms |
1116 KB |
Output is correct |
5 |
Correct |
793 ms |
1116 KB |
Output is correct |
6 |
Correct |
74 ms |
860 KB |
Output is correct |
7 |
Correct |
70 ms |
856 KB |
Output is correct |
8 |
Correct |
73 ms |
856 KB |
Output is correct |
9 |
Correct |
73 ms |
892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
25 ms |
13264 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |