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