이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#ifdef local
#define debug(a...) qqbx(#a, a)
template <typename ...T> void qqbx(const char *s, T ...a) {
int cnt = sizeof...(T);
((std::cerr << "\033[1;32m(" << s << ") = (") , ... , (std::cerr << a << (--cnt ? ", " : "\033[0m)\n")));
}
#define safe std::cerr<<__PRETTY_FUNCTION__<<" line "<<__LINE__<<" safe\n"
#else
#define debug(...) ((void)0)
#define safe ((void)0)
#endif // local
#define all(v) begin(v),end(v)
#define pb emplace_back
#define sort_uni(v) sort(all(v)), v.erase(unique(all(v)), v.end())
using namespace std;
using ll = int64_t;
template <typename T> using min_heap = priority_queue<T, vector<T>, greater<T>>;
const ll INF = 1e18;
const int maxn = 500025, inf = 1e9;
pair<int,int> v[maxn];
struct Event {
int l, r, fl, fr;
};
struct kthMaxsum {
multiset<int> ms;
void insert(int x) {
ms.insert(x);
}
void erase(int x) {
ms.erase(ms.find(x));
}
ll query(int k) {
if (ms.size() < k) return -INF;
ll sum = 0;
auto it = ms.end();
for (int i = 0; i < k; i++)
sum += *--it;
return sum;
}
} ds;
int n, k;
void update(int l, int r) {
debug(l, r);
static int curL = 1, curR = 0;
while (curR < r) ds.insert(v[++curR].second);
while (curL > l) ds.insert(v[--curL].second);
while (curR > r) ds.erase(v[curR--].second);
while (curL < l) ds.erase(v[curL++].second);
}
ll res = -INF;
void solve(int l, int r, int fl, int fr) {
if (l == r) return;
int m = (l+r)/2;
ll ans = -INF;
int pos = -1;
for (int i = fl; i < fr && i < m; i++) {
debug(i, m);
update(i+1, m-1);
ll cur = ds.query(k) + v[i].second + v[m].second - abs(v[i].first - v[m].first) * 2;
if (ans < cur) {
ans = cur;
pos = i;
}
}
res = max(res, ans);
solve(l, m, fl, pos+1);
solve(m+1, r, pos, fr);
}
signed main() {
ios_base::sync_with_stdio(0), cin.tie(0);
cin >> n >> k;
for (int i = 0; i < n; i++) cin >> v[i].second >> v[i].first;
sort(v, v+n);
k -= 2;
/*
ll ans = -INF;
for (int i = 0; i < n; i++) {
priority_queue<int, vector<int>, greater<int>> pq;
ll sum = 0;
for (int j = i+1; j < n; j++) {
if (pq.size() == k)
ans = max(ans, sum + v[i].second + v[j].second - (v[j].first - v[i].first) * 2);
sum += v[j].second, pq.push(v[j].second);
if (pq.size() > k) sum -= pq.top(), pq.pop();
}
}
cout << ans << '\n';
*/
solve(0, n, 0, n);
cout << res << '\n';
}
컴파일 시 표준 에러 (stderr) 메시지
cake3.cpp: In member function 'll kthMaxsum::query(int)':
cake3.cpp:38:23: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
38 | if (ms.size() < k) return -INF;
| ~~~~~~~~~~^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |