程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> (貪心)HDU 1789 解題報告

(貪心)HDU 1789 解題報告

編輯:C++入門知識

(貪心)HDU 1789 解題報告


Doing Homework again

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


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

思路:

既然要讓被扣掉的分數最少,那麼必然是對分數高的作業優先安排。注意題中有一個不是很明顯的條件可以支持這一點:完成每份作業都需要一天。這樣就避免了優先完成一份分數高的作業而導致n(n>1)份作業沒有完成,而且這n份作業分數和比一份分數高的作業還要大的情況。


方法:

對於所有的作業,按照分數從高到低排序,分數相同時,截止時間小的排在前面。另外初始化一個大小為n的數組,用來保存某一天是否已經被占用。然後開始貪心,對於每份作業,看從當天到當前之前的時間裡面,有沒有空余,如果有空余,安排到空余且天數最大的一天;如果沒有空余,這份作業只能被扣分。注意這邊天數從1開始,而代碼中習慣數組從0開始,天數減1或者數組加1都可以。


參考代碼:

#include
#include
using namespace std;
struct node{
    int deadline;
    int score;
};
bool cmp(node a, node b){
    if(a.score==b.score){
    	return a.deadlineb.score;
}
node homework[1002];
int used[1002];
int main(){
	int t, n, ans;
	scanf("%d", &t);
	while(t--){
		scanf("%d", &n);
		for(int i=0;i=0;j--){
				if(used[j]==0){
					used[j] = 1;
					flag = true;
					break;
				}
			}
			if(!flag){
				ans += homework[i].score;
			}
		}
		printf("%d\n", ans);
	}
}


  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved