제출 #336995

#제출 시각아이디문제언어결과실행 시간메모리
336995alishahali1382Cake 3 (JOI19_cake3)C++14
24 / 100
4056 ms10604 KiB
#include <bits/stdc++.h>
#pragma GCC optimize ("O2,unroll-loops")
//#pragma GCC optimize("no-stack-protector,fast-math")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")

using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef pair<ll, ll> pll;
#define debug(x) cerr<<#x<<'='<<(x)<<endl;
#define debugp(x) cerr<<#x<<"= {"<<(x.first)<<", "<<(x.second)<<"}"<<endl;
#define debug2(x, y) cerr<<"{"<<#x<<", "<<#y<<"} = {"<<(x)<<", "<<(y)<<"}"<<endl;
#define debugv(v) {cerr<<#v<<" : ";for (auto x:v) cerr<<x<<' ';cerr<<endl;}
#define all(x) x.begin(), x.end()
#define pb push_back
#define kill(x) return cout<<x<<'\n', 0;

const int inf=1000000010;
const ll INF=10000000000000010LL;
const int mod=1000000007;
const int MAXN=200010, LOG=20;

ll ans=-INF;
int n, m, k, u, v, x, y, t, a, b, shit;
pii A[MAXN];

struct Set{
	multiset<int> A, B;
	ll sum=0;
	inline void add(int x){
		assert(shit++<20000000);
		A.insert(x);
		sum+=x;
		if (A.size()>k){
			auto it=A.begin();
			B.insert(*it);
			sum-=*it;
			A.erase(it);
		}
	}
	inline void rem(int x){
		assert(shit++<20000000);
		auto it=B.find(x);
		if (it!=B.end()) B.erase(it);
		else{
			it=A.find(x);
			sum-=*it;
			A.erase(it);
			if (B.size()){
				it=B.end();
				it--;
				sum+=*it;
				A.insert(*it);
				B.erase(it);
			}
		}
	}
} S;
inline void Move(int L, int R, int l, int r){
	while (l<L) S.add(A[--L].second);
	while (R<r) S.add(A[R++].second);
	while (L<l) S.rem(A[L++].second);
	while (r<R) S.rem(A[--R].second);
}
void divide(int tl, int tr, int optl, int optr){
	if (tr<tl) return ;
	int mid=(tl+tr)>>1, opt=-1;
	ll val=-INF;
	for (int i=tl; i<=mid; i++) S.add(A[i].second);
	for (int i=optl; i<=min(mid-k+1, optr); i++){
		ll c=S.sum - 2*(A[mid].first-A[i].first);
		if (c>val){
			val=c;
			opt=i;
		}
		S.rem(A[i].second);
	}
	ans=max(ans, val);
	for (int i=optl; i<=min(mid-k+1, optr); i++) S.add(A[i].second);
	Move(optl, mid+1, opt, mid+1);
	divide(mid+1, tr, opt, optr);
	Move(opt, mid+1, optl, tl);
	divide(tl, mid-1, optl, opt);
}

int main(){
	ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	cin>>n>>k;
	for (int i=1; i<=n; i++) cin>>A[i].second>>A[i].first;
	sort(A+1, A+n+1);
	for (int i=1; i<k; i++) S.add(A[i].second);
	divide(k, n, 1, n-k+1);
	cout<<ans<<"\n";
	
	return 0;
}
/*
5 3
2 1
4 2
6 4
8 8
10 16



*/

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

cake3.cpp: In member function 'void Set::add(int)':
cake3.cpp:36:15: warning: comparison of integer expressions of different signedness: 'std::multiset<int>::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   36 |   if (A.size()>k){
      |       ~~~~~~~~^~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...