Skip to content

Commit d20e1ee

Browse files
reststops python
1 parent b432551 commit d20e1ee

File tree

1 file changed

+81
-49
lines changed

1 file changed

+81
-49
lines changed

solutions/usaco-810.mdx

+81-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
id: usaco-810
33
source: USACO Silver 2018 Silver
44
title: Rest Stops
5-
author: Vivian Han
5+
author: Vivian Han, Kevin Sheng
66
---
77

88
## Explanation
@@ -39,88 +39,81 @@ stop early, and our greedy algorithm is correct.
3939
**Time Complexity:** $\mathcal{O}(N)$
4040

4141
<LanguageSection>
42-
4342
<CPPSection>
4443

4544
```cpp
4645
#include <iostream>
4746
#include <fstream>
48-
using namespace std;
47+
#include <vector>
4948

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;
5450

5551
int main() {
56-
int trailLen, stopNum, fRate, bRate;
5752
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++) {
6163
in >> x[i] >> c[i];
6264
}
6365

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) {
6971
// there are no stops after stop i that are "better"
7072
good[i] = true;
71-
max = c[i];
73+
max_tastiness = c[i];
7274
}
7375
}
76+
int total = 0;
77+
for (bool i : good) total += i;
78+
cout << total << endl;
7479

7580
// simulate the whole trail
76-
long long prevStopPos = 0;
81+
long long prev_stop_pos = 0;
7782
long long ans = 0;
78-
for (int i = 0; i < stopNum; i++) {
83+
for (int i = 0; i < stop_num; i++) {
7984
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];
8691
}
8792
}
8893

89-
ofstream out;
90-
out.open("reststops.out");
91-
out << ans << endl;
92-
out.close();
94+
ofstream("reststops.out") << ans << endl;
9395
}
9496
```
9597
9698
</CPPSection>
97-
9899
<JavaSection>
99100
100101
```java
101102
import java.io.*;
102103

103104
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-
110105
public static void main(String[] args) throws IOException{
111106
BufferedReader br = new BufferedReader(new FileReader("reststops.in"));
107+
112108
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
115110
int stopNum = Integer.parseInt(line[1]);
116111
int fRate = Integer.parseInt(line[2]);
117112
int bRate = Integer.parseInt(line[3]);
118113

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+
124117
for (int i = 0 ; i < stopNum; i++) {
125118
line = br.readLine().split(" ");
126119
x[i] = Integer.parseInt(line[0]);
@@ -129,12 +122,13 @@ public class RestStops {
129122
br.close();
130123

131124
// find all "best" stops
132-
int max = 0;
125+
boolean[] good = new boolean[stopNum];
126+
int maxTastiness = 0;
133127
for (int i = stopNum-1; i >= 0; i--) {
134-
if (c[i] > max) {
128+
if (c[i] > maxTastiness) {
135129
// there are no stops after stop i that are "better"
136130
good[i] = true;
137-
max = c[i];
131+
maxTastiness = c[i];
138132
}
139133
}
140134

@@ -152,13 +146,51 @@ public class RestStops {
152146
}
153147
}
154148

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();
158152
}
159153
}
160154
```
161155
162156
</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+
```
163194
195+
</PySection>
164196
</LanguageSection>

0 commit comments

Comments
 (0)