HDOJ1789--Doing Homework again

来源:互联网 发布:尼康软件人像后期 编辑:程序博客网 时间:2024/06/06 19:04
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

 
思路:1.这是一道贪心算法的题,需要排序,首先按照分数的非递减进行排序; 2.如果遇到相等的分,
就按日期的从小到大排序;   3.排序后计算结果时,是用另外一个数组,如果遇到相同的日期就往前面

找能不能找到插入的地方,最后剩下的就是最小的扣分情况正确答案如下:

#include<iostream>#include<stdio.h>#include<string.h>#include<stdio.h>#include<algorithm>using namespace std;struct dot{    int x;    int score;};bool cmp(const dot &a,const dot &b){    if(a.score!=b.score)    return a.score>b.score;    else    return a.x<b.x;}int main(){    dot a[1050];    int num[1050];    int test;    int n,j,i;    cin>>test;    while(test--)    {        memset(num,0,sizeof(num));     //对数组进行初始化        cin>>n;        for(int i=0;i<n;i++)            cin>>a[i].x;        for(int i=0;i<n;i++)            cin>>a[i].score;            sort(a,a+n,cmp);            int sum=0;      for(i=0;i<n;i++)                //这一点不好想     {        for(j=a[i].x;j>0;j--)        //分数大的并且相同日期,就往前面找看能不能插入        {          if(num[j]==0)          {              num[j]=1;              break;          }        }      if(j==0)                     //重点每次循环不能找到话就相加给sum        sum+=a[i].score;     }        cout <<sum<<endl;    }    return 0;}




0 0