[USACO09MAR] Moon moo moo Moon Mooing (analog)

 

 

Links: https://ac.nowcoder.com/acm/contest/1086/F
Source: Cattle-off network

Title Description

A full moon casts some sort of spell on the cows and, like their cousins the wolves and coyotes, they bay at the moon -- mooing instead of howling, of course.
Each 'moo' lasts a certain amount of time. A short 'moo' might last time 1; a longer one might last time 24 or even 1,000,000,000 or longer (cows can really moo when they want to). No 'moo' will last more than or equal to 2^63.
It should come as no surprise that the cows have a pattern to their moos.  Bessie will choose an integer c (1 <= c <= 100) that is the initial length of a moo.
After Bessie moos for length c, the cows calculate times for subsequent moos. They apply two formulae to each moo time to yield even more moo times. The two formulae are:
        f1(c)=a1*c/d1+b1 (integer divide, of course) and
        f2(c)=a2*c/d2+b2.
They then successively use the two new times created by evaluating f1(c) and f2(c) to create even more mooing times. They keep a sorted list of all the possible mooing times (discarding duplicates).
They are allowed to moo a total of N times (1 <= N <= 4,000,000). Please determine the length of the longest moo before they must quit.
The constants in the formulae have these constraints: 1 <= d1 < a1; d1 < a1 <= 20; 0 <= b1 <= 20; 1 <= d2 < a2; d2 < a2 <= 20; 0 <= b2 <= 20.
Consider an example where c=3 and N=10. The constants are:
    a1=4    b1=3     d1=3
    a2=17   b2=8     d2=2
The first mooing time is 3, given by the value of c. The total list of mooing times is:
     1. c=3             ->  3       6. f2(3)=17*3/2+8  -> 33
     2. f1(3)=4*3/3+3   ->  7       7. f1(28)=4*28/3+3 -> 40
     3. f1(7)=4*7/3+3   -> 12       8. f1(33)=4*33/3+3 -> 47
     4. f1(12)=4*12/3+3 -> 19       9. f1(40)=4*40/3+3 -> 56
     5. f1(19)=4*19/3+3 -> 28      10. f1(47)=4*47/3+3 -> 65
The tenth time is 65, which would be the proper answer for this set of inputs.

Enter a description:

* Line 1: Two space-separated integers: c and N
* Line 2: Three space-separated integers: a1, b1, and d1
* Line 3: Three space-separated integers: a2, b2, and d2

Output Description:

* Line 1: A single line which contains a single integer which is the length of the Nth moo

Example 1

Entry

3 10 
4 3 3 
17 8 2 

Export

65

 

A similar sequence of merging two things, through law is a priority queue just fine, but this question will time out.

However, a closer look, the sequence is a single-increasing (a> c), which do well, similar to a merger do just fine.

 

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <string>
 5 #include <math.h>
 6 #include <algorithm>
 7 #include <vector>
 8 #include <queue>
 9 #include <set>
10 #include <map>
11 #include <math.h>
12 const int INF=0x3f3f3f3f;
13 typedef long long LL;
14 const intMOD = 1E9 + . 7 ;
 15  const  int MAXN = . 4 * 1e6 + 10 ;
 16  the using  namespace STD;
 . 17  
18 is LL ANS [MAXN]; // ANS array is an increasing queue 
. 19  
20 is  int main ()
 21 is  {
 22 is      int C, n-, A1, B1, D1, A2, B2, D2;
 23 is      Scanf ( " % D% D% D% D% D% D% D% D " , & C, & n-, & A1, & B1, & D1, & A2, & B2, & D2 );
 24      int CNT = . 1 ; // queue counter 
25      int F1 = . 1, F2 = . 1 ; // function counter 
26 is      ANS [CNT ++] = c; // initial element c enqueued 
27      the while (CNT <= n-)
 28      {
 29          LL T min = (A1 * ANS [F1] / D1 + B1 , A2 * ANS [F2] / B2 + D2); // smaller value () was taken Fl () and F2 of 
30          ANS [CNT ++] = T; // the small value enqueued 
31 is          IF (T = ANS A1 * = [F1] / D1 + B1) // If the smaller value from the F1 (), then Fl () +. 1 pointer F1 
32              F1 ++ ;
 33 is          IF (T == A2 * ANS [F2] / D2 B2 +) // If a smaller value from F2 (), then F2 of () +. 1 pointer F2 
34 is              F2 ++ ;
 35      }// important to understand the contents of the while loop. Fl calculated as () or F2 of () monotonically increasing data, can be generated in such a manner the entire series 
36      the printf ( " % LLD " , ANS [n-]); // final output can. 
37 [      return  0 ;
 38 is }

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/jiamian/p/11494876.html