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