2014基本贪心1003

来源:互联网 发布:男士双肩包推荐 知乎 编辑:程序博客网 时间:2024/06/05 07:18

Doing Homework again

Time Limit : 1000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 44   Accepted Submission(s) : 28

Font: Times New Roman | Verdana | Georgia

Font Size:  

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.

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

03

5

思路:

从最后一天的作业开始算,往前挑出分数较高的先做。

代码:

#include <iostream>#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>using namespace std;struct node{    int a,b;}a[1001];bool cmp(node a,node b){   return a.a<b.a;}int main(){    int T;    cin>>T;    while(T--)    {       int n;       cin>>n;       int i,j;       int b[1001];       memset(b,0,sizeof(b));       int sum=0;       for(i=1;i<=n;i++)       {           cin>>a[i].a;       }       for(i=1;i<=n;i++)       {           cin>>a[i].b;           sum+=a[i].b;       }       sort(a+1,a+1+n,cmp);       int c,d;       int sum1=0;       for(i=a[n].a;i>0;i--)       {           c=0,d=0;           for(j=n;j>=1;j--)           {               if(a[j].a>=i)               {                   if(b[j]==0)                   {                       if(a[j].b>c)                       {                           c=a[j].b;                           d=j;                       }                   }               }               else break;           }           sum1+=c;           b[d]=1;       }       printf("%d\n",sum-sum1);    }    return 0;}

原创粉丝点击