Nyoj 914 maximize yougth

Source: Internet
Author: User
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

Analysis:

To obtain the maximum unit weight value, you need to perform a binary search between 0 and Max. Max is the maximum unit weight value obtained from any selected item,

Max = max (V [I]/W [I] | I = 0... n-1 ).

Why is Max so valid? The following example shows:

Assume that a pair of VI and WI meet the following requirements: VI/Wi = max (V [I]/W [I] | I = 0 ...... n-1), any pair of VK, wk (K! = I), there are vi/wi> VK/wk,

That is, VI * wk> VK * WI records it as Formula 1.

Proof:

What you want to prove is (vi + VK1 +... + vk2)/(WI + wk1 +... + WK2) <= VI/WI, (0 <= k1 <= k2 <= n-1 & K1! = I & K2! = I ),

1. When k1 = k2 = 0, that is, no value is added. The upper equals sign is valid.

2. For ease of proof, only VK (K! = I), it is common. Certificate (vi + VK)/(WI + WK) <VI/WI,

That is, WI (vi + VK) <VI (WI + WK), that is, VI * wk> VK * WI. This formula is Formula 1!

3. When the number of 2... N-1 is increased, it is also true.

4. Pass the certificate.

For more information about cleck (A), see the code below.

# Include <cstdio> # include <iostream> # include <cstring> # include <algorithm> using namespace STD; const int maxn = 10000 + 5; Double X [maxn], W [maxn], V [maxn]; // fully defined as double, which facilitates the calculation of int N, K; bool cleck (double A) {for (INT I = 0; I <n; I ++) x [I] = V [I]-A * W [I]; sort (x, x + n); double sum = 0; for (INT I = 0; I <K; I ++) sum + = x [n-I-1]; // greedy, return sum> = 0? True: false;} int main () {While (~ Scanf ("% d", & N, & K) {double max = 0; For (INT I = 0; I <n; I ++) {scanf ("% lf", & W [I], & V [I]); max = max (max, V [I]/W [I]);} double L = 0, r = max; while (R-l> 1e-4) {// binary search optimal value double M = (L + r)/2; If (cleck (M) L = m; else r = m;} printf ("%. 2lf \ n ", L);} return 0 ;}

Cleck (a) checks whether the unit weight value a is desirable. If yes, it must meet

(VK1 +... + vk2)/(wk1 +... + WK2)> = a (0 <= k1 <= k2 <= N-1 ),

That is, VK1 +... + vk2> = a (wk1 +... + WK2 ),

VK1-A * wk1 +... + vk2-A * WK2> = 0

Therefore, we recommend that you use vki-A * wki (that is, XI in the Code) as much as possible.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.