이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
const long long INF = 1e18;
struct cake{
int v, c;
bool operator < (const cake &aux)const{
if(c != aux.c) return c < aux.c;
return v > aux.v;
}
};
cake a[200005];
int n, m;
multiset <long long> s1, s2;
long long S = 0;
void add(int i){
if(s2.size() < m){
s2.insert(a[i].v);
S += a[i].v;
}
else{
if(*s2.begin() < a[i].v){
s1.insert(*s2.begin());
S -= *s2.begin();
s2.erase(s2.begin());
s2.insert(a[i].v);
S += a[i].v;
}
else s1.insert(a[i].v);
}
}
void rem(int i){
if(s2.find(a[i].v) != s2.end()){
s2.erase(s2.find(a[i].v));
S -= a[i].v;
if(!s1.empty()){
s2.insert(*s1.rbegin());
S += *s1.rbegin();
s1.erase(s1.find(*s1.rbegin()));
}
}
else s1.erase(s1.find(a[i].v));
}
int l = 1, r = 0;
long long calc(int st, int dr){
while(r < dr) add(++r);
while(l > st) add(--l);
while(l < st) rem(l++);
while(r > dr) rem(r--);
if(dr - st + 1 < m) return -INF;
return S;
}
long long solve(int st, int dr, int down, int up){
int mij = (st + dr) / 2;
long long sol = -INF, best = 0;
for(int i = max(down, mij); i <= up ; ++i){
long long val = calc(mij, i) - (a[i].c - a[mij].c);
if(val > sol){
sol = val;
best = i;
}
}
if(st == dr || sol == -INF) return sol;
return max(sol, max(solve(st, mij, down, best), solve(mij + 1, dr, best, up)));
}
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;
}
sort(a + 1, a + n + 1);
printf("%lld", solve(1, n - m + 1, m, n));
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In function 'void add(int)':
cake3.cpp:20:18: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(s2.size() < m){
~~~~~~~~~~^~~
cake3.cpp: In function 'int main()':
cake3.cpp:80: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:83: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... |