I - Doing Homework again(贪心)

I - Doing Homework again

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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 score of the final test. And now we assume that doing everyone homework always takes one day. So zichen 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

 

//又坑到我了,唉,贪心算法。我想的是按交作业的天数从小到大排,天数一样就扣分多的放前面,然后开始遍历,如果不能完成,看前面有没有分数比这个小的,有就扣那个最少的分,注意是最少的,没有就扣这个分,然后直到遍历结束,求告诉哪里错了。

 

 1 #include <iostream>
 2 #include <algorithm>
 3 using namespace std;
 4 
 5 struct zuoye
 6 {
 7     int day;
 8     int fen;
 9 };
10 zuoye book[1001];
11 
12 int cmp(zuoye a,zuoye b)
13 {
14 
15     if (a.day!=b.day)
16         return a.day<b.day;
17     return a.fen>b.fen;    
18 }
19 
20 int main()
21 {
22     int n,i,all,cur,min,T;
23     scanf("%d",&T);
24     while(T--)
25     {
26         scanf("%d",&n);
27         for (i=0;i<n;i++) scanf("%d",&book[i].day);
28         for (i=0;i<n;i++) scanf("%d",&book[i].fen);
29         sort(book,book+n,cmp);
30         cur=1,all=0;
31         for (i=0;i<n;i++)
32         {
33             if (cur<=book[i].day)
34             {
35                 cur++;
36                 continue;
37             }
38             if (cur>book[i].day)
39             {
40                 min=book[i].fen;
41                 for (int j=i-1;j>=0;j--)
42                 {
43                     if (book[j].fen<min)
44                     {
45                         min=book[j].fen;
46                     }
47                 }
48                 all+=min;
49             }
50         }
51         printf("%d\n",all);
52     }
53     return 0;
54 }

View Code

 

AC的:看别人的,是按分数大的排前面,若分一样,天数小的靠前。遍历每一个数据,将截至日期前的天数再遍历,如果有空闲的日子,就用那天做这个,没有就扣分

 

 

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 struct Node
 7 {
 8     int day,fen;
 9 } node[1005];
10 
11 int cmp(struct Node a,struct Node b)
12 {
13     if(a.fen!=b.fen)
14         return a.fen > b.fen;//扣分越多的越靠前
15     return a.day < b.day;//扣分相同的时候,deadline越早的越靠前
16 }
17 
18 int  visit[2000];//如果当天没用过,值为0;否则为1
19 
20 int main()
21 {
22     int T,n,i,j,all;
23     scanf("%d",&T);
24     while (T--)
25     {
26         scanf("%d",&n);
27         for (i=0;i<n;i++) scanf("%d",&node[i].day);
28         for (i=0;i<n;i++) scanf("%d",&node[i].fen);
29         memset(visit,0,sizeof(visit));
30         sort(node,node+n,cmp);
31         all=0;
32         for (i=0;i<n;i++)
33         {
34             for(j=node[i].day;j>0;j--)
35             {
36                 if (!visit[j])
37                 {
38                     visit[j]=1;
39                     break;
40                 }
41             }
42             if (j==0) all+=node[i].fen;
43         }
44         printf("%d\n",all);
45 
46     }
47 
48     return 0;
49 }

View Code

 

 

 

 

 

posted @ 2016-07-21 16:30  happy_codes  阅读(199)  评论(0编辑  收藏  举报