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
5
题意:有T组数据,每组数据一个n,表示有n个科目,后跟两行数据,第一行表示这n个科目上交的截至时间,第二行表示这n个科目如果没交上分别扣多少分。求怎样安排你的时间才能使扣的分最少。
思路:运用结构体知识,对结构体进行一个排序,将分扣的多的往前排,先写扣分多的,进行for循环从头到尾便利,每一个都从后往前安放,直到有一天满足条件,标记这一天被占用了,那么这个作业就能交上,反正,会扣分。贪心题,用这种做法可以使扣的分最小。代码如下:
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct node
{
int x,y;
} list[1010];
bool cmp(node a,node b)
{
return a.y>b.y;
}
int main()
{
int T,i,j,n,flag;
int book[1010]; //标记这一天有没有被占用
scanf("%d",&T);
while(T--)
{
memset(book,0,sizeof(book));
int maxx=-1,sum=0;
scanf("%d",&n);
for(i=0; i<n; i++)
scanf("%d",&list[i].x);//截至日期
for(i=0; i<n; i++)
scanf("%d",&list[i].y);//扣的分数
sort(list,list+n,cmp);
for(i=0; i<n; i++)
{
if(list[i].x>maxx)
maxx=list[i].x; //求出截止日期最大的
}
for(i=0; i<n; i++)
{
flag=0;
for(j=maxx; j>=1; j--) //从后往前
{
if(book[j]==0&&list[i].x>=j) //这是应满足的条件
{
book[j]=1; //标记这一天被占用
flag=1;
break;
}
}
if(flag==0)
sum=sum+list[i].y;
}
printf("%d\n",sum);
}
return 0;
}