【堆】膳食搭配

【堆】膳食搭配


逆向思维。将时间线倒过来,变成xxx时间点出现某种食物。
然后每次吃最少的那种食物即可。


代码

#include<bits/stdc++.h>
using namespace std;
int n, c, rs, ans, bhh, X[200005];
long long tt, lt, lll;
struct asdf{
	int t;
	long long k;
	int bh;
} F[200005];
struct foood{
	long long num;
	int bh;
	bool operator < (const foood& cmp) const{ //定义小于号
		return num < cmp.num;
	} 
};
bool cmp(asdf aa, asdf bb){
	return aa.t > bb.t;
}
int main(){
	priority_queue<foood> q; 
	scanf("%d%d", &n, &c);
	for(int i = 1; i <= n; ++i){
		scanf("%d%lld", &F[i].t, &F[i].k);
		F[i].bh = i;
	}
	sort(F+1, F+1+n, cmp);
	rs = 0;
	while(rs < n){
		++rs; tt = F[rs].t;
		while(F[rs].t == tt && rs <= n){
			q.push((foood){-F[rs].k, F[rs].bh});
			++rs;
		}
		lt = tt - F[rs].t; //食用时间 
		--rs;
		lt = lt * c; //食用量 
		while(!q.empty() && lt){
			if(-q.top().num <= lt) {
				lt += q.top().num;
				X[++ans] = q.top().bh;
				q.pop();
			}
			else{
				lll = -q.top().num - lt;
				bhh = q.top().bh;
				q.pop();
				q.push((foood){-lll, bhh});
				break;
			}
		}
	}
	printf("%d\n", ans);
	sort(X+1, X+1+ans);
	for(int i = 1; i <= ans; ++i)
		printf("%d ", X[i]);
} 
上一篇:基于STC单片机的无线充电系统


下一篇:P4211 [LNOI2014]LCA