이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pi;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
#define pb push_back
#define f first
#define s second
const int MAXN = 2e5 + 100;
const int LOGN = 20;
const ll INF = 1e15;
int N, K;
ll v[MAXN], c[MAXN];
ll bTotal = -INF;
set<pl> sets[LOGN];
set<pl> ext[LOGN];
pi sRange[LOGN];
ll sSum[LOGN];
//struct lazyQueue{
// int
//};
void reRange(int setI, pi nRange) {
// ps(setI, nRange);
set<pl> &cSet = sets[setI];
set<pl> &cExt = ext[setI];
pi &cRange = sRange[setI];
ll &cSum = sSum[setI];
// assert(cRange.f <= nRange.f && cRange.s <= nRange.s);
// ps(cRange, nRange);
while (cRange.s < nRange.s) {
cExt.insert({v[cRange.s + 1], cRange.s + 1});
cRange.s++;
}
while (cRange.f < nRange.f) {
if (cSet.erase({v[cRange.f], cRange.f})) {
cSum -= v[cRange.f];
} else {
cExt.erase({v[cRange.f], cRange.f});
}
cRange.f++;
}
while (cSet.size() < K && cExt.size()) {
cSum += (--cExt.end())->f;
cSet.insert(*(--cExt.end()));
cExt.erase(--cExt.end());
}
while (cExt.size() && cSet.begin()->f < (--cExt.end())->f) {
cSum -= cSet.begin()->f;
cSum += (--cExt.end())->f;
cExt.insert(*cSet.begin());
cSet.erase(cSet.begin());
cSet.insert(*(--cExt.end()));
cExt.erase(--cExt.end());
}
// assert(cRange == nRange);
// assert(cSet.size() == min(K, cRange.s - cRange.f + 1));
}
ll getTotal(int setI) {
set<pl> &cSet = sets[setI];
// assert(cSet.size() <= K);
if (cSet.size() == K) {
return sSum[setI];
} else {
return -INF;
}
}
int call = 0;
void compute(int level, int l1, int l2, int r1, int r2) {
int cL = (l1 + l2) / 2;
// cout << call++ << endl;
ll maxTotal = -INF;
int maxR = -1;
// assert(0 <= l1 && 0 <= r1);
for (int cR = max(cL + K - 1, r1); cR <= r2; cR++) {
reRange(level, {cL, cR});
ll cTotal = getTotal(level) + c[cL] - c[cR];
if (cTotal > maxTotal) {
maxTotal = cTotal;
maxR = cR;
}
}
// assert(maxR != -1);
bTotal = max(bTotal, maxTotal);
if (l1 < cL) {
compute(level + 1, l1, cL - 1, r1, maxR);
}
if (cL < l2) {
compute(level + 1, cL + 1, l2, maxR, r2);
}
}
pi items[MAXN];
int main() {
// freopen("cake3.in", "r", stdin);
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin >> N >> K;
// cout << "start" << endl;
for (int i = 0; i < N; i++) {
int x, y;
cin >> x >> y;
items[i] = {y, x};
}
sort(items, items + N);
for (int i = 0; i < N; i++) {
v[i] = items[i].s;
c[i] = items[i].f;
c[i] *= 2;
}
fill(sSum, sSum + LOGN, 0);
fill(sRange, sRange + LOGN, make_pair(0, -1));
// cout << "computing" << endl;
compute(0, 0, N - K, 0, N - 1);
cout << bTotal << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'void reRange(int, pi)':
cake3.cpp:60:24: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (cSet.size() < K && cExt.size()) {
~~~~~~~~~~~~^~~
cake3.cpp: In function 'll getTotal(int)':
cake3.cpp:85:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (cSet.size() == K) {
~~~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |