# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
30517 | ozaslan | Holiday (IOI14_holiday) | C++14 | 0 ms | 0 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <string>
#include <utility>
#include <algorithm>
#include <stdio.h>
#include "holiday.h"
#define mp make_pair
#define ff first
#define ss second
#define ll long long
using namespace std;
const int max_N = 1 << 17;
int N, bas, g;
int d[max_N];
ll sonuc = 0;
pair<ll, int> st[max_N << 1];
int id[max_N], sira[max_N];
void ekle (int k) {
st[k + max_N] = mp(d[sira[k]], 1);
k += max_N;
while(k > 1){
k >>= 1;
st[k] = mp(st[k+k].ff + st[k+k+1].ff, st[k+k].ss + st[k+k+1].ss);
}
}
void sil (int k) {
st[k + max_N] = mp(0, 0);
k += max_N;
while (k > 1) {
k >>= 1;
st[k] = mp(st[k+k].ff + st[k+k+1].ff, st[k+k].ss + st[k+k+1].ss);
}
}
ll get (int k) {
int x = 1;
ll sonuc = 0;
// printf("N: %d\n", N);
while (x < max_N) {
// printf("x: %d\n", x);
if (st[x+x+1].ss >= k)
x = x+x+1;
else {
k -= st[x+x+1].ss;
sonuc += st[x+x+1].ff;
x = x+x;
}
}
sonuc += (k > 0) * st[x].ff;
return sonuc;
}
int bsol, bsag;
void fix (int sol, int sag) {
while (bsag < sag) ekle (id[++bsag]);
while (bsol > sol) ekle (id[--bsol]);
while (bsag > sag) sil (id[bsag--]);
while (bsol < sol) sil (id[bsol++]);
}
void coz (int sol, int sag, int oSol, int oSag) {
//printf("Sol: %d, sag: %d, oSol: %d, oSag: %d\n", sol, sag, oSol, oSag);
if (sol > sag) return;
int o = (sol + sag) >> 1;
ll eb = 0;
ll opt = -1;
for (int i = oSol; i <= o; i++) {
fix(i, o);
ll s = get(g - (o - i) - (o - bas));
// printf("i: %d, o: %d, s: %d\n", i, o, s);
if (opt == -1 || s >= eb) {
sonuc = max(sonuc, s);
eb = s;
opt = i;
}
}
coz (sol, o-1, oSol, opt);
coz (o+1, sag, opt, oSag);
}
bool cmp(int x, int y) {return d[x] < d[y];}
void cozBas() {
for (int i = 1; i <= N; i++)
sira[i] = i;
sort (sira +1, sira + N +1, cmp);
for (int i = 1; i <= N; i++)
id[sira[i]] = i;
bsol = 1;
bsag = 0;
memset(st, 0, sizeof st);
for (int i = bas; i <= N; i++) {
ekle(id[i]);
sonuc = max(sonuc, get(g- (i - bas)) );
}
memset(st, 0, sizeof st);
coz(bas, N, 1, bas);
}
long long int findMaxAttraction(int n, int start, int D, int attraction[]) {
N = n;
bas = start + 1;
g = D;
for (int i = 0; i < n; i++)
d[i+1] = attraction[i];
cozBas();
reverse(d +1, d + n +1);
bas = n - bas +1;
cozBas();
return sonuc;
}