이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include"holiday.h"
#include <bits/stdc++.h>
#define F first
#define S second
#define V vector
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(v) int((v).size())
#define ALL(v) (v).begin(), (v).end()
using namespace std;
typedef long long ll;
typedef pair<int, int> pi;
typedef V<int> vi;
const int INF = 1e9 + 7, C = 1e5 + 7;
ll go(int n, int s, int d, int a[]) {
priority_queue<int, vi, greater<int>> pq;
ll ans = 0, sum = 0;
int cost = 0;
for(int i = s; i < n; i++) {
pq.push(a[i]);
sum += a[i];
while(SZ(pq) + cost > d) {
if(pq.empty()) return ans;
sum -= pq.top();
pq.pop();
}
ans = max(ans, sum);
cost++;
}
return ans;
}
struct DS {
static const int MX = 2e6;
int l[MX], r[MX], cnt[MX], sz;
ll sum[MX];
vi val;
int upd(int t, int pos, int tl, int tr) {
int nw = sz++;
if(tr - tl == 1) {
cnt[nw] = cnt[t] + 1;
sum[nw] = sum[t] + val[tl];
return nw;
}
l[nw] = l[t], r[nw] = r[t];
int tm = (tl + tr) / 2;
if(pos < tm) l[nw] = upd(l[t], pos, tl, tm);
else r[nw] = upd(r[t], pos, tm, tr);
cnt[nw] = cnt[l[nw]] + cnt[r[nw]];
sum[nw] = sum[l[nw]] + sum[r[nw]];
return nw;
}
ll qqry(int t, int need, int tl, int tr) {
if(need == 0) return 0LL;
assert(cnt[t] >= need);
if(tr - tl == 1)
return 1LL * val[tl] * need;
int tm = (tl + tr) / 2;
if(cnt[r[t]] >= need)
return qqry(r[t], need, tm, tr);
else
return qqry(l[t], need - cnt[r[t]], tl, tm) + sum[r[t]];
}
vi a;
V<pi> stk;
int c, n;
vi h;
ll test(int d, int i) {
if(i * c > d) return -1;
d -= i * c;
d = min(d, i + 1);
return qqry(h[i], d, 0, SZ(val));
}
DS(vi _a, int _c) {
memset(l, 0, sizeof l);
memset(r, 0, sizeof r);
memset(cnt, 0, sizeof cnt);
memset(sum, 0, sizeof sum);
a = _a;
c = _c;
n = SZ(a);
sz = 1;
val = a;
sort(ALL(val));
val.resize(unique(ALL(val)) - val.begin());
int t = 0;
for(int i = 0; i < n; i++) {
int tt = lower_bound(ALL(val), a[i]) - val.begin();
t = upd(t, tt, 0, SZ(val));
h.PB(t);
}
for(int i = 0; i < n; i++) {
while(SZ(stk) && test(stk.back().F, stk.back().S) <= test(stk.back().F, i))
stk.pop_back();
if(stk.empty())
stk.EB(i * c, i);
else {
int lb = stk.back().F + 1, rb = INF;
// find first x s.t. test(x, stk.back().S) <= test(x, i)
while(lb <= rb) {
int mb = (lb + rb) / 2;
if(test(mb, stk.back().S) <= test(mb, i))
rb = mb - 1;
else
lb = mb + 1;
}
stk.EB(lb, i);
}
}
}
ll qry(int d) {
int it = upper_bound(ALL(stk), pi(d, INF)) - stk.begin() - 1;
return test(d, stk[it].S);
}
};
ll solve(int n, int s, int d, int a[]) { // go left in the first step
if(s == 0) return 0;
vi pl, pr;
for(int i = s - 1; i >= 0; i--) pl.PB(a[i]);
for(int i = s; i < n; i++) pr.PB(a[i]);
V<ll> aux(d);
{
DS dsl = DS(pl, 2);
for(int i = 0; i + 2 <= d; i++)
aux[i] += dsl.qry(i);
}
{
DS dsr = DS(pr, 1);
for(int i = 0; i + 2 <= d; i++)
aux[i] += dsr.qry(d - i - 2);
}
ll ans = 0;
for(int i = 0; i + 2 <= d; i++)
ans = max(ans, aux[i]);
return ans;
}
ll findMaxAttraction(int n, int s, int d, int a[]) {
ll ans = go(n, s, d, a);
ans = max(ans, solve(n, s, d, a));
reverse(a, a + n);
s = n - s - 1;
ans = max(ans, go(n, s, d, a));
ans = max(ans, solve(n, s, d, a));
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... |