HDU-1789 Doing Homework again

来源:互联网 发布:淘宝网址怎么修改 编辑:程序博客网 时间:2024/05/16 00:31

一道贪心题目,没感觉出来哪里是DP。

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
333 3 310 5 131 3 16 2 371 4 6 4 2 4 33 2 1 7 6 5 4
Sample Output
035




提议很容易读懂,给你科目的截止时间和对应科目的分数,让你求怎样的做题顺序,使扣掉的分数最少.

贪心策略:

先按分数从高到低排序, 如果分数相同就按截止时间从早到晚.

编程:

大体思路就是从一个科目的截止时间往前倒着走,看看是否还有时间可以利用,如果没有时间可以利用,那么这门科目注定要扣分了。

上代码:

#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>using namespace std;struct node{ int dead; int sc;};bool used[1010];node op[1010];int n;int t;bool cmp(node a,node b){    if(a.sc == b.sc) return a.dead < b.dead;    return a.sc > b.sc;}int main(){    scanf("%d", &t);    while(t--)    {        scanf("%d", &n);        memset(op, 0, sizeof(op));               for(int i=1; i<=n; i++)        {            scanf("%d", &op[i].dead);        }        for(int i=1; i<=n; i++)        {            scanf("%d", &op[i].sc);        }        sort(op+1, op+n+1,cmp);        int sum = 0;        memset(used, false, sizeof(used));        for(int i=1; i<=n; i++)        {           int j;           for(j=op[i].dead; j>=1; j--) // 倒着往前走,看看是否还有时间可以利用           {               if(!used[j])               {                   used[j] = true;                   break;               }           }           if(j<1) sum += op[i].sc;        }                cout << sum << endl;    }    return 0;}

水波。

原创粉丝点击