이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, pii> pipii;
typedef pair<pii, int> piipi;
typedef pair<pii, pii> piipii;
#define mp make_pair
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(a) (int)(a).size()
#define eb emplace_back
ll findMaxAttraction(int n, int start, int d, int attraction[]){
long long ans = 0;
if(n <= 3000){
for(int l=0;l<n;l++){
multiset<int> mx1, mx2;
ll res = 0;
for(int r=l;r<n;r++){
int cost = 0;
if(l <= start && start <= r) cost = min(abs(start-l + (r-l)), abs(r-start + (r-l)));
else if(r <= start) cost = start-l;
else cost = r-start;
if(d-cost < 0) continue;
mx1.insert(attraction[r]);
while(sz(mx2) < d-cost){
if(mx1.empty()) break;
int val = *mx1.rbegin();
mx1.erase(--mx1.end());
res += val;
mx2.insert(val);
}
while(sz(mx2) > d-cost){
int val = *mx2.begin();
mx2.erase(mx2.begin());
res -= val;
mx1.insert(val);
}
while(!mx1.empty() && !mx2.empty() && *mx1.rbegin() > *mx2.begin()){
int val1 = *mx1.rbegin(), val2 = *mx2.begin();
mx1.erase(--mx1.end());
mx2.erase(mx2.begin());
res -= val2;
res += val1;
mx1.insert(val2);
mx2.insert(val1);
}
ans = max(ans, res);
}
}
// for(int i=1;i<(1<<n);i++){
// vector<int> b;
// int l = 1e9, r = 0;
// for(int j=0;j<n;j++){
// if((i>>j)&1){
// b.eb(j);
// l = min(l, j);
// r = max(r, j);
// }
// }
// int cost = 0;
// if(l <= start && start <= r) cost = min(abs(start-l + (r-l)), abs(r-start + (r-l)));
// else if(r <= start) cost = start-l;
// else cost = r-start;
// if(cost > d) continue;
// vector<int> c;
// for(int i=0;i<sz(b);i++){
// int val = attraction[b[i]];
// c.eb(val);
// }
// sort(all(c));
// reverse(all(c));
// ll res = 0;
// int cnt = 0;
// for(int i=0;i<sz(c);i++){
// if(cnt < d-cost){
// cnt++;
// res += c[i];
// }
// }
// ans = max(ans, res);
// }
}
else if(start == 0){
multiset<int> mx1, mx2;
long long res = 0;
for(int i=0;i<n;i++){
mx1.insert(attraction[i]);
while(sz(mx2) < d){
if(mx1.empty()) break;
int val = *mx1.rbegin();
mx1.erase(--mx1.end());
res += val;
mx2.insert(val);
}
while(sz(mx2) > d){
int val = *mx2.begin();
mx2.erase(mx2.begin());
res -= val;
mx1.insert(val);
}
while(!mx1.empty() && !mx2.empty() && *mx1.rbegin() > *mx2.begin()){
int val1 = *mx1.rbegin(), val2 = *mx2.begin();
mx1.erase(--mx1.end());
mx2.erase(mx2.begin());
res -= val2;
res += val1;
mx1.insert(val2);
mx2.insert(val1);
}
ans = max(ans, res);
d--;
if(d < 0) break;
}
}
return ans;
}
# | 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... |