Yougth maximum time limit: 1000 MS | memory limit: 65535 KB difficulty: 4
-
Description
-
The weight and value of n items in Yougth are Wi and Vi. Can you help him select k items to maximize the value of unit weight?
-
Input
-
Multiple groups of test data
The first row of each group of test data has two numbers n and k, and the next row has n numbers Wi and Vi.
(1 <= k = n <= 10000) (1 <= Wi, Vi <= 1000000)
-
Output
-
Outputs the maximum unit value. (Retain two decimal places)
-
Sample Input
-
3 22 25 32 1
-
Sample output
-
0.75
Idea: Binary and greedy
#include
#include
#includeusing namespace std;int w[10000],v[10000],n,k;double y[10000];bool C(double x){ for(int i=0;i
=0;}void solve(){ double lb =0,ub=10000; for(int i=0;i<100;i++) { double mid =(lb+ub)/2; if(C(mid)) lb = mid; else ub=mid; } printf("%.2f\n",ub);}int main(){ while(cin>>n>>k) { for(int i=0;i
>w[i]>>v[i]; solve(); //system("pause"); } return 0; }