제출 #1083882

#제출 시각아이디문제언어결과실행 시간메모리
1083882hahahahaCake 3 (JOI19_cake3)C++17
100 / 100
3133 ms23176 KiB
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const ll INF=1e18+7;
const int LIM=2e5+7;
pair<ll,ll>T[LIM], dp[LIM];
multiset<ll>S;
vector<pair<ll,ll>>rollback;
ll akt, n, m;
void upd(int x) {
	S.insert(T[x].nd);
	akt+=T[x].nd;
	if(S.size()>m-2) {
		rollback.pb({T[x].nd, *S.begin()});
		akt-=*S.begin();
		S.erase(S.begin());
	} else rollback.pb({T[x].nd, -1});
}
void undo() {
	if(rollback.back().nd!=-1) {
		akt+=rollback.back().nd;
		S.insert(rollback.back().nd);
	}
	akt-=rollback.back().st;
	S.erase(S.find(rollback.back().st));
	rollback.pop_back();
}
void solve(int l, int r, int a, int b) {
	if(l>r) return;
	int mid=(l+r)/2;
	for(int i=mid+1; i<=min(r, a-1); ++i) upd(i);
	for(int i=max(mid+1, a); i<=b; ++i) {
		if(S.size()==m-2) dp[mid]=max(dp[mid], {akt+T[i].nd+T[mid].nd-2*T[i].st+2*T[mid].st, i});
		upd(i);
	}
	for(int i=b; i>=max(mid+1, a); --i) undo();
	if(mid<a) upd(mid);
	solve(l, mid-1, a, dp[mid].nd);
	if(mid<a) undo();
	for(int i=min(r, a-1); i>=mid+1; --i) undo();
	for(int i=max(r+1, a); i<dp[mid].nd; ++i) upd(i);
	solve(mid+1, r, dp[mid].nd, b);
	for(int i=dp[mid].nd-1; i>=max(r+1, a); --i) undo();
}
int main() {
	ios_base::sync_with_stdio(0); cin.tie(0);
	cin >> n >> m;
	rep(i, n) dp[i]={-INF, -1};
	rep(i, n) cin >> T[i].nd >> T[i].st;
	sort(T, T+n);
	solve(0, n-m, 0, n-1);
	ll ans=-INF;
	rep(i, n-m+1) ans=max(ans, dp[i].st);
	cout << ans << '\n';
}

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

cake3.cpp: In function 'void upd(int)':
cake3.cpp:18:13: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   18 |  if(S.size()>m-2) {
      |     ~~~~~~~~^~~~
cake3.cpp: In function 'void solve(int, int, int, int)':
cake3.cpp:38:14: warning: comparison of integer expressions of different signedness: 'std::multiset<long long int>::size_type' {aka 'long unsigned int'} and 'll' {aka 'long long int'} [-Wsign-compare]
   38 |   if(S.size()==m-2) dp[mid]=max(dp[mid], {akt+T[i].nd+T[mid].nd-2*T[i].st+2*T[mid].st, i});
      |      ~~~~~~~~^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...