hdu 4442 Physical Examination

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4442

题目描述:

 

 

Physical Examination

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1555    Accepted Submission(s): 461


Problem Description
     WANGPENG is a freshman. He is requested to have a physical examination when entering the university.
Now WANGPENG arrives at the hospital. Er….. There are so many students, and the number is increasing!
     There are many examination subjects to do, and there is a queue for every subject. The queues are getting longer as time goes by. Choosing the queue to stand is always a problem. Please help WANGPENG to determine an exam sequence, so that he can finish all the physical examination subjects as early as possible.
 


 

Input
     There are several test cases. Each test case starts with a positive integer n in a line, meaning the number of subjects(queues).
     Then n lines follow. The i-th line has a pair of integers (ai, bi) to describe the i-th queue:
     1. If WANGPENG follows this queue at time 0, WANGPENG has to wait for ai seconds to finish this subject.
     2. As the queue is getting longer, the waiting time will increase bi seconds every second while WANGPENG is not in the queue.
     The input ends with n = 0.
     For all test cases, 0<n≤100000, 0≤a i,b i<2 31.
 


 

Output
     For each test case, output one line with an integer: the earliest time (counted by seconds) that WANGPENG can finish all exam subjects. Since WANGPENG is always confused by years, just print the seconds mod 365×24×60×60.
 


 

Sample Input
  
  
5 1 2 2 3 3 4 4 5 5 6 0
 


 

Sample Output
  
  
1419
Hint
In the Sample Input, WANGPENG just follow the given order. He spends 1 second in the first queue, 5 seconds in the 2th queue, 27 seconds in the 3th queue, 169 seconds in the 4th queue, and 1217 seconds in the 5th queue. So the total time is 1419s. WANGPENG has computed all possible orders in his 120-core-parallel head, and decided that this is the optimal choice.

 

 

题意:说要参加一场考试,每个科目就是一个考试队列,每个科目队列有如下两个属性,a,b,那么该科目的消耗时间=a+b*(不在该队列排队的时间);问给出一系列科目队列,确定一个最短总耗时的队列次序。

 

题解:贪心,策略为比较 bi/ai  与  bj/aj 的大小,较大者就先考较大的这门科目,ai=0的  排在前面(bi较大者排在前面),bi=0的 排在最后,因为相当于此科目没有成长性,随便什么时候做都行,这样利用这个策略将科目队列排序,然后依照顺序做就行了,注意数据应 longlong  和  结果取模。

 

代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#define DM 31536000
using namespace std;
__int64 Examin[100000+5][2];
int n=0;
__int64 Sum=0;
/*compare for the qsort*/
int qcmp(const void *a,const void *b)
{
	__int64 *A=(__int64 *)a;
	__int64 *B=(__int64 *)b;
	if(A[0]==0&&B[0]==0)
	{
		if(A[1]>B[1])
		{
			return(-1);
		}
		else if(A[1]<B[1])
		{
			return(1);
		}
		else
		{
			return(0);
		}
	}
	else if(A[0]==0&&B[0]!=0)
	{
		return(-1);
	}
	else if(A[0]!=0&&B[0]==0)
	{
		return(1);
	}
	else//A[0]!=0 && B[0]!=0
	{
		if(A[1]==0&&B[1]==0)
		{
			return(0);
		}
		else if(A[1]==0&&B[1]!=0)
		{
			return(1);
		}
		else if(A[1]!=0&&B[1]==0)
		{
			return(-1);
		}
		else
		{
			if((double)A[1]/A[0]>(double)B[1]/B[0])
			{
				return(-1);
			}
			else if((double)A[1]/A[0]<(double)B[1]/B[0])
			{
				return(1);
			}
			else
			{
				return(0);
			}
		}
	}
	return(0);
}
/*for test*/
int test()
{
	return(0);
}
/*main process*/
int MainProc()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		int i=0;
		Sum=0;
		for(i=0;i<=n-1;i++)
		{
			scanf("%I64d%I64d",&Examin[i][0],&Examin[i][1]);
		}
		qsort(Examin,n,sizeof(Examin[0]),qcmp);
		//for(i=0;i<=n-1;i++)
		//{
		//	printf("%d %d\n",Examin[i][0],Examin[i][1]);
		//}
		Sum+=Examin[0][0];
		for(i=1;i<=n-1;i++)
		{
			Sum+=Examin[i][0]+(Examin[i][1]*Sum)%DM;
			Sum%=DM;
			//printf("%d\n",Sum);
		}
		printf("%I64d\n",Sum);
	}
	return(0);
}
int main()
{
	MainProc();
	return(0);
}


 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值