QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#520849 | #7605. Yet Another Mex Problem | liuziao | WA | 5ms | 45256kb | C++14 | 4.1kb | 2024-08-15 16:31:47 | 2024-08-15 16:31:48 |
Judging History
answer
#include <bits/stdc++.h>
#define int int64_t
using pii = std::pair<int, int>;
const int kMaxN = 2e5 + 5;
int n, k;
int a[kMaxN], sum[kMaxN], f[kMaxN];
pii sub(pii a, pii b) { return {a.first - b.first, a.second - b.second}; }
int mul(pii a, pii b) { return a.first * b.second - a.second * b.first; }
int func(pii a, int k) { return a.first * k + a.second; }
struct SGT1 {
int mi[kMaxN * 4];
void pushup(int x) { mi[x] = std::min(mi[x << 1], mi[x << 1 | 1]); }
void update(int x, int l, int r, int ql, int v) {
if (l == r) return void(mi[x] = v);
int mid = (l + r) >> 1;
if (ql <= mid)
update(x << 1, l, mid, ql, v);
else
update(x << 1 | 1, mid + 1, r, ql, v);
pushup(x);
}
int getmex(int x, int l, int r, int ql) {
assert(mi[x] < ql);
if (l == r) return l;
int mid = (l + r) >> 1;
if (mi[x << 1] < ql)
return getmex(x << 1, l, mid, ql);
else
return getmex(x << 1 | 1, mid + 1, r, ql);
}
int query(int x, int l, int r, int ql, int qr) {
if (l > qr || r < ql)
return 1e9;
else if (l >= ql && r <= qr)
return mi[x];
int mid = (l + r) >> 1;
return std::min(query(x << 1, l, mid, ql, qr),
query(x << 1 | 1, mid + 1, r, ql, qr));
}
} sgt1;
struct SGT2 { // f[l] - mex * sum[l]
// (sum[l], f[l]) 上凸壳
std::vector<pii> v[kMaxN * 4];
void ins(int x, pii p) {
// if (!v[x].empty() && p <= v[x].back()) return;
// for (;
// v[x].size() >= 2 &&
// mul(sub(p, v[x].back()), sub(v[x].back(), v[x][v[x].size() - 2])) <= 0;
// v[x].pop_back()) {
// }
v[x].emplace_back(p);
}
int getmax(int x, int k) {
if (!v[x].size()) return -1e18;
// int L = 0, R = v[x].size(), res = 0;
// while (L + 1 < R) {
// int mid = (L + R) >> 1;
// if (func(v[x][mid], k) >= func(v[x][mid - 1], k))
// L = res = mid;
// else
// R = mid;
// }
// return func(v[x][res], k);
int ret = -1e18;
for (auto p : v[x]) ret = std::max(ret, func(p, k));
return ret;
}
void update(int x, int l, int r, int ql, pii p) {
ins(x, p);
if (l == r) return;
int mid = (l + r) >> 1;
if (ql <= mid)
update(x << 1, l, mid, ql, p);
else
update(x << 1 | 1, mid + 1, r, ql, p);
}
int query(int x, int l, int r, int ql, int qr, int k) {
if (l > qr || r < ql)
return -1e18;
else if (l >= ql && r <= qr)
return getmax(x, k);
int mid = (l + r) >> 1;
return std::max(query(x << 1, l, mid, ql, qr, k),
query(x << 1 | 1, mid + 1, r, ql, qr, k));
}
} sgt2, sgt3;
void dickdreamer() {
std::cin >> n >> k;
for (int i = 1; i <= n; ++i) {
std::cin >> a[i];
sum[i] = sum[i - 1] + a[i];
}
sgt2.update(1, 0, n, 0, {0, 0}), sgt3.update(1, 0, n, 0, {0, 0});
for (int i = 1; i <= n; ++i) {
int l = sgt1.query(1, 0, n, 0, a[i]) + 1,
r = (a[i] ? sgt1.query(1, 0, n, 0, a[i] - 1) : i);
sgt1.update(1, 0, n, a[i], i);
// std::cerr << i << " : ---------\n";
for (; l <= r;) {
int mex = sgt1.getmex(1, 0, n, r), t = sgt1.query(1, 0, n, 0, mex);
int val = sgt2.query(1, 0, n, t, r - 1, -mex);
sgt3.update(1, 0, n, t, {mex, val});
r = t;
}
l = std::max<int>(i - k + 1, 1);
int mex = sgt1.getmex(1, 0, n, l);
r = (mex ? sgt1.query(1, 0, n, 0, mex - 1) : i);
assert(l <= r);
f[i] = std::max(sgt3.query(1, 0, n, l - 1, i - 1, sum[i]),
sgt2.query(1, 0, n, l - 1, r - 1, -mex) + sum[i] * mex);
sgt2.update(1, 0, n, i, {sum[i], f[i]});
sgt3.update(1, 0, n, i, {!a[i], f[i]});
// std::cerr << i << ' ' << f[i] << '\n';
}
std::cout << f[n] << '\n';
}
int32_t main() {
#ifdef ORZXKR
freopen("in.txt", "r", stdin);
freopen("out.txt", "w", stdout);
#endif
std::ios::sync_with_stdio(0), std::cin.tie(0), std::cout.tie(0);
int T = 1;
// std::cin >> T;
while (T--) dickdreamer();
// std::cerr << 1.0 * clock() / CLOCKS_PER_SEC << "s\n";
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 5ms
memory: 45256kb
input:
5 3 3 4 0 0 3
output:
24
result:
wrong answer 1st numbers differ - expected: '10', found: '24'