灌水VS抽水

Problem Description
There is something crazy! There is a pool and many of pumps to fill up the empty pool. We know that the capacity of each pump. And at last, we want you to tell us whether we can fill up the empty pool and the time we have to. And if we can’t fill up the pool with all of the pumps, the least number of pumps we have to close.
Input
There are multiply test cases in this problem. In each test case, first line is a integer of n(1<=n<=15), which is the number of pumps. At the second line there are n integers ai(1<=|ai|<=10),which is the time(in hours) to fill up the empty pool(ai>0) or to empty the pool(ai<0).
All the cases will be end with n=0.
Output
If we can fill up the empty pool with all of the pump, print the hours (in integer) we have to use, otherwise print one line of “NO” and the least number we have to close up. At last, if we can’t fill up the pool with some of the pumps closed, print one line of “Impossible”.
Sample Input
2
2 3
2
-2 3
2
-2 -2
0
Sample Output
2
NO 1

Impossible

#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define INF 1.e-8
int pump[205],n;
double pump1[205];
int main()
{
	//freopen("b.txt","r",stdin);
	while(scanf("%d",&n)==1&&n)
	{
		int i,flag=0;
		double sum=0.0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&pump[i]);
			if(pump[i]>0) 
				flag=1;
			pump1[i]=1.0/(pump[i]*1.0);
			sum+=pump1[i];
		}
		sort(pump1+1,pump1+n+1);
		if(!flag)
			printf("Impossible\n");
		else
		{
			if(sum>INF)
			{
				double tmp=1.0/sum;
				int x=(int)tmp;
				if(tmp-x>INF)
					printf("%d\n",x+1);
				else printf("%d\n",x);
			}
			else 
			{
				for(i=1;i<=n;i++)
				{
					sum-=pump1[i];
					if(sum>INF) break;
				}
				printf("NO %d\n",i);
			}
		}
	}
	return 0;
}




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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值