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.
思路:按时间从小到大排序,然后从后往前遍历,用ts存储剩余天数,若ts比前一组数的天数大一天,则用sum减去从i开始所有数的最大扣分;若大于一天,则重复上述操作,并重复比较ts和前一组数天数。
#include "stdio.h"
int main()
{
int i,j,t,n,lp,stu[1002][1002],min,temp,max,ts;
long long int sum=0;
scanf("%d",&t);
stu[1][0]=0;
while(t--)
{
sum=0;
scanf("%d",&n);
for(i=1;i<=2*n;i++) //读时间
{
if(i<=n)
scanf("%d",&stu[1][i]);
else
{
scanf("%d",&stu[2][i-n]);
sum=sum+stu[2][i-n];
}
}
/* seect排序 */
for(i =1;i<n;i++)
{
min = i;
for(j =i+1;j<=n;j++)
{
if(stu[1][j] < stu[1][min]) //天数小的排前面
{
min = j;
}
}
if(i!=min)
{
temp = stu[1][i];
stu[1][i]= stu[1][min];
stu[1][min] = temp;
temp = stu[2][i];
stu[2][i]= stu[2][min];
stu[2][min] = temp;
}
}
/* 排序结束 */
ts=stu[1][n]; //总共的天数
for(i=n;i>=1,ts>0;i--)//从后往前找最大值
{
max=0;
if(ts==stu[1][i-1]+1) //天数相差 1
{
for(j=i;j<=n;j++)
{
if (stu[2][j]>=max)
{
max=stu[2][j];
lp=j;
}
}
sum=sum-max;
stu[2][lp]=0;
ts--;
}
else if(ts>stu[1][i-1]+1)
{
for(j=i;j<=n;j++)
{
if (stu[2][j]>=max)
{
max=stu[2][j];
lp=j;
}
}
sum=sum-max;
stu[2][lp]=0;
i++; //重新比较
ts--;
}
}
printf("%lld\n",sum);
}
return 0;
}