灌水VS抽水

来源:互联网 发布:cnabs数据库全称 编辑:程序博客网 时间:2024/04/30 11:53
Problem DescriptionThere 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.InputThere 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.OutputIf 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 Input22 32-2 32-2 -20Sample Output2NO 1Impossible//标程:#include<iostream>#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;double a[100];const double inf = 1e-8;int main(){//freopen("a.txt","r",stdin);//freopen("b.txt","w",stdout);    int n, i;while(cin >> n){if(n == 0) break;double sum = 0.0;for(i = 0; i < n; ++ i){double x;cin >> x;a[i] = 1.0 / x;sum += a[i];}sort(a,a+n);        if(sum > inf){sum = 1.0 / sum;int tmp = (int)sum;if(sum - tmp > inf) tmp ++;cout << tmp << endl;}else{for(i = 0; i < n; ++ i){sum -= a[i];if(sum > inf) break;}if(i == n)  cout << "Impossible" << endl;else cout << "NO " << i + 1 << endl;}}return 0;}

0 0