Submission #376674

#TimeUsernameProblemLanguageResultExecution timeMemory
376674fhvirusCake 3 (JOI19_cake3)C++17
24 / 100
4051 ms8468 KiB
// Knapsack DP is harder than FFT.
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii; typedef pair<ll,ll> pll;
#define ff first
#define ss second
#define pb emplace_back
#define FOR(i,n) for(int i = 0; i < (n); ++i)
#define FOO(i,a,b) for(int i = (a); i <= (b); ++i)
#define AI(x) begin(x),end(x)
template<class I> bool chmax(I &a, I b){ return a < b ? (a = b, true) : false;}
template<class I> bool chmin(I &a, I b){ return a > b ? (a = b, true) : false;}
#ifdef OWO
#define debug(args...) LKJ("[ " + string(#args) + " ]", args)
void LKJ(){ cerr<<endl;}
template<class I, class...T> void LKJ(I&&x, T&&...t){ cerr<<x<<", ", LKJ(t...);}
template<class I> void DE(I a, I b){ while(a < b) cerr<<*a<<" \n"[next(a) == b], ++a;}
#else
#define debug(...) 0
#define DE(...) 0
#endif
const int N = 2e5 + 225;
int n, m;
struct cake{
	ll v, c;
	cake(ll v = 0, ll c = 0):
		v(v), c(c){}
	bool operator<(const cake &a){
		return c < a.c;
	}
} C[N];
ll ans = -1e18;
typedef priority_queue<ll, vector<ll>, greater<ll>> minheap;

int32_t main(){
	ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	cin >> n >> m;
	FOR(i,n){
		ll v, c;
		cin >> v >> c;
		C[i] = cake(v, c);
	}
	sort(C, C + n);
	FOR(i,n){
		//if(i + m > n) break;
		ll sum = C[i].v;
		minheap pq;
		for(int j = i + 1; j < n; ++j){
			while(pq.size() >= m - 1){
				sum -= pq.top();
				pq.pop();
			}
			pq.push(C[j].v);
			sum += C[j].v;
			if(pq.size() == m - 1){
				chmax(ans, sum - 2 * abs(C[j].c - C[i].c));
				debug(i, j, sum, sum - 2 * abs(C[j].c - C[i].c));
			}
		}
	}
	cout << ans;
	return 0;
}

Compilation message (stderr)

cake3.cpp: In function 'int32_t main()':
cake3.cpp:50:20: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   50 |    while(pq.size() >= m - 1){
      |          ~~~~~~~~~~^~~~~~~~
cake3.cpp:56:17: warning: comparison of integer expressions of different signedness: 'std::priority_queue<long long int, std::vector<long long int>, std::greater<long long int> >::size_type' {aka 'long unsigned int'} and 'int' [-Wsign-compare]
   56 |    if(pq.size() == m - 1){
      |       ~~~~~~~~~~^~~~~~~~
cake3.cpp:20:20: warning: statement has no effect [-Wunused-value]
   20 | #define debug(...) 0
      |                    ^
cake3.cpp:58:5: note: in expansion of macro 'debug'
   58 |     debug(i, j, sum, sum - 2 * abs(C[j].c - C[i].c));
      |     ^~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...