Skip to content

Commit 3771b45

Browse files
Merge pull request #3147 from SansPapyrus683/reststops
reststops python
2 parents 7a8e0f4 + da634cd commit 3771b45

File tree

1 file changed

+83
-49
lines changed

1 file changed

+83
-49
lines changed

solutions/usaco-810.mdx

+83-49
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
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

8+
[Official Analysis (C++)](http://www.usaco.org/current/data/sol_reststops_silver_feb18.html)
9+
810
## Explanation
911

1012
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.
3941
**Time Complexity:** $\mathcal{O}(N)$
4042

4143
<LanguageSection>
42-
4344
<CPPSection>
4445

4546
```cpp
4647
#include <iostream>
4748
#include <fstream>
48-
using namespace std;
49+
#include <vector>
4950

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

5553
int main() {
56-
int trailLen, stopNum, fRate, bRate;
5754
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++) {
6165
in >> x[i] >> c[i];
6266
}
6367

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) {
6973
// there are no stops after stop i that are "better"
7074
good[i] = true;
71-
max = c[i];
75+
max_tastiness = c[i];
7276
}
7377
}
78+
int total = 0;
79+
for (bool i : good) total += i;
80+
cout << total << endl;
7481

7582
// simulate the whole trail
76-
long long prevStopPos = 0;
83+
long long prev_stop_pos = 0;
7784
long long ans = 0;
78-
for (int i = 0; i < stopNum; i++) {
85+
for (int i = 0; i < stop_num; i++) {
7986
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];
8693
}
8794
}
8895

89-
ofstream out;
90-
out.open("reststops.out");
91-
out << ans << endl;
92-
out.close();
96+
ofstream("reststops.out") << ans << endl;
9397
}
9498
```
9599
96100
</CPPSection>
97-
98101
<JavaSection>
99102
100103
```java
101104
import java.io.*;
102105

103106
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-
110107
public static void main(String[] args) throws IOException{
111108
BufferedReader br = new BufferedReader(new FileReader("reststops.in"));
109+
112110
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
115112
int stopNum = Integer.parseInt(line[1]);
116113
int fRate = Integer.parseInt(line[2]);
117114
int bRate = Integer.parseInt(line[3]);
118115

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+
124119
for (int i = 0 ; i < stopNum; i++) {
125120
line = br.readLine().split(" ");
126121
x[i] = Integer.parseInt(line[0]);
@@ -129,12 +124,13 @@ public class RestStops {
129124
br.close();
130125

131126
// find all "best" stops
132-
int max = 0;
127+
boolean[] good = new boolean[stopNum];
128+
int maxTastiness = 0;
133129
for (int i = stopNum-1; i >= 0; i--) {
134-
if (c[i] > max) {
130+
if (c[i] > maxTastiness) {
135131
// there are no stops after stop i that are "better"
136132
good[i] = true;
137-
max = c[i];
133+
maxTastiness = c[i];
138134
}
139135
}
140136

@@ -152,13 +148,51 @@ public class RestStops {
152148
}
153149
}
154150

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();
158154
}
159155
}
160156
```
161157
162158
</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+
```
163196
197+
</PySection>
164198
</LanguageSection>

0 commit comments

Comments
 (0)