Hdu 4803 Poor Warehouse Keeper (Greedy + math)

Source: Internet
Author: User

Link: hdu 4803 Poor Warehouse Keeper

A screen can display two values: quantity x and total price y. There are two types of operations: one is to add the total price once to change to x, x + y; the other is to add a quantity, and the total price will also add a price accordingly, 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.

Solution: if you add the total price once, the unit price will increase. If you add the total price once, the unit price will remain unchanged. All in all, the unit price will only go up rather than down.
Then the number of items must also change from 1 to x, that is to say, at least to add X-1 unit price can be, 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.
Set the current quantity to I and the critical total price to t.
Y + 1> t? Xi
T <(y + 1 )? Ix

That is, for each quantity, add the total price as much as possible, so that the unit price is as large as possible, and ensure that the amount of plus in the dough is not greater than the upper limit, because the unit price is large, the total price of each addition is faster than the target value.
#include 
  
   #include 
   
    #include 
    
     const double eps = 1e-9;int main () {    double x, y;    while (scanf("%lf%lf", &x, &y) == 2) {        if (x > y) {            printf("-1\n");            continue;        }        double k = (y+1-eps) / x;        int cnt = (int)x - 1;        double tmp = 1;        for (int i = 1; i <= (int)x; i++) {            double t = i * k;            int u = (int)(t-tmp);            tmp += u;            tmp = tmp * (i+1) / i;            cnt += u;        }        printf("%d\n", cnt);    }    return 0;}
    
   
  

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.