Doing Homework again【暑期集训R题】【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.
InputThe 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. 
OutputFor 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

    这道题的思维与DP的关系倒是不大,说实话,我觉得用到的贪心思想会多一些,之后的对于要写的作业的放置顺序会更加的贴近哈希算法。

    思路:我们能够得到这样的一串没有排过序的紊乱的截止日期与不做完就要扣的分数,我们要尽所能使得扣分少一些,那么,我想到的就是,把分数最高的开始,我们对于作业分数降序排列,而且让高分刚好掐在它的截止日期上,这样才能给后面的分数段留足空间,但遇到截止日期上刚好被其他数霸占了,那么说明这个数优先级要低,但又比剩下的数优先级高,遇到这种情况,我们把它往前提1,若不为空继续提1,但要是都满了,这个数就只能舍弃了。

完整代码:

#include 
#include 
#include 
using namespace std;
struct node
{
    int a,b;        //a指截止日期,b指扣分数
}k[1005];
int vis[1005];
bool cmp(node r1, node r2)
{
    return r1.b==r2.b?(r1.a>r2.a):(r1.b>r2.b);
}
int N;
int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        int sum=0;
        memset(vis, 0, sizeof(vis));
        scanf("%d",&N);
        for(int i=0; i0; j--)
            {
                if(!vis[j])
                {
                    vis[j]=1;
                    break;
                }
                else if(j==1)
                {
                    sum+=k[i].b;
                }
            }
        }
        printf("%d\n",sum);
    }
    return 0;
}

你可能感兴趣的:(贪心,哈希)