洛谷 P2107 小Z的AK計劃

題目傳送門

\(\color{red}{這個題}\)思路差不多,本題可以先忽略距離的影響,最後再考慮.

一定一定一定一定一定一定一定一定一定一定要開long long

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>

using namespace std;

int n,k1,ans;
long long k,m;
struct kkk {
	long long x,sum;
}e[100001];
priority_queue<kkk> q;

inline bool operator <(kkk a,kkk b) {
	return a.sum < b.sum;
}

inline bool cmp(kkk a,kkk b) {
	return a.x < b.x;
}

int main() {
	scanf("%d%lld",&n,&m);
	for(int i = 1;i <= n; i++)
		scanf("%lld%lld",&e[i].x,&e[i].sum);
	sort(e+1,e+n+1,cmp);
	for(int i = 1;i <= n; i++) {
		if(k + e[i].x + e[i].sum <= m) {
			k = k + e[i].sum;
			ans++;
			q.push(e[i]);
		}
		else {
			if(q.empty()) continue;
			kkk o = q.top();
			if(k - o.sum + e[i].x + e[i].sum < k) {
				k = k - o.sum + e[i].x + e[i].sum;
				q.pop();
				q.push(e[i]);
			}
		}
	}
	printf("%d",ans);
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章