HDUJ 1789 Doing Homework again 贪心

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6335    Accepted Submission(s): 3746

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.

Sample Input

3
3
3 3 3
10 5 1
3
1 3 1
6 2 3
7
1 4 6 4 2 4 3
3 2 1 7 6 5 4

Sample Output

0
3
5
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;

struct node
{
	int time,score;
}s[1005];
int v[1005];

int cmp(node x,node y)
{
	if(x.score==y.score)
		return x.time<y.time;
	else
		return x.score>y.score;
}

int main()
{
	int n,m;
	cin>>n;
	while(n--)
	{
		cin>>m;
		int i,j;
		for(i=0;i<m;i++)
			cin>>s[i].time;
		for(j=0;j<m;j++)
			cin>>s[j].score;

		sort(s,s+m,cmp);
		int sum=0;
		memset(v,0,sizeof(v));
		for(i=0;i<m;i++)
		{
			int t=s[i].time;
			if(v[t]==0)
				v[t]=1;
			else
			{
				while(v[t]==1)  t--;
				if(t==0)   sum += s[i].score;
				else   v[t]=1;
			}
		}

		cout<<sum<<endl;
	}

	return 0;
}
时间: 2024-11-07 14:58:22

HDUJ 1789 Doing Homework again 贪心的相关文章

hdu 1789 Doing HomeWork Again (贪心算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 /*Doing Homework again Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 7903 Accepted Submission(s): 4680 Problem Description Ignatius has just c

HDU 1789 - Doing Homework again - [贪心+优先队列]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1789 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem DescriptionIgnatius has just come back school from the 30th ACM/ICPC. Now he has a lot of homework to do. Eve

HDU 1789 Doing Homework again(贪心)

Doing 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 deadlin

hdoj 1789 Doing Homework again 【贪心】

贪心策略:先按分数从大到小排序,分数一样,再按时间从小到大排序 分最高的越靠近期限完成,越好 话不多说直接看代码 题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=1789 代码: #include<cstdio> #include<cstring> #include<algorithm> using std::sort; typedef struct{ int sco, time; }str; str s[1005]; bo

HDUJ 3177 Crixalis&#39;s Equipment 贪心

Crixalis's Equipment Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2568    Accepted Submission(s): 1059 Problem Description Crixalis - Sand King used to be a giant scorpion(蝎子) in the deserts

杭电 1789 Doing Homework again (贪心 求最少扣分)

Description zichen 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 zichen hands in the homework after the deadline, the teacher will reduce his s

HDU - 1789 Doing Homework again(贪心) ~~~学了一波sort对结构体排序

题目中因为天数和分数是对应的,所以我们使用一个结构体来存分数和截止如期. 一开始做这道题的时候,很自然的就想到对天数排序,然后天数一样的分数从大到小排序,最后WA了之后才发现没有做到"舍小取大"的贪心.所以改变一下策略,对分数排序,如果分数一样的话,时间从小到大排序(因为我们的目的就是先做分多的作业,所以分数一样的得放到前几天去做). (具体sort排结构体知识见代码里面,其实也可以写两次for来排序): 思路:排好序之后,从小到大遍历,每找到一个分数,去寻找对应的天数到第一天中有没有

贪心/hdu 1789 Doing Homework again

1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 4 using namespace std; 5 int n,ans; 6 struct node 7 { 8 int score; 9 int ddl; 10 }; 11 node a[1010]; 12 bool day[1010]; 13 bool cmp(node x,node y) 14 { 15 return x.score>y.s

hdu 1789 Doing Homework again(贪心)

题意:N个作业,每个作业有个deadline.每个作业完成耗时一天. 如果某个作业没在deadline前完成,则要扣去一定的分数. 给出N个要扣除的分数score[1]....score[N]. 如何安排使得扣分最少?求最少扣分. 思路: 按扣分多少从大到小排序,然后一个一个放到各自的deadline前的某个位置,哪个位置? 哪个位置有空就放那儿,且必须要都靠后!也就是从后往前放.这样可以保证最优地不占用更靠前的别人的deadline前的空间. 具体看代码,,,, 代码: struct node