# P1909 buy a pencil problem solution

Title Description

P n Teachers need to go to the store to buy pencils as children participate NOIP gift. She found the store a total of  3 three kinds of packaging pencil, pencil in a number of different packaging may be different, prices may vary. To be fair, P teacher decided to only buy the same package of pencils.

Shops are not allowed to open pencil packaging, so P teacher may need to buy more than the n- the n-pencil is enough for the children to send gifts.

Now P teacher wanted to know, in each store the number of packages are sufficient, at least enough to buy the n- the n-pencil least need to spend much money.

Input Format

The first line contains a positive integer n- n-, represents the number of pencil required.

The next three rows with 2 two positive integers a packaged pencil described: wherein first . 1 a represents an integer number of such packages within the pencil, the first 2 2 Prices integer such packages.

Ensure that all . 7 7 digits is no more than 10000 . 1 0 0 0 a positive integer including 0.

Output Format

1 an integer representing the money it takes at least P teacher.

Sample input and output

Input # 1
1 57
2 2 2
3 50 30
4 30 27
View Code
Output # 1
1 57
View Code
 
Input # 2
1 9998
2 128 233
3 128 2333
4 128 666
View Code
Output # 2
1 18407
View Code
Input # 3
1 9999
2 101 1111
3 1 9999
4 1111 9999
View Code
Output # 3
1 89991
View Code

Description / Tips

Pencil three different packagings are:

  • 2 two loading rate is 2 2;
  • 50 . 5 0 means the rate is 30 . 3 to 0;
  • 30 . 3 0 means the rate is 27 2 7

P teacher will need to purchase at least 57 5 7 pencils.

If she chooses to buy a first package, then she would like to purchase 29 2 9 parts, total 2 \ = Times 29 58 2 × 2 . 9 = . 5 . 8 sticks, the money it takes for 2 \ = Times 29 58 2 × 2 . 9 = 5 8

In fact, P teacher will choose to buy the third package, so you need to buy 2 2 copies. Although the number of available final pencil more, to 30 \ 60 Times = 2 . 3 0 × 2 = . 6 0 sticks, but the cost was reduced to 27 \ 54 is Times 2 = 2 . 7 × 2 = . 5 . 4, than the first less.

For the second package, although Each pencil is the lowest price, but have to buy enough to send 2 2 parts, the actual cost reaches  30 \ Times 2 = 60 . 3 0 × 2 = . 6 0, therefore no teacher P They will choose.

So the answer is that the final output 54 is . 5 4

【Subtasks】

Subtasks will be given part of the characteristics of the test data. If you encounter difficulties to solve the problem, you can try to resolve only part of the test data.

Data size and characteristics of each test point in the following table:

On the table meaning "integer multiples" is: if it is K K, corresponding data indicates the number of pencil required the n- N-pencil set is the number of packages for each integer multiples (which means that you can not necessarily buy a pencil).

answer

  • analysis

Use bit operations to accumulate significant, is the idea of ​​doubling

. 1 I << . 1 is equivalent to * I 2
View Code
  • answer

. 1 #include <bits / STDC ++ H.>
 2  the using  namespace STD;
 . 3  int I, J, K, n-, m, W, ANS;
 . 4  int main ()
 . 5  {
 . 6      Scanf ( " % D " , & n-);
 . 7      for (I = 0 ; I < . 3 ; I ++ )
 . 8      {
 . 9          Scanf ( " % D% D " , & J, & K);
 10          m = J;
 . 11          W = K; // input coexistence initial price and number 
12 is          the while (J <n-)
 13 is          {
 14              J = << . 1 ;
 15              K = << . 1 ; // Price growing number until the number is greater than 2 * n- 
16          }
 . 17          the while (J> n-)
 18 is          {
 . 19              J-= m;
 20 is              - K- W =; // * 2 may lead to too much to buy, minus some 
21 is          }
 22 is          the while (J < n-)
 23 is          {
 24              J + = m;
 25              K = W +; // after subtraction may be too small and, plus some 
26         }
 27          IF (K <|| ANS ANS == 0 )
 28              ANS = K; // determine whether the minimum cost is 
29      }
 30      the printf ( " % D \ n- " , ANS);
 31 is      return  0 ; // output and return 
32 }
View Code

 

Guess you like

Origin www.cnblogs.com/ssf-lrk/p/11263204.html