2
2
id : usaco-810
3
3
source : USACO Silver 2018 Silver
4
4
title : Rest Stops
5
- author : Vivian Han
5
+ author : Vivian Han, Kevin Sheng
6
6
---
7
7
8
+ [ Official Analysis (C++)] ( http://www.usaco.org/current/data/sol_reststops_silver_feb18.html )
9
+
8
10
## Explanation
9
11
10
12
Suppose that early in the hike, there is a rest stop with tastiness $A$,
@@ -39,88 +41,81 @@ stop early, and our greedy algorithm is correct.
39
41
** Time Complexity:** $\mathcal{ O } (N)$
40
42
41
43
<LanguageSection >
42
-
43
44
<CPPSection >
44
45
45
46
``` cpp
46
47
#include < iostream>
47
48
#include < fstream>
48
- using namespace std ;
49
+ # include < vector >
49
50
50
- const int MAX_LEN = 100000 ;
51
- int x[MAX_LEN]; // x: position of each stop
52
- int c[MAX_LEN]; // c: tastiness value of each stop
53
- bool good[MAX_LEN];
51
+ using namespace std ;
54
52
55
53
int main () {
56
- int trailLen, stopNum, fRate, bRate;
57
54
ifstream in("reststops.in");
58
- in >> trailLen >> stopNum >> fRate >> bRate;
59
- // scan in rest stop data
60
- for (int i = 0 ; i < stopNum; i++) {
55
+
56
+ int trail_len; // not used
57
+ int stop_num;
58
+ int f_rate;
59
+ int b_rate;
60
+ in >> trail_len >> stop_num >> f_rate >> b_rate;
61
+
62
+ vector<int > x(stop_num); // position of each stop
63
+ vector<int > c(stop_num); // tastiness value of each stop
64
+ for (int i = 0 ; i < stop_num; i++) {
61
65
in >> x [i ] >> c [i ];
62
66
}
63
67
64
- // find all "best" stops
65
- int max = 0 ;
66
- for ( int i = stopNum-1; i >= 0; i--) {
67
- good [ i ] = false ;
68
- if (c [i ] > max ) {
68
+ // find all the "best" stops
69
+ vector< bool > good(stop_num) ;
70
+ int max_tastiness = 0;
71
+ for (int i = stop_num - 1; i >= 0; i--) {
72
+ if (c [i ] > max_tastiness ) {
69
73
// there are no stops after stop i that are "better"
70
74
good[i ] = true;
71
- max = c[i ];
75
+ max_tastiness = c[i ];
72
76
}
73
77
}
78
+ int total = 0;
79
+ for (bool i : good ) total += i ;
80
+ cout << total << endl ;
74
81
75
82
// simulate the whole trail
76
- long long prevStopPos = 0;
83
+ long long prev_stop_pos = 0 ;
77
84
long long ans = 0 ;
78
- for (int i = 0; i < stopNum ; i++) {
85
+ for (int i = 0 ; i < stop_num ; i ++ ) {
79
86
if (good [i ]) {
80
- long long travelDist = x[i ] - prevStopPos ;
81
- long long fTime = travelDist * fRate ;
82
- long long bTime = travelDist * bRate ;
83
- long long restTime = fTime - bTime ;
84
- ans += restTime * c[i ];
85
- prevStopPos = x[i ];
87
+ long long travel_dist = x [i ] - prev_stop_pos ;
88
+ long long f_time = travel_dist * f_rate ;
89
+ long long b_time = travel_dist * b_rate ;
90
+ long long rest_time = f_time - b_time ;
91
+ ans += rest_time * c [i ];
92
+ prev_stop_pos = x [i ];
86
93
}
87
94
}
88
95
89
- ofstream out;
90
- out.open("reststops.out");
91
- out << ans << endl;
92
- out.close();
96
+ ofstream (" reststops.out" ) << ans << endl ;
93
97
}
94
98
` ` `
95
99
96
100
</CPPSection>
97
-
98
101
<JavaSection>
99
102
100
103
` ` ` java
101
104
import java .io .* ;
102
105
103
106
public class RestStops {
104
-
105
- static int[] x, c;
106
- // x: position of each stop
107
- // c: tastiness value of each stop
108
- static boolean[] good;
109
-
110
107
public static void main(String [] args ) throws IOException{
111
108
BufferedReader br = new BufferedReader (new FileReader (" reststops.in" ));
109
+
112
110
String [] line = br .readLine ().split (" " );
113
- int trailLen = Integer.parseInt(line[0 ]);
114
- // trail length is never used...
111
+ int trailLen = Integer .parseInt (line [0 ]); // never used
115
112
int stopNum = Integer .parseInt (line [1 ]);
116
113
int fRate = Integer .parseInt (line [2 ]);
117
114
int bRate = Integer .parseInt (line [3 ]);
118
115
119
- x = new int[stopNum ];
120
- c = new int[stopNum ];
121
- good = new boolean[stopNum ];
122
-
123
- // scan in rest stop data
116
+ int [] x = new int [stopNum ]; // position of each stop
117
+ int [] c = new int [stopNum ]; // tastiness value of each stop
118
+
124
119
for (int i = 0 ; i < stopNum ; i ++ ) {
125
120
line = br .readLine ().split (" " );
126
121
x [i ] = Integer .parseInt (line [0 ]);
@@ -129,12 +124,13 @@ public class RestStops {
129
124
br .close ();
130
125
131
126
// find all "best" stops
132
- int max = 0;
127
+ boolean [] good = new boolean [stopNum ];
128
+ int maxTastiness = 0 ;
133
129
for (int i = stopNum - 1 ; i >= 0 ; i -- ) {
134
- if (c[i ] > max ) {
130
+ if (c [i ] > maxTastiness ) {
135
131
// there are no stops after stop i that are "better"
136
132
good [i ] = true ;
137
- max = c[i ];
133
+ maxTastiness = c [i ];
138
134
}
139
135
}
140
136
@@ -152,13 +148,51 @@ public class RestStops {
152
148
}
153
149
}
154
150
155
- BufferedWriter bw = new BufferedWriter(new FileWriter( "reststops.out") );
156
- bw.write (ans + "\n" );
157
- bw .close();
151
+ PrintWriter pw = new PrintWriter ( " reststops.out" );
152
+ pw . println (ans );
153
+ pw .close ();
158
154
}
159
155
}
160
156
` ` `
161
157
162
158
</JavaSection>
159
+ <PySection>
160
+
161
+ ` ` ` py
162
+ with open (' reststops.in' ) as read :
163
+ # trail_len won ' t be used
164
+ trail_len , stop_num , f_rate , b_rate = [int (i ) for i in read .readline ().split ()]
165
+
166
+ x = [] # position of each stop
167
+ c = [] # tastiness value of each stop
168
+ for _ in range (stop_num ):
169
+ a , b = [int (i ) for i in read .readline ().split ()]
170
+ x .append (a )
171
+ c .append (b )
172
+
173
+ # find all the " best" stops
174
+ good = [False for _ in range (stop_num )]
175
+ max_tastiness = 0
176
+ for i in range (stop_num - 1 , - 1 , - 1 ):
177
+ if c [i ] > max_tastiness :
178
+ # there are no stops after stop i that are " better"
179
+ good [i ] = True
180
+ max_tastiness = c [i ]
181
+
182
+ # simulate the whole trail
183
+ prev_stop_pos = 0
184
+ ans = 0
185
+ for i in range (stop_num ):
186
+ if good [i ]:
187
+ travel_dist = x [i ] - prev_stop_pos
188
+ f_time = travel_dist * f_rate
189
+ b_time = travel_dist * b_rate
190
+ rest_time = f_time - b_time
191
+ ans += rest_time * c [i ]
192
+ prev_stop_pos = x [i ]
193
+
194
+ print (ans , file = open (' reststops.out' , ' w' ))
195
+ ` ` `
163
196
197
+ </PySection>
164
198
</LanguageSection>
0 commit comments