Doing Homework again
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10008 Accepted Submission(s): 5854
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.
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
对于每个作业,先按照扣分从大到小排序,若分数相同则按照截止日期从小到大排序。
然后按顺序,从截止日期开始往前找没有占用掉的时间。
如果找不到了,则加到罚分里面。
1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 const int MAX = 1050; 7 struct Homework 8 { 9 int s; 10 int d; 11 }; 12 typedef Homework H; 13 H h[MAX]; 14 bool u[MAX]; 15 bool compare(const H& h1, const H& h2) 16 { 17 if (h1.s>h2.s) 18 return true; 19 else if (h1.s == h2.s && h1.d<h2.d) 20 return true; 21 else 22 return false; 23 } 24 25 int solute(int n) 26 { 27 int j, score = 0; 28 for (int i = 0; i < n; i++) 29 { 30 for (j = h[i].d; j > 0; j--) 31 { 32 if (!u[j]) 33 { 34 u[j] = true; 35 break; 36 } 37 } 38 if (j == 0) 39 score += h[i].s; 40 } 41 return score; 42 } 43 int main() 44 { 45 int t, n, pos; 46 scanf("%d", &t); 47 while (t--) 48 { 49 scanf("%d", &n); 50 memset(u, 0, sizeof(u)); 51 for (int i = 0; i < n; i++) 52 scanf("%d", &h[i].d); 53 54 for (int i = 0; i<n; i++) 55 scanf("%d", &h[i].s); 56 57 sort(h, h + n, compare); 58 59 printf("%d ", solute(n)); 60 } 61 62 }