代码改变世界

Summer Vacation(贪心 优先队列)

2019-09-28 15:53  木木王韦  阅读(165)  评论(0)    收藏  举报

Summer Vacation 贪心 优先队列

题目链接

这道题用普通的贪心会wa,需要加优先队列,好像是因为在结构体排序之后还有其他规律,因为不是很了解优先队列。。。补完优先队列再来

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
priority_queue<int>que;
int n,m;
struct tl{
	int t;
	int w;
};
tl x[1234567];
bool cmp(tl a,tl b){
	if(a.t==b.t) return a.w>b.w;
	return a.t<b.t ;
}
//int t[1234567],w[1234567];
int main(){
	cin>>n>>m;
	for(int i=0;i<n;i++){
		cin>>x[i].t>>x[i].w;
	}
	sort(x,x+n,cmp);
	int sum=0;
	int cnt=0;
	for(int i=0;i<m;i++){
//		cout<<x[cnt].t<<"  "<<x[cnt].w<<endl;
		while(x[cnt].t<=i+1&&cnt<=n){
			que.push(x[cnt].w);
//			sum+=x[cnt].w;
//			cout<<"::::"<<x[cnt].t<<"  "<<x[cnt].w<<endl;
			cnt++;
		}
		if(!que.empty()){
			sum+=que.top();
//			cout<<sum<<endl;
			que.pop();
		}
	}
	cout<<sum<<endl;
	return 0;
} 
/*
6 4
1 2
3 3
1 4
2 1
2 3
2 3
*/