HDOJ贪心训练1009Doing Homework again


Problem Description

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.

Input

The input contains several test cases. The first line of the input is a single integer T that is the number of test cases. T test cases follow.
Each test case start with a positive integer N(1<=N<=1000) which indicate the number of homework.. Then 2 lines follow. The first line contains N integers that indicate the deadlines of the subjects, and the next line contains N integers that indicate the reduced scores.

Output

For each test case, you should output the smallest total reduced score, one line per test case.

题目大意:

    你参加过ACM了,落下很多课程的作业,你需要去写作业,每天只能完成一门,完不成是要扣分的

    给你每门作业的截止期限,然后给你对应要扣的分数,问你 最少扣多少分

题目思路:

    当然是贪心了,我不晓得,这个贪心应该是最优解,但不知道为什么是总问题的最优解

   先建立结构体,然后先对分数 进行排序,分数相同的话 然后对日期从小到大 因为日期短的话,就得先写,而且这个分数相同

 然后建立一个标记数组,用来标记是否有时间来完成这项作业

  之后对作业进行循环,对于第 i 份作业,前b[1..a[i].bed]是否有空余的一天把这个作业给完成  如果说完成不了的话,就只能

不做了

AC_CODE


   

#include<iostream>
#include<cstring>
#include<algorithm>
#define N 1005
using namespace std;
struct node
{
	int ded;
	int score;		
}a[N];
int b[N]; //标记数组 
int cmp(node a,node b)//分数不同的时候从高到低 分数相同的时候  日期从底到高  
{
	if(a.score==b.score)
		return a.ded<b.ded;
	return 	a.score>b.score;
}
int main()
{
	int T;//测试个数 
	cin>>T;
	int n;//作业的种类数 
	while(T--)
	{
		memset(b,0,sizeof(b));
		int sum=0;
		cin>>n;
		for(int i=1;i<=n;i++)
			cin>>a[i].ded;
		for(int i=1;i<=n;i++)
			cin>>a[i].score;
			
		sort(a+1,a+n+1,cmp);
		
		for(int i=1;i<=n;i++)//循环作业种类 
		{
			int j=a[i].ded; // 从截止时间开始往前推,如果有一天没用过,这一天就做这一门课,这门课不扣分
			while(j)
			{
				if(!b[j])
				{
					b[j]=1;
					break;
				}
				j--;
			}
			if(j==0)//如果j=0,表明从time往前的每一天都被占用了,这门课完不成
			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、付费专栏及课程。

余额充值