1. 程式人生 > >2018.2.4【 HDU

2018.2.4【 HDU

Doing Homework again

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15959    Accepted Submission(s): 9294


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
Author lcy
Source
Recommend lcy

【題目大意】

做作業,每一份作業有ddl和score(不按期完成減掉的分數)兩個屬性,給出n份作業的兩個屬性,求最少的扣分。

【解題思路】

貪心,我們儘量保證讓分值大的作業按期完成,相同考慮相同分值大的安排ddl小的(避免先安排ddl大的佔據了ddl小的位置,而ddl大的有多種選擇)。這樣按照score較大,相同score則ddl較小的優先的方式排序,然後從ddl日期逐步往前推(儘量先佔後面的日期),如果找不到安排該作業的時間,則扣分加上該作業的score值。

【解題程式碼】

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
const int maxn=1e5+10;
struct node 
{
	int ddl;
	int score;
}a[maxn];
int vis[maxn];
bool cmp(node a,node b)
{
	if(a.score!=b.score) return a.score>b.score;
	else return a.ddl<b.ddl; 
} 
int main()
{
	int t;
	scanf("%d",&t);
	while(t--)
	{
		int n;
		scanf("%d",&n);
		memset(vis,0,sizeof(vis)); 
		for(int i=0;i<n;i++)
			scanf("%d",&a[i].ddl);
		for(int i=0;i<n;i++)
			scanf("%d",&a[i].score);
		sort(a,a+n,cmp);
		int sum=0;
		for(int i=0;i<n;i++)
		{
			int j=a[i].ddl;
				for(;j>0;j--)
				{
					if(!vis[j])
					{
						vis[j]=1;
						break;
					}
				}
				if(!j)	sum+=a[i].score;
		} 
		printf("%d\n",sum);
	} 
	return 0;
	
	
 } 

【收穫與反思】

貪心策略,優先分值大的,並且優先安排日期靠後的位置(這一步學習一下)。