nyoj 757 期末考试【优先队列+贪心】

期末考试

时间限制:1000 ms  |  内存限制:65535 KB
难度:2
 
描述
马上就要考试了,小T有许多作业要做,而且每个老师都给出来了作业要交的期限,如果在规定的期限内没
交作业就会扣期末成绩的分数,假设完成每门功课需要一天的时间,你能帮助小T扣除的分数最小吗?
 
输入
输入n,表示n门功课(n<2000),接下来n行,每行两个数a,b,分别表示交作业的最后期限,迟交扣除的分数。
(以文件结尾)
输出
输出扣除的最小分数。
样例输入
3
3 10
3 5
3 1
3
1 6
3 2
1 3
7
1 3
4 2
6 1
4 7
2 6
4 5
3 4
样例输出
0
3
5
将数据存入优先队列,优先队列出队顺序按照分数从大到小,设置数组vis[]来标记对应日期是否有空闲时间,
若无空闲时间则必须舍弃当下分数:
#include<stdio.h>
#include<cstring>
#include<queue>
using namespace std;
struct node
{
	int time;
	int score;
	friend bool operator < (node a,node b)
	{
		return a.score<b.score;
	} 
};
int main()
{
	int n,m,j,i,t;
	int vis[2100];
	while(scanf("%d",&t)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		priority_queue<node>q;
		node x;
		for(i=0;i<t;i++)
		{
			scanf("%d%d",&n,&m);
			x.time=n;
			x.score=m;
			q.push(x);
		}
		int sum=0;
		for(i=0;i<t;i++)
		{
			x=q.top();
			for(j=x.time;j>=1;j--)
			{
				if(vis[j]==0)
				{
					vis[j]=1;
					break;
				}
			}
			if(j==0)
			sum+=x.score;
			q.pop();
		}
		printf("%d\n",sum);
	}
	return 0;
}

  

posted @ 2015-07-28 10:21  非我非非我  阅读(156)  评论(0编辑  收藏  举报