二、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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值