제출 #900008

#제출 시각아이디문제언어결과실행 시간메모리
900008DEQKCake 3 (JOI19_cake3)C++17
24 / 100
4009 ms15012 KiB
#include <bits/stdc++.h> using namespace std; #define ll long long #define pb push_back #define sz(a) ((int) a.size()) #define F first #define S second const int N = 3e5 + 15; int n, K; pair<int, int> a[N]; ll ans = -1e18; int L = 1, R = 0; ll cur = 0; multiset<pair<int, int>> sm, bg; void add(int p) { if(sz(bg) < K) { cur += a[p].S; bg.insert({a[p].S, p}); } else { assert(sz(bg) == K); if(a[p].S > (*(bg.begin())).F) { cur -= (*bg.begin()).F; sm.insert(*bg.begin()); bg.erase(bg.begin()); bg.insert({a[p].S, p}); cur += a[p].S; } else { sm.insert({a[p].S, p}); } } } void del(int p) { if(bg.find({a[p].S, p}) != bg.end()) { cur -= a[p].S; bg.erase(bg.find({a[p].S, p})); if(sz(sm)) { auto it = sm.end(); it--; cur += (*it).F; bg.insert(*it); sm.erase(it); } } else { assert(sm.find({a[p].S, p}) != sm.end()); sm.erase(sm.find({a[p].S, p})); } } void dnc(int l, int r, int tl, int tr) { if(l > r) { return; } int m = (l + r) / 2; int pt = tl; ll mx = -1e18; //cout << l << ' ' << r << ' ' << tl << ' ' << tr << '\n'; for(int k = max(m, tl); k <= tr; k++) { while(R < k) { add(++R); } while(L > m) { add(--L); } while(R > k) { del(R--); } while(L < m) { del(L++); } if(sz(bg) == K && mx < cur - 2 * (a[k].F - a[m].F)) { mx = cur - 2 * (a[k].F - a[m].F); pt = k; } } ans = max(ans, mx); dnc(l, m - 1, tl, tr); dnc(m + 1, r, tl, tr); } int main() { ios::sync_with_stdio(0); cin.tie(0); cin >> n >> K; for(int i = 1; i <= n; i++) { cin >> a[i].S; cin >> a[i].F; } sort(a + 1, a + n + 1); dnc(1, n, 1, n); cout << ans << '\n'; }

컴파일 시 표준 에러 (stderr) 메시지

cake3.cpp: In function 'void dnc(int, int, int, int)':
cake3.cpp:53:6: warning: variable 'pt' set but not used [-Wunused-but-set-variable]
   53 |  int pt = tl;
      |      ^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...