AtCoder Beginner Contest 084 C

Special Trains 

Problem Statement

A railroad running from west to east in Atcoder Kingdom is now complete.

There are NN stations on the railroad, numbered 11 through NN from west to east.

Tomorrow, the opening ceremony of the railroad will take place.

On this railroad, for each integer ii such that 1iN11≤i≤N−1 , there will be trains that run from Station ii to Station i+1i+1 in CiCi seconds. No other trains will be operated.

The first train from Station ii to Station i+1i+1 will depart Station ii SiSi seconds after the ceremony begins. Thereafter, there will be a train that departs Station ii every FiFi seconds.

Here, it is guaranteed that FiFi divides SiSi .

That is, for each Time tt satisfying SitSi≤t and tFi=0t%Fi=0 , there will be a train that departs Station ii tt seconds after the ceremony begins and arrives at Station i+1i+1 t+Cit+Ci seconds after the ceremony begins, where ABA%B denotes AA modulo BB , and there will be no other trains.

For each ii , find the earliest possible time we can reach Station NN if we are at Station ii when the ceremony begins, ignoring the time needed to change trains.

Constraints

  • 1N5001≤N≤500
  • 1Ci1001≤Ci≤100
  • 1Si1051≤Si≤105
  • 1Fi101≤Fi≤10
  • SiFi=0Si%Fi=0
  • All input values are integers.

Input

Input is given from Standard Input in the following format:

NN


C1C1

 S1S1

 F1F1


::


CN1CN−1

 SN1SN−1

 FN1FN−1


Output

Print NN lines. Assuming that we are at Station ii (1iN)(1≤i≤N) when the ceremony begins, if the earliest possible time we can reach Station NN is xx seconds after the ceremony begins, the ii -th line should contain xx .


Sample Input 1 Copy

Copy
3
6 5 1
1 10 1

Sample Output 1 Copy

Copy
12
11
0

We will travel from Station 11 as follows:

  • 55 seconds after the beginning: take the train to Station 22 .
  • 1111 seconds: arrive at Station 22 .
  • 1111 seconds: take the train to Station 33 .
  • 1212 seconds: arrive at Station 33 .

We will travel from Station 22 as follows:

  • 1010 seconds: take the train to Station 33 .
  • 1111 seconds: arrive at Station 33 .

Note that we should print 00 for Station 33 .


Sample Input 2 Copy

Copy
4
12 24 6
52 16 4
99 2 2

Sample Output 2 Copy

Copy
187
167
101
0

Sample Input 3 Copy

Copy
4
12 13 1
44 17 17
66 4096 64

Sample Output 3 Copy

Copy
4162
4162
4162
0

https://img.atcoder.jp/abc084/editorial.pdf

Fisrt,considering when it is possible to ride a train which goes to station j+1 ,in the situation that arriving station j ,t seconds after the ceremony begin.

・If t < Sj , Sj seconds after the ceremony begin.

・If t ≧ Sj ,but t % Fj = 0 , t seconds after the ceremony begin.

・Othersise, t + Fj −(t % Fj) seconds after the ceremony begin. Considering this,simulate in every case,it would be O(N2) and you can get 300 points.、

 1 #include <cstdio >
 2 int N,C[500],S[500],F[500];
 3 int main()
 4 {
 5     scanf("%d",&N);
 6     for(int i=0; i<N-1; i++)
 7         scanf("%d%d%d",&C[i],&S[i],&F[i]);
 8     for(int i=0; i<N; i++)
 9     {
10         int t=0;
11         for(int j=i; j<N-1; j++)
12         {
13             i
14             f(t<S[j])t=S[j];
15             else if(t%F[j]==0);
16             else t=t+F[j]-t%F[j];
17             t+=C[j];
18         }
19         printf("%d
",t);
20     }
21 }



原文地址:https://www.cnblogs.com/zxhyxiao/p/8151308.html