HDU 4803 poor warehouse keeper (Greedy)

Source: Internet
Author: User

Question Link

The screen displays two values: quantity X and total price y. There are two types of operations, one is to add a total price, to X, 1 + Y; the other is to add a quantity, and the total price will also add a price, to x + 1, Y + Y/X. The total price is an integer after the integer is obtained. The fractional part is ignored. Given a target X and Y, the initial state is. The minimum number of times the target State can be obtained. If the target State cannot be reached, the output is-1.

Idea: if it is to add a total price, the unit price will increase; if it is to add a quantity, the unit price will not change. Therefore, the unit price only increases and does not drop. The number of items must also be changed from 1 to X, that is to say, at least to add X-1 unit price can, then if the unit price too large S, S * (x + 1) ≥y + 1 is definitely not allowed. Therefore, for each I (Quantity), the unit price has an upper limit to ensure that the total price will not overflow when the number is increased.

1 # include <stdio. h> 2 # include <string. h> 3 # include <iostream> 4 5 using namespace STD; 6 7 const double EPS = 1e-9; 8 int main () 9 {10 double X, Y; 11 while (~ Scanf ("% lf", & X, & Y) {12 if (x> Y) 13 {14 printf ("-1 \ n"); 15 continue; 16} 17 double price = (Y + 1-eps)/X; // calculate by the two numbers x and y required in the final question, the final unit price should be S18 double sum = 1.0; 19 int ans = int (x-1); // if the number changes, the total price will not change, so to change to the final X, at least also need to change the X-1 times 20 for (INT I = 1; I <= int (x); I ++) {21 double totalprice = I * price; // take price as the final unit price, then the total price of the current quantity should be 22 int K = (INT) (totalprice-sum ); // how much is the difference to the total price of 23 ans + = K at the unit price; // Add the 24 sum + = K of the difference; // when the number is increased, the total price is also increased by 25 sum = sum * (I + 1) * 1.0/I); 26} 27 printf ("% d \ n ", ans); 28} 29 return 0; 30}
View code

 

HDU 4803 poor warehouse keeper (Greedy)

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.