二、stl ,模拟,贪心等 [Cloned] P - 贪心

原题:

Ignatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Every teacher gives him a deadline of handing in the homework. If Ignatius hands in the homework after the deadline, the teacher will reduce his score of the final test. And now we assume that doing everyone homework always takes one day. So Ignatius wants you to help him to arrange the order of doing homework to minimize the reduced score.

题意:

有一些作业需要完成,每个作业有他的完成期限和如果没完成就要扣除的分数,每天可以完成一个作业

题解:

正常的贪心问题,先按照扣分顺序从高倒低开始完成,每个任务都尽量在最后一天完成,如果这个任务的完成期限那一天已经安排了别的任务,就往前推一天看能不能放在这一天完成,如果还不被占了就继续往前推,直到找到一个可以放的位置或者一个位置都找不到,那这个任务就只能放弃丢掉相应的分数。总的来讲就是贪心优先完成分值最高的任务。

代码:AC

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
typedef struct
{
	int deadline;
	int score;
}homework;
homework A[1020];
int compare(homework a,homework b)
{
	return a.score>b.score;
}
int main()
{
	int n;
	cin>>n;
	while(n--)
	{
		int m;
		cin>>m;
		int i,j,sum=0;
		for(i=0;i<m;i++)
			cin>>A[i].deadline;
		for(i=0;i<m;i++)
			cin>>A[i].score;
		sort(A,A+m,compare);
		int days[1020];
		memset(days,0,sizeof(days));
		for(i=0;i<m;i++)
		{
			for(j=A[i].deadline;j>0;j--)
			{
				if(!days[j])
				{
					days[j]=1;
					break;
				}
			}
				if(j==0)
					sum+=A[i].score;
		}
		cout<<sum<<endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/npuyan/article/details/81395730
今日推荐