이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct cake{
int pv, v, c;
bool operator < (const cake &aux)const{
if(c != aux.c) return c < aux.c;
return v < aux.v;
}
};
cake a[200005], v[200005];
bool cmp(cake x, cake y){
if(x.v != y.v) return x.v < y.v;
return x.c < y.c;
}
int n, m;
long long Arb[800005];
int cnt[800005];
void update(int p, int v, int st = 1, int dr = n, int nod = 1){
if(st == dr){
Arb[nod] += v;
if(v > 0) ++cnt[nod];
else --cnt[nod];
return ;
}
int mij = (st + dr) / 2;
if(p <= mij) update(p, v, st, mij, nod * 2);
else update(p, v, mij + 1, dr, nod * 2 + 1);
Arb[nod] = Arb[nod * 2] + Arb[nod * 2 + 1];
cnt[nod] = cnt[nod * 2] + cnt[nod * 2 + 1];
}
long long query(int k, int st = 1, int dr = n, int nod = 1){
if(k == 0) return 0;
if(cnt[nod] <= k) return Arb[nod];
int mij = (st + dr) / 2;
if(cnt[nod * 2 + 1] >= k) return query(k, mij + 1, dr, nod * 2 + 1);
return Arb[nod * 2 + 1] + query(k - cnt[nod * 2 + 1], st, mij, nod * 2);
}
int l, r;
long long calc(int st, int dr){
if(st > dr) return -INF;
while(r < dr) ++r, update(a[r].pv, a[r].v);
while(l > st) --l, update(a[l].pv, a[l].v);
while(l < st) update(a[l].pv, -a[l].v), ++l;
while(r > dr) update(a[r].pv, -a[r].v), --r;
if(dr - st + 1 < m - 2) return -INF;
return query(m - 2);
}
long long ans = -INF;
void solve(int st, int dr, int down, int up){
if(st > dr) return ;
int mij = (st + dr) / 2;
long long sol = -INF, best = 0;
for(int i = max(down, mij + m - 1); i <= up ; ++i){
long long val = calc(mij + 1, i - 1) - (a[i].c - a[mij].c) + a[i].v + a[mij].v;
if(val > sol){
sol = val;
best = i;
}
}
ans = max(ans, sol);
if(st == dr) return ;
solve(mij + 1, dr, best, up);
solve(st, mij - 1, down, best);
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n ; ++i){
scanf("%d%d", &a[i].v, &a[i].c);
a[i].c *= 2;
v[i] = a[i];
}
sort(a + 1, a + n + 1);
sort(v + 1, v + n + 1, cmp);
for(int i = 1; i <= n ; ++i){
int st = 1, dr = n;
while(st <= dr){
int mij = (st + dr) / 2;
if(v[mij].v < a[i].v || (v[mij].v == a[i].v && v[mij].c <= a[i].c)) st = mij + 1;
else dr = mij - 1;
}
a[i].pv = dr;
}
solve(1, n - m + 1, m, n);
printf("%lld", ans);
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'int main()':
cake3.cpp:87:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &n, &m);
~~~~~^~~~~~~~~~~~~~~~
cake3.cpp:89:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &a[i].v, &a[i].c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |