UVa 1648 (push formula) business Center

Source: Internet
Author: User

Test instructions

There's a strange elevator that can only walk up the U-floor or down D-floors at a time.

Now there are M elevators, the minimum number of floors (which must be positive) to arrive at exactly N times, and the first default is on the No. 0 floor.

Analysis:

Assuming that the elevator goes up x times, then go down n-x times, then reach the floor is Xu-(n-x) d≥0

(u+d) The minimum value for the x≥nd,x is

In other words, if nd% (u+d) = = 0, x = nd/(u+d)

otherwise x = nd/(u+d) + 1

Considering that the condition cannot be reached on the 0 floor: when nd% (u+d) = = 0 This happens, you can only have one more time on the U-level, less the next D-floor, so this will eventually stop at the U+D layer

1#include <cstdio>2 3 intMain ()4 {5     intN, M;6 7      while(SCANF ("%d%d", &n, &m) = =2)8     {9         intAns =1000000000;Ten          while(m--) One         { A             intu, D; -scanf"%d%d", &u, &d); -             if((n*d)% (u+d) = =0) {if(U+d < ans) ans = u+D;} the             Else -             { -                 intx = (n*d)/(U+D) +1; -                 intt = x* (u+d)-n*D; +                 if(T < ans) ans =T; -             } +         } Aprintf"%d\n", ans); at     } -  -     return 0; -}
code June

UVa 1648 (push formula) business Center

Related Article

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.