이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include "holiday.h"
#include <bits/stdc++.h>
#define max_n 1<<17
#define ll long long
using namespace std;
int n, d, start, L, R;
ll res;
pair<ll,int> segment[max_n<<1];
int id[max_n];
int ord[max_n];
int a[max_n];
ll get(int x) {
int k = 1;
ll sum = 0;
while(x < max_n) {
if(segment[k+k+1].second >= x) k = k+k+1;
else {
x -= segment[k+k+1].second;
sum += segment[k+k+1].first;
k>>=1;
}
}
sum += (x > 0) * segment[k].first;
return sum;
}
void add(int x) {
segment[x+max_n] = make_pair(a[ord[x]],1);
x+= max_n;
x>>=1;
for(;x>1;x>>=1) {
segment[x].first = segment[x+x].first + segment[x+x+1].first;
segment[x].second = segment[x+x].second + segment[x+x+1].second;
}
}
void erase(int x) {
segment[x+max_n] = make_pair(0,0);
x+=max_n;
x>>=1;
for(;x>1;x>>=1) {
segment[x].first = segment[x+x].first + segment[x+x+1].first;
segment[x].second = segment[x+x].second + segment[x+x+1].second;
}
}
void fix(int l, int r) {
while(l>L) erase(id[L++]);
while(r<R) erase(id[R--]);
while(l<L) add(id[--L]);
while(r>R) add(id[++R]);
}
bool cmp(int x, int y) {
return a[x]<a[y];
}
void solve(int l, int r, int sl, int sr) {
if(l>r) return;
int mid = (l+r)>>1;
ll ress = 0;
int opt = -1;
for(int i = sl; i<=sr; i++) {
if(i>mid) break;
fix(i,mid);
ll tmp = get(d-(mid-i)-(mid-start));
if(opt==-1 or tmp>=ress) {
res = max(res, tmp);
ress=tmp;
opt=i;
}
}
solve(l, mid-1, sl,opt);
solve(mid+1,r,opt,sr);
}
void solveStart(void) {
for(int i=1; i<=n; i++) ord[i] = i;
sort(ord+1, ord+n+1, cmp);
for(int i = 1; i <= n; i++)
id[ord[i]] = i;
L = 1;
R = 0;
memset(segment, 0, sizeof(segment));
for(int i = start; i <= n; i++) {
add(id[i]);
res = max(res, get(d - (i - start)));
}
memset(segment, 0, sizeof(segment));
solve(start, n, 1, start);
}
long long int findMaxAttraction(int N, int Start, int D, int attraction[]) {
n = N, d = D, start = Start+1;
for (int i = 0; i < N; ++i)
a[i+1] = attraction[i];
solveStart();
reverse(a+1, a+n+1);
start = n - start + 1;
solveStart();
return res;
}
컴파일 시 표준 에러 (stderr) 메시지
holiday.cpp: In function 'void add(int)':
holiday.cpp:28:11: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
segment[x+max_n] = make_pair(a[ord[x]],1);
^
holiday.cpp: In function 'void erase(int)':
holiday.cpp:37:11: warning: suggest parentheses around '+' inside '<<' [-Wparentheses]
segment[x+max_n] = make_pair(0,0);
^
grader.cpp: In function 'int main()':
grader.cpp:7:12: warning: variable 'n_s' set but not used [-Wunused-but-set-variable]
int i, n_s;
^
# | 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... |