Bestcoder#5 1002

Bestcoder#5 1002

Poor MitsuiTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 336    Accepted Submission(s): 70


Problem Description

Because of the mess Mitsui have made, (not too long ago, Mitsui, leading a group of bully guys, intended to make some trouble and damage to the basketball team.) he was punished to mop the floor this week. And he needs to get some water first.

He takes N broken buckets to get some water. When he turns on the water-tap, the water begins to fill in at a speed V unit water per second.(And this speed will always be V.) As the buckets is broken, when there is water in the bucket, water begins to flow away at a certain speed. Each bucket has its own speed. At a time, there can only be one bucket to receive water from the water-tap, but at the same time water still flows away from the buckets (those contains water at that moment). 

We don’t allow a bucket to receive water from the water-tap for more than once or receive water from other buckets. And we assume that Mitsui can change the position of the buckets in a very short time (we can ignore the time it takes) and the capacity of each backet is so large that it can’t be fulfilled.

Now Mitsui wonders can there be a moment that each bucket is filled with a specific volunm of water. If it’s possible, find the earliest one, otherwise he will just go home in a bad mood.

 

 

Input

This problem contains multiple tests.
The first line contains a number T (1 ≤ T ≤ 150), which tells the total number of test cases.
Each test consists of three lines, the first line contains two integers, N and V. (1 ≤ N ≤ 40, 1 ≤ V ≤ 40). N tells the number of buckets and V tells the speed of water come out of the water-tap. In the second line, there are N integers, Ai (1 ≤ Ai ≤ 40) is the speed of water flow away from the i-th bucket. In the third line, there are N interges, Bi (0 ≤ Bi ≤ 40) is the volunm of water that Mitsui wants the i-th bucket to contain at that special moment.

 

 

Output

This problem is intended to use special judge. But so far, BestCoder doesn’t support special judge. So if it’s possible for Mitsui to optimize his movement to make that happen, you should output a number which tells the earliest time that might happen since Mitsui turns on the water-tap, rounded it into an integer, otherwise just output -1 instead.

 

 

Sample Input

23 31 1 32 2 23 31 1 12 2 2

 

 

Sample Output

-15

Hint

In the first example, Mitsui can’t make that happen no matter how he arranges the order of the buckets to receive water from the water-tap. So the answer is -1.In the second example, Mitsui can make that happen in 4.75 seconds. And that is the optimal answer. So you should output 5. Please note, If the accurate answer is X, you are recommended to  use printf(“%.0f\n”, X) to output the answer in C++.  

  1 #include<iostream>
  2 
  3 #include<cstdio>
  4 
  5 #include<cstring>
  6 
  7 #include<vector>
  8 
  9 #include<cmath>
 10 
 11 #include<map>
 12 
 13 #include<algorithm>
 14 
 15 #define M(a,b) memset(a,b,sizeof(a))
 16 
 17 using namespace std;
 18 
 19 
 20 
 21 int T;
 22 
 23 int N;
 24 
 25 double V;
 26 
 27 double time[106];
 28 
 29 
 30 
 31 struct bow
 32 
 33 {
 34 
 35     double A,B;
 36 
 37     bool operator < (const bow& rhs) const
 38 
 39     {
 40 
 41         return B/A>rhs.B/rhs.A;
 42 
 43     }
 44 
 45 }bo[106];
 46 
 47 
 48 
 49 int main()
 50 
 51 {
 52 
 53   scanf("%d",&T);
 54 
 55   while(T--)
 56 
 57   {
 58 
 59       scanf("%d%lf",&N,&V);
 60 
 61       for(int i = 0;i<N;i++)
 62 
 63       {
 64 
 65           scanf("%lf",&bo[i].A);
 66 
 67       }
 68 
 69       for(int i = 0;i<N;i++)
 70 
 71       {
 72 
 73           scanf("%lf",&bo[i].B);
 74 
 75       }
 76 
 77       sort(bo,bo+N);
 78 
 79       double res = 0;
 80 
 81       int flag = 1;
 82 
 83       for(int i = N-1;i>=0;i--)
 84 
 85       {
 86 
 87           if(bo[i].B==0) continue;  //不明白为什么需要加上这一句,没理由啊,求hack
 88 
 89           bo[i].B += bo[i].A*res;
 90 
 91           if((bo[i].A>=V&&bo[i].B!=0)||bo[i].B/(V-bo[i].A)<0) {flag = 0; break;}
 92 
 93           res+= bo[i].B/(V-bo[i].A);
 94 
 95           //cout<<res<<endl;
 96 
 97       }
 98 
 99       if(!flag) {puts("-1"); continue;}
100 
101       printf("%.0lf\n",res);
102 
103   }
104 
105   return 0;
106 
107 }

 

posted @ 2014-10-19 19:56  haohaooo  阅读(167)  评论(0编辑  收藏  举报