이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; typedef pair<int,int> pii;
#define ff first
#define ss second
#define pb emplace_back
#define AI(x) begin(x),end(x)
#ifdef OWO
#define debug(args...) SDF(#args, args)
#define OIU(args...) ostream& operator<<(ostream&O,args)
#define LKJ(S,B,E,F) template<class...T>OIU(S<T...>s){O<<B;int c=0;for(auto i:s)O<<(c++?", ":"")<<F;return O<<E;}
LKJ(vector,'[',']',i)LKJ(deque,'[',']',i)LKJ(set,'{','}',i)LKJ(multiset,'{','}',i)LKJ(unordered_set,'{','}',i)LKJ(map,'{','}',i.ff<<':'<<i.ss)LKJ(unordered_map,'{','}',i.ff<<':'<<i.ss)
template<class...T>void SDF(const char* s,T...a){int c=sizeof...(T);if(!c){cerr<<"\033[1;32mvoid\033[0m\n";return;}(cerr<<"\033[1;32m("<<s<<") = (",...,(cerr<<a<<(--c?", ":")\033[0m\n")));}
template<class T,size_t N>OIU(array<T,N>a){return O<<vector<T>(AI(a));}template<class...T>OIU(pair<T...>p){return O<<'('<<p.ff<<','<<p.ss<<')';}template<class...T>OIU(tuple<T...>t){return O<<'(',apply([&O](T...s){int c=0;(...,(O<<(c++?", ":"")<<s));},t),O<<')';}
#else
#define debug(...) ((void)0)
#endif
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
int N, M; cin >> N >> M;
vector<pair<int, int>> cakes(N + 1);
for (int i = 1; i <= N; ++i)
cin >> cakes[i].second >> cakes[i].first;
sort(begin(cakes) + 1, end(cakes));
int64_t ans = LLONG_MIN;
int64_t sum = 0;
priority_queue<int, vector<int>, greater<int>> pq;
auto add = [&](const int& v) -> void {
pq.emplace(v);
sum += v;
if (pq.size() > M) {
sum -= pq.top();
pq.pop();
}
};
auto solve = [&](const auto& solve, int l, int r, int sl, int sr) -> void {
r = min(r, sr - M + 1);
if (l > r) return;
int m = (l + r) / 2;
int bests = -1;
int64_t bestv = LLONG_MIN;
for (int i = m; i <= sr; ++i) {
add(cakes[i].second);
int64_t val = sum - 2ll * (cakes[i].first - cakes[m].first);
if (pq.size() == M and bestv < val) {
bests = i;
bestv = val;
}
}
assert(bests != -1);
ans = max(ans, bestv);
while (!pq.empty()) { sum -= pq.top(); pq.pop(); }
if (l <= m - 1) solve(solve, l, m - 1, sl, bests);
if (m + 1 <= r) solve(solve, m + 1, r, bests, sr);
};
solve(solve, 1, N - M + 1, M, N);
cout << ans << '\n';
return 0;
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In lambda function:
cake3.cpp:37:17: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
37 | if (pq.size() > M) {
| ~~~~~~~~~~^~~
cake3.cpp: In instantiation of 'main()::<lambda(const auto:23&, int, int, int, int)> [with auto:23 = main()::<lambda(const auto:23&, int, int, int, int)>]':
cake3.cpp:64:33: required from here
cake3.cpp:52:18: warning: comparison of integer expressions of different signedness: 'std::priority_queue<int, std::vector<int>, std::greater<int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
52 | if (pq.size() == M and bestv < val) {
| ~~~~~~~~~~^~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |