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 }
时间: 2024-10-09 16:55:37

Bestcoder#5 1002的相关文章

bestcoder#36 1002 哈希

bestcoder#36 1002 哈希 Gunner Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 726    Accepted Submission(s): 326 Problem Description Long long ago, there is a gunner whose name is Jack. He likes t

bestcoder#43 1002 在数组中找两个数的和取模的最大值 二分

bestcoder#43 1002 在数组中找两个数的和取模的最大值  二分 pog loves szh II Accepts: 97 Submissions: 834 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Problem Description Pog and Szh are playing games. There is a sequence with n number

HDOJ 4884 &amp; BestCoder#2 1002

TIANKENG’s rice shop Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 212    Accepted Submission(s): 9 Problem Description TIANKENG managers a pan fried rice shop. There are n kinds of fried rice

bestcoder#32 1002 哈希+后缀数组

bestcoder#32 1002 哈希+后缀数组 Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1161    Accepted Submission(s): 59 Problem Description When given an array (a0,a1,a2,?an−1) a

HDU 5269 &amp;&amp; BestCoder #44 1002 ZYB loves Xor I (分治)

题目地址:HDU 5269 比赛的时候想到了分治的思路,但是脑残了.,.写麻烦了...调了好久也没调出来..(分治写的太少..)赛后优化了一下..就过了.. 我的思路是先排序,排序是按照的将每个数字的二进制表示倒过来的字典序从大到小排,比如样例2中的2,6,5,4,0,二进制分别是010,110,101,100,000,排序之后是 101 110 010 100 000 这样的话就把后缀相同的都给放在一块了.其实也相当于字典树,不过比赛的时候没想到字典树,只想到了分治.. 然后从末位开始找,对所

bestcoder #9 1002(hdu4994)Revenge of Nim(博弈)

题目地址:HDU 4994 在这个题中,谁拥有了第一个大于1的数的控制权,就是谁赢,因为它可以有两种选择,一种是全选,另一种是选n-1个,此时另一个只能选剩下的那一个.但是当数为1的时候是没法控制的,只能选这一个.这时候就可以在每次选一个大于1的数的时候,就通过这两种选择,来让自己会正好选下一个大于1的数,由于中间的全是1,所以完全可以达到目的.这样只要控制了第一个大于1的数,那后面的就可以通过这次的操作让下面一个大于1的数仍然正好轮到自己,一直到最后一个也是.所以就转化成了判断第一个大于1的数

bestcoder#23 1002 Sequence II 树状数组+DP

Sequence II Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 652    Accepted Submission(s): 164 Problem Description Long long ago, there is a sequence A with length n. All numbers in this sequenc

BestCoder Round #4 1002

这题真是丧心病狂,引来今天的hack狂潮~ Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 10    Accepted Submission(s): 3 Problem Description There are N point on X-axis . Miaomiao would like t

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g