Doing Homework again C语言

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;
 } 
 
 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值