|
2 | 2 | id: usaco-713
|
3 | 3 | source: USACO Bronze 2017 February
|
4 | 4 | title: Why Did the Cow Cross the Road III
|
5 |
| -author: Óscar Garries, Ryan Chou |
| 5 | +author: Óscar Garries, Ryan Chou, Varun Ragunath |
6 | 6 | ---
|
7 | 7 |
|
8 | 8 | [Official Analysis (Java)](http://www.usaco.org/current/data/sol_cowqueue_bronze_feb17.html)
|
9 | 9 |
|
| 10 | +## Video Solution |
| 11 | + |
| 12 | +By Varun Ragunath |
| 13 | + |
| 14 | +<Youtube id="O7r-Tp9vFGQ" /> |
| 15 | + |
| 16 | +<Spoiler title="Video Solution Code"> |
| 17 | +<LanguageSection> |
| 18 | +<CPPSection> |
| 19 | + |
| 20 | +```cpp |
| 21 | +#include<bits/stdc++.h> |
| 22 | +using namespace std; |
| 23 | + |
| 24 | +int main() { |
| 25 | + freopen("cowqueue.in", "r", stdin); |
| 26 | + freopen("cowqueue.out", "w", stdout); |
| 27 | + cin.sync_with_stdio(0); cin.tie(0); |
| 28 | + |
| 29 | + // get input |
| 30 | + // number of cows, information for each cow |
| 31 | + |
| 32 | + int n; |
| 33 | + cin >> n; |
| 34 | + |
| 35 | + vector<pair<int, int>> cows(n); |
| 36 | + |
| 37 | + for(int i = 0; i < n; i++) { |
| 38 | + cin >> cows[i].first >> cows[i].second; |
| 39 | + } |
| 40 | + |
| 41 | + // now we need to determine the optimal ordering |
| 42 | + // obviously sorting is the "best" and only way to sort the cows |
| 43 | + |
| 44 | + // to sort the cows we can either use a library implementation or bubble sort |
| 45 | + |
| 46 | + // here we will implement bubble sort |
| 47 | + |
| 48 | + bool swapped = true; |
| 49 | + |
| 50 | + while(swapped) { |
| 51 | + swapped = false; |
| 52 | + for(int i = 0; i < n - 1; i++) { |
| 53 | + if(cows[i] > cows[i + 1]) { |
| 54 | + swap(cows[i], cows[i + 1]); |
| 55 | + swapped = true; |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + // now that we have sorted the array, it is time to simulate the process |
| 61 | + |
| 62 | + int cur_time = 0; // stores the current time |
| 63 | + |
| 64 | + for(int i = 0; i < n; i++) { |
| 65 | + // if the time the current cow we are processing has already passed |
| 66 | + // then we need to update their time to the current time |
| 67 | + cows[i].first = max(cows[i].first, cur_time); |
| 68 | + // now we need to calculate when they will be able to leave the questioning |
| 69 | + cur_time = cows[i].first + cows[i].second; |
| 70 | + // cur_time now holds the amount of time needed to process the first i cows |
| 71 | + } |
| 72 | + |
| 73 | + cout << cur_time << '\n'; |
| 74 | + |
| 75 | + return 0; |
| 76 | +} |
| 77 | +``` |
| 78 | + |
| 79 | +</CPPSection> |
| 80 | +<JavaSection> |
| 81 | + |
| 82 | +```java |
| 83 | +import java.io.*; |
| 84 | +import java.util.*; |
| 85 | + |
| 86 | +public class cowqueue{ |
| 87 | + public static void main(String[] args) throws IOException{ |
| 88 | + cowqueue.Kattio io = new cowqueue.Kattio("cowqueue"); |
| 89 | + |
| 90 | + // getting input |
| 91 | + // number of cows, information for each cow |
| 92 | + int n = io.nextInt(); |
| 93 | + int[][] cows = new int[n][2]; |
| 94 | + |
| 95 | + for(int i = 0; i < n; i++) { |
| 96 | + for(int j = 0; j < 2; j++) { |
| 97 | + cows[i][j] = io.nextInt(); |
| 98 | + } |
| 99 | + } |
| 100 | + |
| 101 | + // we need to generate the optimal ordering of cows |
| 102 | + // clearly the optimal ordering will be to just sort the cows by arriving time |
| 103 | + // we can do bubble sort |
| 104 | + |
| 105 | + boolean swapped = true; |
| 106 | + |
| 107 | + while(swapped) { |
| 108 | + swapped = false; |
| 109 | + for(int i = 0; i < n - 1; i++) { |
| 110 | + if(cows[i][0] > cows[i + 1][0]) { |
| 111 | + // how do you write a swap in java???? |
| 112 | + int ext = cows[i][0]; |
| 113 | + cows[i][0] = cows[i + 1][0]; |
| 114 | + cows[i + 1][0] = ext; |
| 115 | + |
| 116 | + ext = cows[i][1]; |
| 117 | + cows[i][1] = cows[i + 1][1]; |
| 118 | + cows[i + 1][1] = ext; |
| 119 | + |
| 120 | + swapped = true; |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + // now it is time to simulate the process |
| 126 | + int cur_time = 0; // stores the current time |
| 127 | + |
| 128 | + for(int i = 0; i < n; i++) { |
| 129 | + // update the time cow[i] starts questioning |
| 130 | + cows[i][0] = Math.max(cur_time, cows[i][0]); |
| 131 | + // now that we have the time ith cow enters the queue, |
| 132 | + // lets calculate when it leaves the queue |
| 133 | + cur_time = (cows[i][0] + cows[i][1]); |
| 134 | + } |
| 135 | + |
| 136 | + io.println(cur_time); |
| 137 | + io.close(); |
| 138 | + } |
| 139 | + |
| 140 | + //CodeSnip{Kattio} |
| 141 | +} |
| 142 | +``` |
| 143 | +</JavaSection> |
| 144 | +</LanguageSection> |
| 145 | +</Spoiler> |
| 146 | + |
10 | 147 | Since the time the cows arrive and need for questioning can be up to $10^6$, we should traverse through the times as steps.
|
11 | 148 |
|
12 | 149 | ## Implementation
|
|
0 commit comments