├── IOI ├── 2016 │ ├── Day 2 │ │ └── Aliens.cpp │ └── README.md └── 2018 │ ├── Day 1 │ └── combo.cpp │ └── README.md ├── Implementations ├── Algorithms │ ├── Array │ │ └── MergeSortInversionCount.cpp │ ├── Graph │ │ ├── Flow │ │ │ ├── Dinics.cpp │ │ │ ├── Edmonds-Karp.cpp │ │ │ ├── Ford-Fulkerson.cpp │ │ │ ├── Hopcroft-Karp.cpp │ │ │ ├── MinCostFlow.cpp │ │ │ ├── Push-Relabel │ │ │ │ ├── FIFO + gap.cpp │ │ │ │ ├── highest-label + gap.cpp │ │ │ │ └── relabel-to-front + gap.cpp │ │ │ └── ScalingFlow.cpp │ │ ├── HLD.cpp │ │ ├── Tarjan-LCA.cpp │ │ └── TopoSort.cpp │ ├── Math │ │ ├── FFT.cpp │ │ ├── NTT.cpp │ │ └── StrassensAlgorithm.cpp │ └── String │ │ ├── Aho-Corasick.cpp │ │ ├── KMP.cpp │ │ ├── KMP_LEA.cpp │ │ ├── Manachers.cpp │ │ └── Z.cpp ├── Data Structures │ ├── Geometry │ │ └── ConvexHull.cpp │ ├── Graphs │ │ ├── BiconnectedComponents.cpp │ │ ├── DisjointUnion.cpp │ │ ├── LinkCutTree.cpp │ │ └── SizeRecordingDisjointUnion.cpp │ ├── Lists │ │ └── LinkedList.cpp │ ├── Math │ │ ├── BigInt.cpp │ │ ├── Matrix.cpp │ │ ├── NTTPolynomial.cpp │ │ └── Polynomial.cpp │ ├── Policy Based │ │ └── splay_tree.cpp │ ├── Range Queries │ │ ├── Binary Indexed Trees │ │ │ ├── BIT.cpp │ │ │ └── RUSQ.cpp │ │ ├── Segment Trees │ │ │ ├── LazyMaxSegmentTree.cpp │ │ │ ├── LazyMinSegmentTree.cpp │ │ │ ├── LazySegmentTree.cpp │ │ │ ├── MergeSortTree.cpp │ │ │ └── SegmentTree.cpp │ │ └── Static Queries │ │ │ ├── RangeMaxQuery.cpp │ │ │ └── RangeMinQuery.cpp │ ├── Strings │ │ ├── StringTokenizer.cpp │ │ └── SuffixArray.cpp │ └── Trees │ │ ├── PersistentBitMultiset.cpp │ │ └── PersistentTrieMap.cpp └── Templates │ ├── CodeForces.cpp │ ├── MacOS.cpp │ ├── OP_IO.cpp │ └── USACO.cpp ├── README.md ├── USACO Training Pages └── README.md └── USACO ├── 2013-2014 Season ├── December │ └── Gold │ │ ├── optmilk.cpp │ │ ├── shufflegold.cpp │ │ └── vacationgold.cpp ├── February │ └── Gold │ │ ├── boarding.cpp │ │ ├── dec.cpp │ │ └── rblock.cpp ├── January │ └── Gold │ │ ├── curling.cpp │ │ ├── skicourse.cpp │ │ └── skilevel.cpp ├── March │ └── Gold │ │ ├── fcount.cpp │ │ ├── lazy.cpp │ │ └── sabotage.cpp └── November │ └── Gold │ ├── empty.cpp │ ├── nochange.cpp │ └── sight.cpp ├── 2014-2015 Season ├── December │ ├── Gold │ │ ├── cowjog.cpp │ │ ├── guard.cpp │ │ └── marathon.cpp │ └── README.md ├── February │ ├── Bronze │ │ ├── censor.cpp │ │ ├── cow.cpp │ │ └── hopscotch.cpp │ ├── Gold │ │ ├── censor.cpp │ │ ├── fencing.cpp │ │ └── hopscotch.cpp │ ├── README.md │ └── Silver │ │ ├── censor.cpp │ │ ├── hopscotch.cpp │ │ └── superbull.cpp ├── January │ ├── Gold │ │ ├── cowrect.cpp │ │ ├── grass.cpp │ │ └── movie.cpp │ ├── README.md │ └── Silver │ │ ├── cowroute.cpp │ │ ├── meeting.cpp │ │ └── stampede.cpp ├── README.md └── US Open │ ├── Bronze │ ├── geteven.cpp │ ├── moocrypt.cpp │ ├── palpath.cpp │ └── trapped.cpp │ ├── Gold │ ├── googol.cpp │ ├── palpath.cpp │ ├── trapped(RMQ + bin search).cpp │ └── trapped(range marking).cpp │ ├── README.md │ └── SIlver │ ├── bgm.cpp │ ├── buffet.cpp │ └── trapped.cpp ├── 2015-2016 Season ├── December │ ├── Bronze │ │ ├── badmilk.cpp │ │ ├── paint.cpp │ │ └── speeding.cpp │ ├── Gold │ │ ├── cardgame.cpp │ │ ├── dream.cpp │ │ └── feast.cpp │ ├── Platinum │ │ ├── cardgame.cpp │ │ ├── haybales.cpp │ │ └── maxflow.cpp │ ├── README.md │ └── Silver │ │ ├── bcount.cpp │ │ ├── highcard.cpp │ │ └── lightson.cpp ├── February │ ├── Bronze │ │ ├── balancing.cpp │ │ ├── cbarn.cpp │ │ └── pails.cpp │ ├── Gold │ │ ├── cbarn.cpp │ │ ├── cbarn2.cpp │ │ └── fencedin.cpp │ ├── Platinum │ │ ├── balancing.cpp │ │ ├── cbarn(convex hull).cpp │ │ ├── cbarn(divide and conquer).cpp │ │ └── fencedin.cpp │ ├── README.md │ └── Silver │ │ ├── balancing.cpp │ │ ├── cbarn.cpp │ │ └── pails.cpp ├── January │ ├── Bronze │ │ ├── angry.cpp │ │ ├── mowing.cpp │ │ └── promote.cpp │ ├── Gold │ │ ├── angry.cpp │ │ ├── lightsout.cpp │ │ └── radio.cpp │ ├── Platinum │ │ ├── fortmoo.cpp │ │ ├── lightsout.cpp │ │ └── mowing.cpp │ ├── README.md │ └── Silver │ │ ├── angry.cpp │ │ ├── div7.cpp │ │ └── gates.cpp └── US Open │ ├── Bronze │ ├── bcs.cpp │ ├── diamond.cpp │ └── reduce.cpp │ ├── Gold │ ├── 248.cpp │ ├── closing.cpp │ └── split.cpp │ ├── Platinum │ ├── 262144.cpp │ ├── bcs.cpp │ └── landscaping.cpp │ ├── README.md │ └── Silver │ ├── closing.cpp │ ├── diamond.cpp │ └── reduce.cpp ├── 2016-2017 Season ├── December │ ├── Bronze │ │ ├── blocks.java │ │ ├── cowsignal.java │ │ └── square.java │ ├── Gold │ │ ├── checklist.java │ │ ├── lasers.java │ │ └── moocast.java │ ├── Platinum │ │ ├── roboherd.cpp │ │ ├── team.cpp │ │ └── triangles.cpp │ ├── README.md │ └── Silver │ │ ├── citystate.java │ │ ├── haybales.java │ │ └── moocast.cpp ├── February │ ├── Bronze │ │ ├── circlecross.cpp │ │ ├── cowqueue.cpp │ │ └── crossroad.cpp │ ├── Gold │ │ ├── circlecross.cpp │ │ ├── nocross.cpp │ │ └── visitfj.cpp │ ├── Platinum │ │ ├── friendcross.cpp │ │ ├── mincross.java │ │ └── nocross.java │ ├── README.md │ └── Silver │ │ ├── countcross.cpp │ │ ├── helpcross.cpp │ │ └── maxcross.cpp ├── January │ ├── Bronze │ │ ├── cowtip.cpp │ │ ├── hps.cpp │ │ └── notlast.cpp │ ├── Gold │ │ ├── bphoto.cpp │ │ ├── cownav.cpp │ │ └── hps.cpp │ ├── Platinum │ │ ├── promote.cpp │ │ ├── subrev.cpp │ │ └── tallbarn.cpp │ ├── README.md │ └── Silver │ │ ├── cowcode.cpp │ │ ├── cowdance.cpp │ │ └── hps.cpp └── US Open │ ├── Bronze │ ├── art.cpp │ ├── cownomics.cpp │ └── lostcow.cpp │ ├── Gold │ ├── art2.cpp │ └── cownomics.cpp │ ├── Platinum │ ├── art.cpp │ ├── cowbasic.cpp │ ├── cowbasic.java │ └── grass.cpp │ ├── README.md │ └── Silver │ ├── cownomics.cpp │ ├── pairup.cpp │ └── where.cpp ├── 2017-2018 Season ├── December │ ├── Bronze │ │ ├── billboard.cpp │ │ ├── measurement.cpp │ │ └── shuffle.cpp │ ├── Gold │ │ ├── barnpainting.cpp │ │ ├── hayfeast.cpp │ │ └── piepie.cpp │ ├── Platinum │ │ ├── greedy.cpp │ │ ├── pushabox.cpp │ │ ├── standingout(hashing).cpp │ │ └── standingout(suffix array).cpp │ ├── README.md │ └── Silver │ │ ├── homework.cpp │ │ ├── measurement.cpp │ │ └── shuffle.cpp ├── February │ ├── Bronze │ │ ├── hoofball.cpp │ │ ├── taming.cpp │ │ └── teleport.cpp │ ├── Gold │ │ ├── dirtraverse.cpp │ │ ├── snowboots.cpp │ │ └── taming.cpp │ ├── Platinum │ │ ├── gymnasts.cpp │ │ ├── newbarn.cpp │ │ └── slingshot.cpp │ ├── README.md │ └── Silver │ │ ├── reststops.cpp │ │ ├── snowboots.cpp │ │ └── teleport.cpp ├── January │ ├── Bronze │ │ ├── billboard.cpp │ │ ├── lifeguards.cpp │ │ └── outofplace.cpp │ ├── Gold │ │ ├── atlarge.cpp │ │ ├── mootube.cpp │ │ └── spainting.cpp │ ├── Platinum │ │ ├── atlarge.cpp │ │ ├── lifeguards.cpp │ │ └── sprinklers.cpp │ ├── README.md │ └── Silver │ │ ├── lifeguards.cpp │ │ ├── mootube.cpp │ │ └── rental.cpp └── US Open │ ├── Bronze │ ├── family.cpp │ ├── milkorder.cpp │ └── tttt.cpp │ ├── Gold │ ├── milkorder.cpp │ ├── sort.cpp │ └── talent.cpp │ ├── Platinum │ ├── disruption(HLD).cpp │ ├── disruption(set).cpp │ ├── sort.cpp │ └── train.cpp │ ├── README.md │ └── Silver │ ├── lemonade.cpp │ ├── multimoo.cpp │ └── sort.cpp ├── 2018-2019 Season ├── December │ ├── Gold │ │ ├── cowpatibility.cpp │ │ ├── dining.cpp │ │ └── teamwork.cpp │ ├── Platinum │ │ ├── balance.cpp │ │ ├── gathering.cpp │ │ └── itout.cpp │ └── README.md ├── February │ ├── Gold │ │ ├── cowland.cpp │ │ ├── dishes.cpp │ │ └── paintbarn.cpp │ ├── Platinum │ │ ├── cowdate.cpp │ │ ├── mooriokart.cpp │ │ └── mowing.cpp │ └── README.md ├── January │ ├── Gold │ │ ├── poetry.cpp │ │ ├── shortcut.cpp │ │ └── sleepy.cpp │ ├── Platinum │ │ ├── exercise.cpp │ │ ├── redistricting.cpp │ │ └── tracking2.cpp │ └── README.md └── US Open │ ├── Gold │ ├── balance.cpp │ ├── snakes.cpp │ └── walk.cpp │ └── Platinum │ ├── escape.cpp │ ├── treeboxes.cpp │ └── valleys.cpp ├── 2019-2020 Season ├── December │ └── Platinum │ │ ├── pieaters.cpp │ │ ├── snowcow.cpp │ │ └── treedepth.cpp ├── February │ └── Platinum │ │ ├── deleg.cpp │ │ ├── help.cpp │ │ └── triangles.cpp ├── January │ └── Platinum │ │ ├── cave.cpp │ │ ├── falling.cpp │ │ └── nondec.cpp └── US Open │ └── Platinum │ ├── circus.cpp │ ├── exercise.cpp │ └── sprinklers2.cpp └── README.md /IOI/2016/README.md: -------------------------------------------------------------------------------- 1 | # Day 0 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Mini Tetris | Unsolved | Missing | 5 | | Reversing a Sequence | Unsolved | Missing | 6 | | Laugh Analysis | Unsolved | Missing | 7 | | Martian DNA | Unsolved | Missing | 8 | # Day 1 9 | | Problem | State | Implementation | 10 | | ------------- |:---------------:| :--------------:| 11 | | Detecting Molecules | Unsolved | Missing | 12 | | Roller Coaster Railroad | Unsolved | Missing | 13 | | Shortcut | Unsolved | Missing | 14 | # Day 2 15 | | Problem | State | Implementation | 16 | | ------------- |:---------------:| :--------------:| 17 | | Paint By Numbers | Unsolved | Missing | 18 | | Unscrambling a Messy Bug | Unsolved | Missing | 19 | | Aliens | Solved | C++ | 20 | -------------------------------------------------------------------------------- /IOI/2018/README.md: -------------------------------------------------------------------------------- 1 | # Day 0 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Bubble Sort 2 | Unsolved | Missing | 5 | | Road Service | Unsolved | Missing | 6 | | Timecard | Unsolved | Missing | 7 | | Xylophone | Unsolved | Missing | 8 | # Day 1 9 | | Problem | State | Implementation | 10 | | ------------- |:---------------:| :--------------:| 11 | | Combo | Solved | C++ | 12 | | Seats | Unsolved | Missing | 13 | | Werewolf | Unsolved | Missing | 14 | # Day 2 15 | | Problem | State | Implementation | 16 | | ------------- |:---------------:| :--------------:| 17 | | Mechanical Doll | Unsolved | Missing | 18 | | Highway Tolls | Unsolved | Missing | 19 | | Meetings | Unsolved | Missing | 20 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Array/MergeSortInversionCount.cpp: -------------------------------------------------------------------------------- 1 | int msInversion(int *a, int lo, int hi) { 2 | if(hi <= lo + 1) return 0; 3 | int mid = (lo + hi) >> 1; 4 | int res = msInversion(a, lo, mid) + msInversion(a, mid, hi); 5 | int *L = new int[mid - lo], *R = new int[hi - mid]; 6 | copy(a + lo, a + mid, L), copy(a + mid, a + hi, R); 7 | int i = 0, j = 0; 8 | while(i < mid - lo || j < hi - mid) { 9 | if(j == hi - mid || i < mid - lo && L[i] <= R[j]) { 10 | res += j; 11 | a[lo + i + j] = L[i++]; 12 | } else a[lo + i + j] = R[j++]; 13 | } 14 | return res; 15 | } 16 | 17 | int countInversions(int *a, int sz) { 18 | int *b = new int[sz]; 19 | copy(a, a + sz, b); 20 | return msInversion(b, 0, sz); 21 | } 22 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Edmonds-Karp.cpp: -------------------------------------------------------------------------------- 1 | int n, m, prec[MAXN]; 2 | bool vis[MAXN]; 3 | map adj[MAXN]; 4 | 5 | ll edkarp(int s, int t) { 6 | ll flow = 0; 7 | while(1) { 8 | memset(vis, 0, n * sizeof(bool)); 9 | queue> q; 10 | q.push(MP(s, ~(1LL << 63))); vis[0] = 1; 11 | ll maxFlow = 0; 12 | while(!q.empty()) { 13 | pair p = q.front(); q.pop(); 14 | if(p.F == t) { 15 | maxFlow = p.S; 16 | break; 17 | } 18 | for(const pair next : adj[p.F]) if(!vis[next.F]) { 19 | vis[next.F] = 1; 20 | prec[next.F] = p.F; 21 | q.push(MP(next.F, min(p.S, next.S))); 22 | } 23 | } 24 | if(!maxFlow) break; 25 | flow += maxFlow; 26 | int curr = t; 27 | while(curr != s) { 28 | adj[curr][prec[curr]] += maxFlow; 29 | adj[prec[curr]][curr] -= maxFlow; 30 | if(adj[prec[curr]][curr] == 0) adj[prec[curr]].erase(curr); 31 | curr = prec[curr]; 32 | } 33 | } 34 | return flow; 35 | } 36 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Ford-Fulkerson.cpp: -------------------------------------------------------------------------------- 1 | int m, m; 2 | bool vis[MAXN]; 3 | map adj[MAXN]; 4 | 5 | ll dfsFlow(int node, int sink, ll flow) { 6 | if(vis[node]) return 0; 7 | vis[node] = 1; 8 | if(node == sink) return flow; 9 | for(const pair p : adj[node]) if(ll f = dfsFlow(p.F, sink, min(p.S, flow))) { 10 | adj[p.F][node] += f; 11 | adj[node][p.F] -= f; 12 | if(!adj[node][p.F]) adj[node].erase(p.F); 13 | return f; 14 | } 15 | return 0; 16 | } 17 | 18 | ll fordFulk(int s, int t) { 19 | ll flow = 0; 20 | while(1) { 21 | memset(vis, 0, n * sizeof(bool)); 22 | ll maxFlow = dfsFlow(s, t, ~(1LL << 63)); 23 | if(!maxFlow) break; 24 | flow += maxFlow; 25 | } 26 | return flow; 27 | } 28 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Hopcroft-Karp.cpp: -------------------------------------------------------------------------------- 1 | int n, m, p, level[MAXN], match[MAXN]; 2 | vi adj[MAXN]; 3 | bool vis[MAXN]; 4 | 5 | bool dfs(int node) { 6 | if(vis[node]) return 0; 7 | vis[node] = 1; 8 | if(!level[node]) return 1; 9 | if(level[node] & 1) { for(const int next : adj[node]) if(level[next] == level[node] - 1 && dfs(next)) { 10 | match[node] = next; 11 | match[next] = node; 12 | return 1; 13 | }} else return dfs(match[node]); 14 | return 0; 15 | } 16 | 17 | int hopKarp(){ 18 | int res = 0; 19 | F0R(i, n + m) match[i] = -1; 20 | while(1) { 21 | int free = 0, augPaths = 0; 22 | vi frees; 23 | queue q; 24 | F0R(i, n + m) level[i] = ((i < n && match[i] == -1)?0:-1); 25 | memset(vis, 0, (n + m) * sizeof(bool)); 26 | F0R(i, n) if(match[i] == -1) q.push(i); 27 | 28 | while(!q.empty()) { 29 | int curr = q.front(); q.pop(); 30 | if(!free && (level[curr] & 1) && match[curr] == -1) free = level[curr]; 31 | if(free && free == level[curr] && match[curr] == -1) frees.PB(curr); 32 | if(free) continue; 33 | 34 | if(level[curr] & 1) { 35 | level[match[curr]] = level[curr] + 1; 36 | q.push(match[curr]); 37 | } else for(const int next : adj[curr]) if(level[next] == -1) { 38 | level[next] = level[curr] + 1; 39 | q.push(next); 40 | } 41 | } 42 | for(const int i : frees) if(dfs(i)) augPaths++; 43 | if(!augPaths) return res; 44 | res += augPaths; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Push-Relabel/FIFO + gap.cpp: -------------------------------------------------------------------------------- 1 | int n, m, h[MAXN], cnt[MAXN]; 2 | ll e[MAXN], c[MAXN][MAXN], f[MAXN][MAXN]; 3 | vi neigh[MAXN]; 4 | vector> adj[MAXN]; 5 | deque q; 6 | 7 | ll cf(int u, int v) { return c[u][v] - f[u][v]; } 8 | 9 | void push(int u, int v, int t) { 10 | ll flow = min(e[u], cf(u, v)); 11 | f[u][v] += flow, f[v][u] -= flow; 12 | e[u] -= flow, e[v] += flow; 13 | if(v != t && e[v] == flow) q.PB(v); 14 | } 15 | 16 | void gap(int k) { 17 | F0R(i, n) if(h[i] < n && h[i] >= k) { 18 | cnt[h[i]]--; 19 | h[i] = n; 20 | } 21 | } 22 | 23 | void relabel(int u) { 24 | cnt[h[u]]--; 25 | h[u] = n; 26 | for(const int v : neigh[u]) if(cf(u, v) > 0) h[u] = min(h[u], h[v] + 1); 27 | if(h[u] < n) cnt[h[u]]++, q.push_front(u); 28 | } 29 | 30 | void discharge(int u, int t) { 31 | for(const int v : neigh[u]) 32 | if(e[u] && cf(u, v) && h[v] + 1 == h[u]) push(u, v, t); 33 | else if(!e[u]) break; 34 | if(e[u] && cnt[h[u]] == 1) gap(h[u]); 35 | else if(e[u]) relabel(u); 36 | } 37 | 38 | ll flow(int s, int t) { 39 | F0R(i, n) for(const pair p : adj[i]) { 40 | if(!c[i][p.F] && !c[p.F][i]) neigh[i].PB(p.F), neigh[p.F].PB(i); 41 | c[i][p.F] += p.S; 42 | } 43 | for(const pair p : adj[s]) e[s] += p.S; 44 | cnt[0] = n; q.PB(s); 45 | while(!q.empty()) { 46 | int u = q.front(); q.pop_front(); 47 | if(h[u] < n) discharge(u, t); 48 | } 49 | return e[t]; 50 | } 51 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Push-Relabel/highest-label + gap.cpp: -------------------------------------------------------------------------------- 1 | int n, m, maxH, h[MAXN], cnt[MAXN]; 2 | ll e[MAXN], c[MAXN][MAXN], f[MAXN][MAXN]; 3 | vi withH[MAXN], neigh[MAXN]; 4 | vector> adj[MAXN]; 5 | 6 | ll cf(int u, int v) { return c[u][v] - f[u][v]; } 7 | 8 | void enq(int v) { 9 | withH[h[v]].PB(v); 10 | maxH = max(maxH, h[v]); 11 | } 12 | 13 | void push(int u, int v, int t) { 14 | ll flow = min(e[u], cf(u, v)); 15 | f[u][v] += flow, f[v][u] -= flow; 16 | e[u] -= flow, e[v] += flow; 17 | if(v != t && e[v] == flow) enq(v); 18 | } 19 | 20 | void gap(int k) { 21 | F0R(i, n) if(h[i] < n && h[i] >= k) { 22 | cnt[h[i]]--; 23 | withH[h[i]].clear(); 24 | h[i] = n; 25 | } 26 | } 27 | 28 | void relabel(int u) { 29 | cnt[h[u]]--; 30 | h[u] = n; 31 | for(const int v : neigh[u]) if(cf(u, v) > 0) h[u] = min(h[u], h[v] + 1); 32 | if(h[u] < n) cnt[h[u]]++, enq(u); 33 | } 34 | 35 | void discharge(int u, int t) { 36 | for(const int v : neigh[u]) 37 | if(e[u] && cf(u, v) && h[v] + 1 == h[u]) push(u, v, t); 38 | else if(!e[u]) break; 39 | if(e[u] && cnt[h[u]] == 1) gap(h[u]); 40 | else if(e[u]) relabel(u); 41 | } 42 | 43 | ll flow(int s, int t) { 44 | F0R(i, n) for(const pair p : adj[i]) { 45 | if(!c[i][p.F] && !c[p.F][i]) neigh[i].PB(p.F), neigh[p.F].PB(i); 46 | c[i][p.F] += p.S; 47 | } 48 | for(const pair p : adj[s]) e[s] += p.S; 49 | cnt[0] = n; enq(s); 50 | while(maxH >= 0) { 51 | if(withH[maxH].empty()) maxH--; 52 | else { 53 | int u = withH[maxH].back(); withH[maxH].pop_back(); 54 | discharge(u, t); 55 | } 56 | } 57 | return e[t]; 58 | } 59 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/Push-Relabel/relabel-to-front + gap.cpp: -------------------------------------------------------------------------------- 1 | // Uses LinkedList.cpp 2 | 3 | int n, m, h[MAXN], cnt[MAXN]; 4 | ll e[MAXN], c[MAXN][MAXN], f[MAXN][MAXN]; 5 | vi neigh[MAXN]; 6 | vector> adj[MAXN]; 7 | 8 | ll cf(int u, int v) { return c[u][v] - f[u][v]; } 9 | 10 | void push(int u, int v) { 11 | ll flow = min(e[u], cf(u, v)); 12 | f[u][v] += flow, f[v][u] -= flow; 13 | e[u] -= flow, e[v] += flow; 14 | } 15 | 16 | void gap(int k) { 17 | F0R(i, n) if(h[i] < n && h[i] >= k) { 18 | cnt[h[i]]--; 19 | h[i] = n; 20 | } 21 | } 22 | 23 | void relabel(int u) { 24 | cnt[h[u]]--; 25 | h[u] = n; 26 | for(const int v : neigh[u]) if(cf(u, v) > 0) h[u] = min(h[u], h[v] + 1); 27 | if(h[u] < n) cnt[h[u]]++; 28 | } 29 | 30 | void discharge(int u) { 31 | for(const int v : neigh[u]) 32 | if(e[u] && cf(u, v) && h[v] + 1 == h[u]) push(u, v); 33 | else if(!e[u]) break; 34 | if(e[u] && cnt[h[u]] == 1) gap(h[u]); 35 | else if(e[u]) relabel(u); 36 | } 37 | 38 | ll flow(int s, int t) { 39 | F0R(i, n) for(const pair p : adj[i]) { 40 | if(!c[i][p.F] && !c[p.F][i]) neigh[i].PB(p.F), neigh[p.F].PB(i); 41 | c[i][p.F] += p.S; 42 | } 43 | for(const pair p : adj[s]) e[s] += p.S; 44 | LinkedList L; 45 | F0R(i, n) L.push(i); 46 | cnt[0] = n; 47 | Node *it = L.head; 48 | while(it) { 49 | int u = it->val; 50 | int sh = h[u]; 51 | if(h[u] < n && u != t) discharge(u); 52 | if(h[u] > sh) L.moveToFront(it); 53 | else it = it->next; 54 | } 55 | return e[t]; 56 | } 57 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Flow/ScalingFlow.cpp: -------------------------------------------------------------------------------- 1 | int n, m; 2 | bool vis[MAXN]; 3 | map adj[MAXN]; 4 | 5 | bool dfsFlow(int node, int sink, ll minCap) { 6 | if(vis[node]) return 0; 7 | vis[node] = 1; 8 | if(node == sink) return 1; 9 | for(const pair p : adj[node]) 10 | if(p.S >= minCap && dfsFlow(p.F, sink, minCap)) { 11 | adj[node][p.F] -= minCap; 12 | if(!adj[node][p.F]) adj[node].erase(p.F); 13 | adj[p.F][node] += minCap; 14 | return 1; 15 | } 16 | return 0; 17 | } 18 | 19 | ll scalingFlow(int s, int t) { 20 | ll minCap = 0; 21 | F0R(node, n) for(const pair p : adj[node]) minCap = max(minCap, p.S); 22 | ll flow = 0; 23 | while(minCap) { 24 | memset(vis, 0, n * sizeof(bool)); 25 | if(!dfsFlow(s, t, minCap)) minCap /= 2; 26 | else flow += minCap; 27 | } 28 | return flow; 29 | } 30 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/Tarjan-LCA.cpp: -------------------------------------------------------------------------------- 1 | // Uses DisjointUnion.cpp 2 | 3 | int n, q, anc[MAXN], res[MAXQ]; 4 | vi children[MAXN]; 5 | vpii queries, nodeQueries[MAXN]; 6 | bool vis[MAXN]; 7 | DSU dsu; 8 | 9 | void dfs(int node) { 10 | for(const int child : children[node]) { 11 | dfs(child); 12 | dsu.join(node, child); 13 | anc[dsu.root(node)] = node; 14 | } 15 | vis[node] = 1; 16 | for(const pii p : nodeQueries[node]) if(vis[p.S]) res[p.F] = anc[dsu.root(p.S)]; 17 | } 18 | 19 | void lca(int root) { 20 | memset(vis, 0, n * sizeof(bool)); 21 | F0R(i, n) nodeQueries[i].clear(); 22 | F0R(i, q) { 23 | nodeQueries[queries[i].F].PB(MP(i, queries[i].S)); 24 | nodeQueries[queries[i].S].PB(MP(i, queries[i].F)); 25 | } 26 | dsu = DSU(n); 27 | dfs(root); 28 | } 29 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Graph/TopoSort.cpp: -------------------------------------------------------------------------------- 1 | int n; 2 | vi adj[MAXN]; 3 | bool vis[MAXN]; 4 | 5 | void dfs(int node, const vi &topo) { 6 | if(vis[node]) return; 7 | vis[node] = 1; 8 | for(const int next : adj[node]) dfs(next); 9 | topo.PB(node); 10 | } 11 | 12 | vi topoSort() { 13 | vi topo; 14 | F0R(i, n) if(!vis[i]) dfs(i, topo); 15 | reverse(all(topo)); 16 | } 17 | -------------------------------------------------------------------------------- /Implementations/Algorithms/Math/FFT.cpp: -------------------------------------------------------------------------------- 1 | namespace FFT { 2 | 3 | const int LG_MAXN = 22; 4 | bool builtRT; 5 | cld rt[LG_MAXN + 1], invrt[LG_MAXN + 1]; 6 | 7 | void buildRT() { 8 | if(builtRT) return; 9 | builtRT = 1; 10 | F0R(i, LG_MAXN + 1) rt[i] = cld(cos(2 * PI / (1 << i)), sin(2 * PI / (1 << i))); 11 | F0R(i, LG_MAXN + 1) invrt[i] = cld(1, 0) / rt[i]; 12 | } 13 | 14 | int rev(int x, int numBits) { 15 | int res = 0; 16 | F0R(i, numBits) if(x & (1 << i)) res |= (1 << (numBits - 1 - i)); 17 | return res; 18 | } 19 | 20 | vcld bitRevCopy(vcld v) { 21 | vcld res(sz(v)); 22 | F0R(i, sz(v)) res[i] = v[rev(i, 31 - __builtin_clz(sz(v)))]; 23 | return res; 24 | } 25 | 26 | vcld fft(vcld v) { 27 | buildRT(); 28 | vcld res = bitRevCopy(v); 29 | int n = sz(res); 30 | F0R(i, 32 - __builtin_clz(n)) { 31 | int step = 1 << i; 32 | cld root = rt[i]; 33 | for(int k = 0; k < n; k += step) { 34 | cld currMult(1, 0); 35 | F0R(j, step >> 1) { 36 | cld t = currMult * res[k + j + (step >> 1)]; 37 | cld u = res[k + j]; 38 | res[k + j] = u + t; 39 | res[k + j + (step >> 1)] = u - t; 40 | currMult *= root; 41 | } 42 | } 43 | } 44 | return res; 45 | } 46 | 47 | vcld invfft(vcld v) { 48 | swap(rt, invrt); 49 | vcld res = fft(v); 50 | swap(rt, invrt); 51 | ld u = 1.0 / sz(v); 52 | F0R(i, sz(res)) res[i] *= u; 53 | return res; 54 | } 55 | 56 | vcld conv(vcld a, vcld b) { 57 | int finalSize = sz(a) + sz(b) - 1; 58 | int neededSize = (finalSize == 1)?1 : (1 << (32 - __builtin_clz(finalSize - 1))); 59 | a.resize(neededSize), b.resize(neededSize); 60 | a = fft(a), b = fft(b); 61 | F0R(i, neededSize) a[i] *= b[i]; 62 | a = invfft(a); 63 | a.resize(finalSize); 64 | return a; 65 | } 66 | 67 | }; 68 | -------------------------------------------------------------------------------- /Implementations/Algorithms/String/Aho-Corasick.cpp: -------------------------------------------------------------------------------- 1 | struct AhoTrie{ 2 | 3 | int currNode = 0, numPats, numNodes = 0; 4 | vector> trie; 5 | vi dict, link; 6 | vector strData; 7 | 8 | AhoTrie(const vector &pat) { 9 | numPats = sz(pat); 10 | addNode(); 11 | F0R(i, sz(pat)) { 12 | int curr = 0; 13 | for(const char c : pat[i]) { 14 | if(!trie[curr].count(c)) trie[curr].insert(MP(c, numNodes)), addNode(); 15 | curr = trie[curr][c]; 16 | } 17 | dict[curr] = curr; strData[curr].PB(MP(i, sz(pat[i]))); 18 | } 19 | bfs(); 20 | } 21 | 22 | void addNode() { 23 | numNodes++; 24 | dict.PB(0); link.PB(0); strData.PB(vpii()); trie.PB(map()); 25 | } 26 | 27 | void bfs() { 28 | queue q; 29 | q.push(0); 30 | while(!q.empty()) { 31 | int i = q.front(); q.pop(); 32 | for(const pair p : trie[i]) { 33 | q.push(p.S); 34 | int lps = link[i]; 35 | while(lps && !trie[lps].count(p.F)) lps = link[lps]; 36 | if(trie[lps].count(p.F) && lps != i) lps = trie[lps][p.F]; 37 | link[p.S] = lps; 38 | if(!dict[p.S]) dict[p.S] = dict[lps]; 39 | } 40 | } 41 | } 42 | 43 | void step(char c) { 44 | while(currNode && !trie[currNode].count(c)) currNode = link[currNode]; 45 | if(trie[currNode].count(c)) currNode = trie[currNode][c]; 46 | } 47 | 48 | void addToOcc(vector &occ, int idx) { 49 | int curr = dict[currNode]; 50 | while(curr) { 51 | for(const pii p : strData[curr]) occ[p.F].PB(idx - p.S + 1); 52 | curr = dict[link[curr]]; 53 | } 54 | } 55 | 56 | vector getOcc(const string &txt) { 57 | vector occ; 58 | F0R(i, numPats) occ.PB(vi()); 59 | F0R(i, sz(txt)) { 60 | step(txt[i]); 61 | addToOcc(occ, i); 62 | } 63 | return occ; 64 | } 65 | 66 | }; 67 | -------------------------------------------------------------------------------- /Implementations/Algorithms/String/KMP.cpp: -------------------------------------------------------------------------------- 1 | vpii constructDFA(string m) { 2 | vpii dfa(1, MP(-1, 0)); 3 | F0R(i, sz(m)) { 4 | int lps = dfa[i].S; 5 | dfa[i].F = m[i]; 6 | while(lps > 0 && dfa[lps].F != m[i]) lps = dfa[lps].S; 7 | if(dfa[lps].F == m[i] && lps != i) lps++; 8 | dfa.PB(MP(-1, lps)); 9 | } 10 | return dfa; 11 | } 12 | 13 | vi kmp(string pat, string str) { 14 | vpii dfa = constructDFA(pat); 15 | vi occ; 16 | int curr = 0; 17 | F0R(i, sz(str)) { 18 | while(curr > 0 && dfa[curr].F != str[i]) curr = dfa[curr].S; 19 | if(dfa[curr].F == str[i]) curr++; 20 | if(curr == sz(pat)) occ.PB(i - sz(pat) + 1); 21 | } 22 | return occ; 23 | } 24 | -------------------------------------------------------------------------------- /Implementations/Algorithms/String/KMP_LEA.cpp: -------------------------------------------------------------------------------- 1 | // Lowercase English Alphabet 2 | vector constructDFA(string m) { 3 | vector dfa(1, new int[26]{0}); 4 | F0R(i, sz(m)) { 5 | int lps = dfa[i][m[i] - 'a']; 6 | dfa[i][m[i] - 'a'] = i + 1; 7 | dfa.PB(new int[26]); 8 | copy(dfa[lps], dfa[lps] + 26, dfa.back()); 9 | } 10 | return dfa; 11 | } 12 | 13 | vi kmp(string pat, string str) { 14 | vector dfa = constructDFA(pat); 15 | vi occ; 16 | int curr = 0; 17 | F0R(i, sz(str)) { 18 | curr = dfa[curr][str[i] - 'a']; 19 | if(curr == sz(pat)) occ.PB(i - sz(pat) + 1); 20 | } 21 | return occ; 22 | } 23 | -------------------------------------------------------------------------------- /Implementations/Algorithms/String/Manachers.cpp: -------------------------------------------------------------------------------- 1 | vi manachers(string s) { 2 | string t; t.reserve(2 * sz(s) + 1); 3 | F0R(i, sz(s)) t += '.', t += s[i]; 4 | t[0] = 's'; t += 'e'; 5 | vi res(sz(t)); 6 | int lo = 0, hi = 0; 7 | FOR(i, 1, sz(t) - 1) { 8 | res[i] = max(0, min(hi - i, (i <= hi)?res[lo + hi - i]:0)); 9 | while(t[i + res[i] + 1] == t[i - res[i] - 1]) res[i]++; 10 | if(i + res[i] > hi) hi = i + res[i], lo = i - res[i]; 11 | } 12 | F0R(i, sz(s)) res[(i << 1) | 1] |= 1; 13 | F0R(i, sz(s) + 1) if(res[i << 1] & 1) res[i << 1]++; 14 | return res; 15 | } 16 | -------------------------------------------------------------------------------- /Implementations/Algorithms/String/Z.cpp: -------------------------------------------------------------------------------- 1 | vi z(string s) { 2 | vi z(sz(s)); 3 | z[0] = sz(s); 4 | int lo = 0, hi = 0; 5 | FOR(i, 1, sz(s)) { 6 | z[i] = max(0, min(hi - i + 1, z[i - lo])); 7 | while(i + z[i] < sz(s) && s[i + z[i]] == s[z[i]]) lo = i, hi = i + z[i]++; 8 | } 9 | return z; 10 | } 11 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Geometry/ConvexHull.cpp: -------------------------------------------------------------------------------- 1 | // pretty much follows kactl implementation 2 | 3 | bool Q = false; 4 | 5 | struct Line { 6 | 7 | ll slope, yint; 8 | mutable ld pnt; 9 | 10 | bool operator<(const Line& o) const { 11 | return Q ? pnt < o.pnt : slope < o.slope; 12 | } 13 | }; 14 | 15 | struct cht : multiset { 16 | 17 | bool isect(iterator x, iterator y) { 18 | if (y == end()) { x->pnt = 1/0.0; return 0; } 19 | if (x->slope == y->slope) x->pnt = ((x->yint > y->yint)? 1/0.0 : -1/0.0); 20 | else x->pnt = (ld)(x->yint - y->yint) / (y->slope - x->slope); 21 | return x->pnt >= y->pnt; 22 | } 23 | 24 | void add(ll slope, ll yint) { 25 | auto z = insert({slope, yint, 0}), y = z++, x = y; 26 | while(isect(y, z)) z = erase(z); 27 | if (x != begin() && isect(--x, y)) isect(x, y = erase(y)); 28 | while ((y = x) != begin() && (--x)->pnt >= y->pnt) isect(x, erase(y)); 29 | } 30 | 31 | ll query(ll x) { 32 | Q = true; 33 | auto ptr = *LB({0, 0, (ld)x}); 34 | Q = false; 35 | return ptr.slope * x + ptr.yint; 36 | } 37 | }; 38 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Graphs/BiconnectedComponents.cpp: -------------------------------------------------------------------------------- 1 | struct BCC { 2 | 3 | bool *visited; 4 | int *depth, *lowpoint, numNodes; 5 | vi *connections; 6 | vector> components; 7 | 8 | BCC(int sz) { 9 | numNodes = sz; 10 | depth = new int[numNodes]; 11 | lowpoint = new int[numNodes]; 12 | visited = new bool[numNodes]; 13 | connections = new vi[numNodes]; 14 | memset(depth, 0, numNodes); 15 | } 16 | 17 | void addEdge(int a, int b) { 18 | connections[a].PB(b); 19 | connections[b].PB(a); 20 | } 21 | 22 | void calculateBCCs() { 23 | memset(visited, 0, numNodes); 24 | vector *s = new vector(); 25 | F0R(i, numNodes) if(!visited[i]) dfs(i, 0, -1, s); 26 | } 27 | 28 | void dfs(int node, int d, int parent, vector* s) { 29 | visited[node] = true; 30 | depth[node] = lowpoint[node] = d; 31 | for(const int next : connections[node]) { 32 | pii edge = MP(min(node, next), max(node, next)); 33 | if(visited[next] && next != parent) { 34 | lowpoint[node] = min(lowpoint[node], depth[next]); 35 | if(depth[next] < d) s->PB(edge); 36 | } else if (!visited[next]) { 37 | s->PB(edge); 38 | dfs(next, d + 1, node, s); 39 | lowpoint[node] = min(lowpoint[node], lowpoint[next]); 40 | if(lowpoint[next] >= d) { 41 | vector v; 42 | while(true) { 43 | pii p = s->back(); 44 | s->pop_back(); 45 | v.PB(p); 46 | if(p == edge) break; 47 | } 48 | components.PB(v); 49 | } 50 | } 51 | } 52 | } 53 | }; 54 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Graphs/DisjointUnion.cpp: -------------------------------------------------------------------------------- 1 | struct DSU { 2 | 3 | int *parent, *rank; 4 | 5 | DSU(int sz = 0) { 6 | parent = new int[sz], rank = new int[sz]; 7 | F0R(i, sz) parent[i] = i, rank[i] = 0; 8 | } 9 | 10 | int root(int a) { return (parent[a] == a)? a : (parent[a] = root(parent[a])); } 11 | 12 | void join(int a, int b) { 13 | int ra = root(a), rb = root(b); 14 | if(ra == rb) return; 15 | if(rank[ra] > rank[rb]) swap(ra, rb); 16 | parent[ra] = rb; 17 | if(rank[ra] == rank[rb]) rank[rb]++; 18 | } 19 | 20 | bool connected(int a, int b) { return root(a) == root(b); } 21 | 22 | }; 23 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Graphs/SizeRecordingDisjointUnion.cpp: -------------------------------------------------------------------------------- 1 | struct DSU { 2 | 3 | int *parent, *sizeRooted; 4 | 5 | DSU(int sz) { 6 | parent = new int[sz], sizeRooted = new int[sz]; 7 | F0R(i, sz) parent[i] = i, sizeRooted[i] = 1; 8 | } 9 | 10 | int root(int a) { 11 | if(parent[a] == a) return a; 12 | return (parent[a] = root(parent[a])); 13 | } 14 | 15 | void join(int a, int b) { 16 | int ra = root(a), rb = root(b); 17 | if(ra == rb) return; 18 | if(sizeRooted[ra] > sizeRooted[rb]) swap(ra, rb); 19 | parent[ra] = rb; 20 | sizeRooted[rb] += sizeRooted[ra]; 21 | } 22 | 23 | bool connected(int a, int b) { 24 | return root(a) == root(b); 25 | } 26 | 27 | int componentSize(int a) { 28 | return sizeRooted[root(a)]; 29 | } 30 | 31 | }; 32 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Lists/LinkedList.cpp: -------------------------------------------------------------------------------- 1 | template struct Node{ 2 | 3 | Node *next = NULL, *prev = NULL; 4 | T val; 5 | 6 | Node(T t) { this->val = t; } 7 | 8 | }; 9 | 10 | template struct LinkedList{ 11 | 12 | Node *head = NULL, *tail = NULL; 13 | 14 | void push(T t) { 15 | if(!head) tail = head = new Node(t); 16 | else if(head == tail) { 17 | tail = new Node(t); 18 | head->next = tail; 19 | tail->prev = head; 20 | } else { 21 | tail->next = new Node(t); 22 | tail->next->prev = tail; 23 | tail = tail->next; 24 | } 25 | } 26 | 27 | void pop() { 28 | head = head->next; 29 | head->prev = NULL; 30 | } 31 | 32 | void moveToFront(Node *node) { 33 | if(node == head) return; 34 | if(node == tail) { 35 | tail = tail->prev; 36 | tail->next = NULL; 37 | } else { 38 | node->prev->next = node->next; 39 | node->next->prev = node->prev; 40 | } 41 | node->prev = NULL; 42 | node->next = head; 43 | head = head->prev = node; 44 | } 45 | 46 | }; 47 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Binary Indexed Trees/BIT.cpp: -------------------------------------------------------------------------------- 1 | struct BIT { 2 | 3 | int sz; 4 | ll *tree; 5 | 6 | BIT(int sz) { 7 | this->sz = sz; 8 | tree = new ll[sz + 1]; 9 | memset(tree, 0, (sz + 1) * sizeof(ll)); 10 | } 11 | 12 | void update(int idx, ll v) { 13 | for(idx++; idx <= sz; idx += (idx & -idx)) tree[idx] += v; 14 | } 15 | 16 | ll query(int idx) { 17 | ll total = 0; 18 | for(idx++; idx > 0; idx -= (idx & -idx)) total += tree[idx]; 19 | return total; 20 | } 21 | 22 | ll queryRange(int lo, int hi) { 23 | return query(hi) - query(lo - 1); 24 | } 25 | 26 | ll querySingle(int idx) { 27 | ll total = tree[++idx]; 28 | int z = idx - (idx & -idx); 29 | for(idx--; idx > z; idx -= (idx & -idx)) total -= tree[idx]; 30 | return total; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Binary Indexed Trees/RUSQ.cpp: -------------------------------------------------------------------------------- 1 | struct RUSQ { 2 | 3 | int sz; 4 | ll *tree; 5 | 6 | RUSQ(int sz) { 7 | this->sz = sz; 8 | tree = new ll[sz + 1]; 9 | memset(tree, 0, (sz + 1) * sizeof(ll)); 10 | } 11 | 12 | void update(int idx, ll v) { 13 | for(idx++; idx <= sz; idx += (idx & -idx)) tree[idx] += v; 14 | } 15 | 16 | void updateRange(int lo, int hi, int v) { 17 | update(lo, v); 18 | update(hi + 1, -v); 19 | } 20 | 21 | ll query(int idx) { 22 | ll total = 0; 23 | for(idx++; idx > 0; idx -= (idx & -idx)) total += tree[idx]; 24 | return total; 25 | } 26 | 27 | void reset() { 28 | memset(tree, 0, (sz + 1) * sizeof(ll)); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Segment Trees/LazyMaxSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | struct LazySegmentTree { 2 | 3 | int sz; 4 | ll *tree, *lazy; 5 | 6 | LazySegmentTree(int pre_sz) { 7 | sz = 1 << (int)ceil(log2(pre_sz)); 8 | tree = new ll[2 * sz]; 9 | lazy = new ll[2 * sz]; 10 | memset(tree, 0, 2 * sz * sizeof(ll)); 11 | memset(lazy, 0, 2 * sz * sizeof(ll)); 12 | } 13 | 14 | void propagateLazy(int idx) { 15 | tree[2 * idx] += lazy[idx]; 16 | tree[2 * idx + 1] += lazy[idx]; 17 | lazy[2 * idx] += lazy[idx]; 18 | lazy[2 * idx + 1] += lazy[idx]; 19 | lazy[idx] = 0; 20 | } 21 | 22 | void update(int idx, int s, int e, int l, int r, ll v) { 23 | if(l == s && r == e) { 24 | tree[idx] += v; 25 | lazy[idx] += v; 26 | return; 27 | } 28 | propagateLazy(idx); 29 | int mid = (s + e) >> 1; 30 | if(l < mid) update(2 * idx, s, mid, l, min(r, mid), v); 31 | if(r > mid) update(2 * idx + 1, mid, e, max(l, mid), r, v); 32 | tree[idx] = max(tree[2 * idx], tree[2 * idx + 1]); 33 | } 34 | 35 | void update(int l, int r, ll v) { 36 | update(1, 0, sz, l, r, v); 37 | } 38 | 39 | ll query(int idx, int s, int e, int l, int r) { 40 | if(l == s && r == e) return tree[idx]; 41 | propagateLazy(idx); 42 | int mid = (s + e) >> 1; 43 | if(l < mid && r > mid) return max(query(2 * idx, s, mid, l, min(mid, r)), query(2 * idx + 1, mid, e, max(mid, l), r)); 44 | else if(l < mid) return query(2 * idx, s, mid, l, min(mid, r)); 45 | else return query(2 * idx + 1, mid, e, max(mid, l), r); 46 | } 47 | 48 | ll query(int l, int r) { 49 | return query(1, 0, sz, l, r); 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Segment Trees/LazyMinSegmentTree.cpp: -------------------------------------------------------------------------------- 1 | struct LazySegmentTree { 2 | 3 | int sz; 4 | ll *tree, *lazy; 5 | 6 | LazySegmentTree(int pre_sz) { 7 | sz = 1 << (int)ceil(log2(pre_sz)); 8 | tree = new ll[2 * sz]; 9 | lazy = new ll[2 * sz]; 10 | memset(tree, 0, 2 * sz * sizeof(ll)); 11 | memset(lazy, 0, 2 * sz * sizeof(ll)); 12 | } 13 | 14 | void propagateLazy(int idx) { 15 | tree[2 * idx] += lazy[idx]; 16 | tree[2 * idx + 1] += lazy[idx]; 17 | lazy[2 * idx] += lazy[idx]; 18 | lazy[2 * idx + 1] += lazy[idx]; 19 | lazy[idx] = 0; 20 | } 21 | 22 | void update(int idx, int s, int e, int l, int r, ll v) { 23 | if(l == s && r == e) { 24 | tree[idx] += v; 25 | lazy[idx] += v; 26 | return; 27 | } 28 | propagateLazy(idx); 29 | int mid = (s + e) >> 1; 30 | if(l < mid) update(2 * idx, s, mid, l, min(r, mid), v); 31 | if(r > mid) update(2 * idx + 1, mid, e, max(l, mid), r, v); 32 | tree[idx] = min(tree[2 * idx], tree[2 * idx + 1]); 33 | } 34 | 35 | void update(int l, int r, ll v) { 36 | update(1, 0, sz, l, r, v); 37 | } 38 | 39 | ll query(int idx, int s, int e, int l, int r) { 40 | if(l == s && r == e) return tree[idx]; 41 | propagateLazy(idx); 42 | int mid = (s + e) >> 1; 43 | if(l < mid && r > mid) return min(query(2 * idx, s, mid, l, min(mid, r)), query(2 * idx + 1, mid, e, max(mid, l), r)); 44 | else if(l < mid) return query(2 * idx, s, mid, l, min(mid, r)); 45 | else return query(2 * idx + 1, mid, e, max(mid, l), r); 46 | } 47 | 48 | ll query(int l, int r) { 49 | return query(1, 0, sz, l, r); 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Segment Trees/LazySegmentTree.cpp: -------------------------------------------------------------------------------- 1 | struct LazySegmentTree { 2 | 3 | int sz; 4 | ll *tree, *lazy; 5 | 6 | LazySegmentTree(int _sz) { 7 | sz = 1 << __builtin_clz(_sz - 1); 8 | tree = new ll[sz << 1]; 9 | lazy = new ll[sz << 1]; 10 | memset(tree, 0, 2 * sz * sizeof(ll)); 11 | memset(lazy, 0, 2 * sz * sizeof(ll)); 12 | } 13 | 14 | void propagateLazy(int idx, int s, int e, int mid) { 15 | tree[2 * idx] += (mid - s) * lazy[idx]; 16 | tree[2 * idx + 1] += (e - mid) * lazy[idx]; 17 | lazy[2 * idx] += lazy[idx]; 18 | lazy[2 * idx + 1] += lazy[idx]; 19 | lazy[idx] = 0; 20 | } 21 | 22 | void update(int idx, int s, int e, int l, int r, ll v) { 23 | if(e <= l || r <= s) return 24 | if(l <= s && e <= r) {; 25 | tree[idx] += (s - e) * v; 26 | lazy[idx] += v; 27 | return; 28 | } 29 | int mid = (s + e) >> 1; 30 | propagateLazy(idx, s, e, mid); 31 | update(2 * idx, s, mid, l, min(r, mid), v); 32 | update(2 * idx + 1, mid, e, max(l, mid), r, v); 33 | tree[idx] = tree[2 * idx] + tree[2 * idx + 1]; 34 | } 35 | 36 | void update(int l, int r, ll v) { 37 | update(1, 0, sz, l, r, v); 38 | } 39 | 40 | ll query(int idx, int s, int e, int l, int r) { 41 | if(e <= l || r <= s) return 0; 42 | if(l <= s && e <= r) return tree[idx]; 43 | int mid = (s + e) >> 1; 44 | propagateLazy(idx, s, e, mid); 45 | return query(2 * idx, s, mid, l, r) + query(2 * idx + 1, mid, e, l, r); 46 | } 47 | 48 | ll query(int l, int r) { 49 | return query(1, 0, sz, l, r); 50 | } 51 | }; 52 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Segment Trees/SegmentTree.cpp: -------------------------------------------------------------------------------- 1 | template 2 | struct SegmentTree { 3 | 4 | function comb; 5 | function upd; 6 | int sz; 7 | T *tree; 8 | 9 | SegmentTree(int _sz = 0, 10 | function _comb = [](T a, T b) -> T { return a + b; }, // combine function 11 | function _upd = [](T& a, T b) -> void { a = b; }) // update function 12 | : comb(_comb), upd(_upd) 13 | { 14 | sz = 1 << (32 - __builtin_clz(_sz - 1)); 15 | tree = new T[sz << 1]; 16 | memset(tree, 0, 2 * sz * sizeof(T)); 17 | } 18 | 19 | void update(int idx, T v) { 20 | idx += sz; 21 | upd(tree[idx], v); 22 | for(idx /= 2; idx > 0; idx /= 2) tree[idx] = comb(tree[2 * idx], tree[2 * idx + 1]); 23 | } 24 | 25 | T query(int lo, int hi) { 26 | lo += sz, hi += sz; 27 | T total = tree[lo++]; 28 | while(lo <= hi) { 29 | if(lo % 2 == 1) total = comb(total, tree[lo++]); 30 | if(hi % 2 == 0) total = comb(total, tree[hi--]); 31 | lo /= 2; 32 | hi /= 2; 33 | } 34 | return total; 35 | } 36 | }; 37 | 38 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Static Queries/RangeMaxQuery.cpp: -------------------------------------------------------------------------------- 1 | struct RMQ { 2 | 3 | int n; 4 | ll* arr; 5 | vector sparse; 6 | 7 | RMQ(int sz) { 8 | n = sz; 9 | arr = new ll[n]; 10 | memset(arr, 0, n * sizeof(ll)); 11 | } 12 | 13 | void update(int i, ll v) { 14 | arr[i] += v; 15 | } 16 | 17 | void genRMQ() { 18 | F0R(i, n) sparse.PB(vi()); 19 | F0R(i, n) sparse[i].PB(arr[i]); 20 | FOR(i, 1, log2(n) + 1) F0R(j, n) { 21 | if(j + (1 << i) > n) sparse[j].PB(sparse[j][i - 1]); 22 | else sparse[j].PB(max(sparse[j][i - 1], sparse[j + (1 << (i - 1))][i - 1])); 23 | } 24 | } 25 | 26 | ll query(int l, int r) { 27 | int s = (int)floor(log2(r - l + 1)); 28 | return max(sparse[l][s], sparse[r + 1 - (1 << s)][s]); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Range Queries/Static Queries/RangeMinQuery.cpp: -------------------------------------------------------------------------------- 1 | struct RMQ { 2 | 3 | int n; 4 | ll* arr; 5 | vector sparse; 6 | 7 | RMQ(int sz) { 8 | n = sz; 9 | arr = new ll[n]; 10 | memset(arr, 0, n * sizeof(ll)); 11 | } 12 | 13 | void update(int i, ll v) { 14 | arr[i] += v; 15 | } 16 | 17 | void genRMQ() { 18 | F0R(i, n) sparse.PB(vi()); 19 | F0R(i, n) sparse[i].PB(arr[i]); 20 | FOR(i, 1, log2(n) + 1) F0R(j, n) { 21 | if(j + (1 << i) > n) sparse[j].PB(sparse[j][i - 1]); 22 | else sparse[j].PB(min(sparse[j][i - 1], sparse[j + (1 << (i - 1))][i - 1])); 23 | } 24 | } 25 | 26 | ll query(int l, int r) { 27 | int s = (int) floor(log2(r - l + 1)); 28 | return min(sparse[l][s], sparse[r + 1 - (1 << s)][s]); 29 | } 30 | }; 31 | -------------------------------------------------------------------------------- /Implementations/Data Structures/Strings/StringTokenizer.cpp: -------------------------------------------------------------------------------- 1 | struct StringTokenizer { 2 | 3 | int ptr = 0; 4 | string s; 5 | 6 | void takeString(string ss) { 7 | s = ss; 8 | ptr = 0; 9 | } 10 | 11 | string nextToken() { 12 | if(ptr == s.size()) return ""; 13 | string token = ""; 14 | while(ptr < s.size() && s[ptr] == ' ') ptr++; 15 | if(ptr == s.size()) return ""; 16 | while(ptr < s.size() && s[ptr] != ' ') token += s[ptr++]; 17 | return token; 18 | } 19 | 20 | }; 21 | -------------------------------------------------------------------------------- /Implementations/Templates/CodeForces.cpp: -------------------------------------------------------------------------------- 1 | #pragma comment(linker, "/stack:200000000") 2 | #include 3 | #include 4 | #pragma GCC optimize("Ofast") 5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 6 | 7 | #include 8 | 9 | #define F0R(i, a) for (int i = 0; i < (a); i++) 10 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 11 | #define R0F(i, a) for (int i = (a) - 1; i >= 0; i--) 12 | #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) 13 | 14 | #define ran() (rand() & 0x7FFF) 15 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 16 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 17 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 18 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 19 | 20 | #define F first 21 | #define S second 22 | #define PB push_back 23 | #define MP make_pair 24 | #define MT make_tuple 25 | #define UB upper_bound 26 | #define LB lower_bound 27 | #define X real() 28 | #define Y imag() 29 | 30 | #define INF 1e18 31 | #define PI acos(-1) 32 | 33 | #define sz(x) ((int)(x).size()) 34 | #define all(x) (x).begin(), (x).end() 35 | #define SQ(x) ((x) * (x)) 36 | 37 | using namespace std; 38 | using namespace __gnu_pbds; 39 | 40 | typedef long long ll; 41 | typedef long double ld; 42 | typedef unsigned long long ull; 43 | typedef pair pii; 44 | typedef pair pll; 45 | typedef vector vi; 46 | typedef vector vpii; 47 | typedef vector vll; 48 | typedef vector vul; 49 | typedef complex point; 50 | typedef complex cld; 51 | typedef vector vcld; 52 | 53 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; 54 | 55 | int main() { 56 | ios::sync_with_stdio(0); 57 | cin.tie(0); 58 | } 59 | -------------------------------------------------------------------------------- /Implementations/Templates/MacOS.cpp: -------------------------------------------------------------------------------- 1 | #pragma comment(linker, "/stack:200000000") 2 | #pragma GCC optimize("Ofast") 3 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | 15 | #define F0R(i, a) for (int i = 0; i < (a); i++) 16 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 17 | #define R0F(i, a) for (int i = (a) - 1; i >= 0; i--) 18 | #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) 19 | 20 | #define ran() (rand() & 0x7FFF) 21 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 22 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 23 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 24 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 25 | 26 | #define F first 27 | #define S second 28 | #define PB push_back 29 | #define MP make_pair 30 | #define MT make_tuple 31 | #define UB upper_bound 32 | #define LB lower_bound 33 | #define X real() 34 | #define Y imag() 35 | 36 | #define INF 1e18 37 | #define PI acos(-1) 38 | 39 | #define sz(x) ((int)(x).size()) 40 | #define all(x) (x).begin(), (x).end() 41 | #define SQ(x) ((x) * (x)) 42 | 43 | using namespace std; 44 | 45 | typedef long long ll; 46 | typedef long double ld; 47 | typedef unsigned long long ull; 48 | typedef pair pii; 49 | typedef pair pll; 50 | typedef vector vi; 51 | typedef vector vpii; 52 | typedef vector vll; 53 | typedef vector vul; 54 | typedef complex point; 55 | typedef complex cld; 56 | typedef vector vcld; 57 | 58 | int main() { 59 | ios::sync_with_stdio(0); 60 | cin.tie(0); 61 | } 62 | -------------------------------------------------------------------------------- /Implementations/Templates/OP_IO.cpp: -------------------------------------------------------------------------------- 1 | template void print(T t) { 2 | string s = to_string(t); 3 | for(const char c : s) _putchar_nolock(c); 4 | } 5 | 6 | template void println(T t) { 7 | print(t); 8 | _putchar_nolock('\n'); 9 | } 10 | 11 | ll nextLong() { 12 | ll res = 0; 13 | bool neg = 0; 14 | char c; 15 | while((c = _getchar_nolock()) != '-' && (c < '0' || c > '9')) {} 16 | if(c == '-') { 17 | neg = 1; 18 | c = _getchar_nolock(); 19 | } 20 | while(c >= '0' && c <= '9') { 21 | res *= 10; 22 | res += (c - '0'); 23 | c = _getchar_nolock(); 24 | } 25 | if(neg) res *= -1; 26 | return res; 27 | } 28 | 29 | int nextInt() { 30 | int res = 0; 31 | bool neg = 0; 32 | char c; 33 | while((c = _getchar_nolock()) != '-' && (c < '0' || c > '9')) {} 34 | if(c == '-') { 35 | neg = 1; 36 | c = _getchar_nolock(); 37 | } 38 | while(c >= '0' && c <= '9') { 39 | res *= 10; 40 | res += (c - '0'); 41 | c = _getchar_nolock(); 42 | } 43 | if(neg) res *= -1; 44 | return res; 45 | } 46 | 47 | string next() { 48 | string res; 49 | char c; 50 | while((c - _getchar_nolock()) == ' ' || c == '\n' || c == '\t') {} 51 | while(c != ' ' && c != '\n' && c == '\t') { 52 | res += c; 53 | c = _getchar_nolock(); 54 | } 55 | return res; 56 | } 57 | 58 | char nextChar() { 59 | char c; 60 | while((c = _getchar_nolock()) == '\n') {} 61 | return c; 62 | } 63 | -------------------------------------------------------------------------------- /Implementations/Templates/USACO.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | #define F0R(i, a) for (int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 7 | #define R0F(i, a) for (int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for (int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | 26 | #define INF 1e18 27 | #define PI acos(-1) 28 | 29 | #define sz(x) ((int)(x).size()) 30 | #define all(x) (x).begin(), (x).end() 31 | #define SQ(x) ((x) * (x)) 32 | 33 | using namespace std; 34 | using namespace __gnu_pbds; 35 | 36 | typedef long long ll; 37 | typedef long double ld; 38 | typedef unsigned long long ull; 39 | typedef pair pii; 40 | typedef pair pll; 41 | typedef vector vi; 42 | typedef vector vpii; 43 | typedef vector vll; 44 | typedef vector vul; 45 | typedef complex point; 46 | typedef complex cld; 47 | typedef vector vcld; 48 | 49 | typedef tree, rb_tree_tag, tree_order_statistics_node_update> ordered_set; 50 | 51 | int main() { 52 | ifstream cin("file.in"); 53 | ofstream cout("file.out"); 54 | } 55 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Programming Contests 2 | 3 | ## USACO 4 | http://www.usaco.org/index.php 5 | 6 | _Training Pages_: http://train.usaco.org/usacogate 7 | 8 | | Achievement | Date | 9 | | ------------------ | --------------- | 10 | | Reach Silver | December 2016 | 11 | | Reach Gold | December 2016 | 12 | | Reach Platinum | February 2017 | 13 | | Finish Training Pages | October 6, 2017 | 14 | | Place Top 10 US in a Contest | January 2018 | 15 | | Win IOI Gold | September 2020 | 16 | 17 | ## Codeforces 18 | http://codeforces.com/ 19 | 20 | _My Profile_: http://codeforces.com/profile/walnutwaldo20 21 | 22 | | Achievement | Date | 23 | | ------------------ | --------------- | 24 | | Reach Expert (1600+) | May 7, 2017 | 25 | | Reach Candidate Master (1900+) | August 24, 2017 | 26 | | Reach Master (2100+) | June 24, 2018 | 27 | | Reach International Master (2300+) | August 27, 2018 | 28 | | Reach Grandmaster (2400+) | September 2, 2018 | 29 | -------------------------------------------------------------------------------- /USACO Training Pages/README.md: -------------------------------------------------------------------------------- 1 | Finished on October 6, 2017 2 | 3 | # First Try Succesess 4 | ## Chapter 2 5 | **Section 2.3** Zero Sum 6 | 7 | **Section 2.4** Overfencing 8 | 9 | **Section 2.4** Bessie Come Home 10 | ## Chapter 3 11 | **Section 3.1** Agri-Net 12 | 13 | **Section 3.4** Home on the Range 14 | 15 | **Section 3.4** American Heritage 16 | ## Chapter 4 17 | **Section 4.1** Fence Loops 18 | 19 | **Section 4.3** Letter Game 20 | 21 | **Section 4.4** Shuttle Puzzle 22 | ## Chapter 5 23 | **Section 5.1** Starry Night 24 | 25 | **Section 5.2** Snail Trails 26 | ## Chapter 6 27 | **Section 6.1** Postal Vans 28 | 29 | **Section 6.2** Calf Flac 30 | 31 | **Section 6.4** The Primes 32 | 33 | **Section 6.4** Electric Fences 34 | 35 | **Section 6.4** Wisconsin Squares 36 | 37 | **Section 6.5** The Clocks 38 | 39 | **Section 6.5** Checker Challenge 40 | 41 | **Section 6.5** Betsy's Tour 42 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/December/Gold/optmilk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int numcows, numdays; 6 | 7 | long long st[1 << 17][2][2]; 8 | 9 | void upd(int idx) { 10 | if (!idx) { return; } 11 | for (int i = 0; i < 2; i++) { 12 | for (int j = 0; j < 2; j++) { 13 | st[idx][i][j] = max(st[2 * idx][i][0] + st[2 * idx + 1][1][j], 14 | st[2 * idx][i][1] + st[2 * idx + 1][0][j]); 15 | } 16 | } 17 | upd(idx / 2); 18 | } 19 | 20 | int main() { 21 | ifstream cin("optmilk.in"); 22 | ofstream cout("optmilk.out"); 23 | cin >> numcows >> numdays; 24 | for (int i = 0; i < numcows; i++) { 25 | cin >> st[(1 << 16) + i][0][0]; 26 | upd(((1 << 16) + i) / 2); 27 | } 28 | long long res = 0; 29 | for (int i = 0; i < numdays; i++) { 30 | int idx; 31 | cin >> idx; 32 | idx--; 33 | cin >> st[(1 << 16) + idx][0][0]; 34 | upd(((1 << 16) + idx) / 2); 35 | res += st[1][0][0]; 36 | } 37 | cout << res << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/February/Gold/boarding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | 5 | using namespace std; 6 | using namespace pb_ds; 7 | 8 | typedef tree< 9 | int, 10 | null_mapped_type, 11 | less, 12 | rb_tree_tag, 13 | // This policy updates nodes' metadata for order statistics. 14 | tree_order_statistics_node_update> 15 | set_t; 16 | 17 | int n, s[200000]; 18 | long long t[200000]; 19 | 20 | int main() { 21 | ifstream cin("boarding.in"); 22 | ofstream cout("boarding.out"); 23 | cin >> n; 24 | for (int i = 0; i < n; i++) { 25 | cin >> s[i] >> t[i]; 26 | s[i]--; 27 | } 28 | set_t s; 29 | for (int i = 1; i <= n; i++) { s.insert(i); } 30 | long long res = 0; 31 | for (int i = n - 1; i >= 0; i--) { 32 | int walktime = s[i] + n - i; 33 | long long bq = bit.query(walktime); 34 | res = max(res, bq + walktime + t[i]); 35 | bit.update(walktime + 1, bq + t[i]); 36 | } 37 | cout << res << endl; 38 | return 0; 39 | } 40 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/February/Gold/dec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, b; 6 | int dp[1 << 20]; 7 | int skill[20][20]; 8 | vector> bonuses[21]; 9 | 10 | inline void upd(int& a, int b) { a = max(a, b); } 11 | 12 | int main() { 13 | ifstream cin("dec.in"); 14 | ofstream cout("dec.out"); 15 | cin >> n >> b; 16 | for (int i = 0; i < b; i++) { 17 | int k, p, a; 18 | cin >> k >> p >> a; 19 | bonuses[k].emplace_back(p, a); 20 | } 21 | for (int i = 0; i < n; i++) { 22 | sort(bonuses[i].begin(), bonuses[i].end()); 23 | } 24 | for (int i = 0; i < n; i++) { 25 | for (int j = 0; j < n; j++) { 26 | cin >> skill[i][j]; 27 | } 28 | } 29 | dp[0] = 0; 30 | for (int i = 0; i < (1 << n); i++) { 31 | int numCows = __builtin_popcount(i); 32 | for (int j = 0; j < bonuses[numCows].size(); j++) { 33 | if (dp[i] >= bonuses[numCows][j].first) { 34 | dp[i] += bonuses[numCows][j].second; 35 | } 36 | } 37 | for (int j = 0; j < n; j++) { 38 | if (!((i >> j) & 1)) { 39 | upd(dp[i | (1 << j)], dp[i] + skill[j][numCows]); 40 | } 41 | } 42 | } 43 | cout << dp[(1 << n) - 1] << endl; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/March/Gold/fcount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, a[501]; 6 | 7 | bool possible(int excl) { 8 | vector v; 9 | for (int i = 0; i < n + 1; i++) { if (i != excl) { v.push_back(a[i]); } } 10 | sort(v.begin(), v.end()); 11 | while (!v.empty()) { 12 | vector tmp(v); 13 | v.clear(); 14 | int t = tmp.back(); 15 | if (t == 0) { break; } 16 | if (t > tmp.size() - 1 || tmp[tmp.size() - 1 - t] == 0) { return false; } 17 | int i = tmp.size() - 1 - t; 18 | int j = 0; 19 | while (i < tmp.size() - 1 || j < tmp.size() - 1 - t) { 20 | if (j == tmp.size() - 1 - t || (i < tmp.size() - 1 && tmp[i] - 1 < tmp[j])) { 21 | v.push_back(tmp[i++] - 1); 22 | } else { v.push_back(tmp[j++]); } 23 | } 24 | } 25 | return true; 26 | } 27 | 28 | int main() { 29 | ifstream cin("fcount.in"); 30 | ofstream cout("fcount.out"); 31 | cin >> n; 32 | for (int i = 0; i < n + 1; i++) { 33 | cin >> a[i]; 34 | } 35 | vector res; 36 | for (int i = 0; i < n + 1; i++) { 37 | if (possible(i)) { 38 | res.push_back(i + 1); 39 | } 40 | } 41 | cout << res.size() << "\n"; 42 | for (const int i : res) { cout << i << "\n"; } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/March/Gold/sabotage.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, m[100000]; 6 | long double total; 7 | long double pref[100000], dp[100000]; 8 | 9 | bool possible(long double prod) { 10 | pref[0] = m[0] - prod; 11 | for (int i = 1; i < n - 1; i++) { pref[i] = m[i] - prod + pref[i - 1]; } 12 | long double minpref = pref[0]; 13 | for (int i = 1; i < n - 1; i++) { 14 | if (pref[i] - minpref >= total - prod * n) { return true; } 15 | minpref = min(minpref, pref[i]); 16 | } 17 | return false; 18 | } 19 | 20 | int main() { 21 | ifstream cin("sabotage.in"); 22 | ofstream cout("sabotage.out"); 23 | cin >> n; 24 | for (int i = 0; i < n; i++) { cin >> m[i]; total += m[i]; } 25 | long double lo = 0, hi = (1 << 14); 26 | while (round(lo * 1000) != round(hi * 1000)) { 27 | long double mid = (lo + hi) / 2; 28 | if (possible(mid)) { hi = mid; } else { lo = mid; } 29 | } 30 | cout << fixed << setprecision(3) << lo << endl; 31 | return 0; 32 | } 33 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/November/Gold/empty.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n, k; 6 | int cnt[3000000]; 7 | 8 | int main() { 9 | ifstream cin("empty.in"); 10 | ofstream cout("empty.out"); 11 | cin >> n >> k; 12 | int cowcnt = 0; 13 | for (int i = 0; i < k; i++) { 14 | int x, y; 15 | long long a, b; 16 | cin >> x >> y >> a >> b; 17 | for (int j = 1; j <= y; j++) { 18 | cnt[(a * j + b) % n] += x; 19 | } 20 | } 21 | int running = 0; 22 | for (int i = 0; i < 2 * n; i++) { 23 | running += cnt[i % n]; 24 | cnt[i % n] = 0; 25 | if (running > 0) { 26 | cnt[i % n]++; 27 | running--; 28 | } 29 | } 30 | for (int i = 0; i < n; i++) { 31 | if (cnt[i] == 0) { 32 | cout << i << endl; 33 | exit(0); 34 | } 35 | } 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/November/Gold/nochange.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef pair pii; 6 | 7 | int k, n; 8 | long long v[16]; 9 | long long c[100000]; 10 | int dp[1 << 16]; 11 | 12 | void upd(int& a, int b) { a = max(a, b); } 13 | 14 | int main() { 15 | ifstream cin("nochange.in"); 16 | ofstream cout("nochange.out"); 17 | cin >> k >> n; 18 | for (int i = 0; i < k; i++) { 19 | cin >> v[i]; 20 | } 21 | for (int i = 0; i < n; i++) { 22 | cin >> c[i]; 23 | if (i) { c[i] += c[i - 1]; } 24 | } 25 | for (int i = 0; i < (1 << k); i++) { dp[i] = 0; } 26 | long long res = -1; 27 | for (int popcount = k; popcount >= 0; popcount--) { 28 | for (int mask = 0; mask < (1 << k); mask++) { 29 | if (__builtin_popcount(mask) == popcount) { 30 | long long base = dp[mask] ? c[dp[mask] - 1] : 0; 31 | long long curr = 0; 32 | for (int i = 0; i < k; i++) { 33 | if ((mask >> i) & 1) { 34 | curr += v[i]; 35 | upd(dp[mask ^ (1 << i)], (int)(upper_bound(c, c + n, base + v[i]) - c)); 36 | } 37 | } 38 | if (dp[mask] == n) { res = max(res, curr); } 39 | } 40 | } 41 | } 42 | cout << res << endl; 43 | } 44 | -------------------------------------------------------------------------------- /USACO/2013-2014 Season/November/Gold/sight.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define PI acos(-1) 4 | 5 | using namespace std; 6 | 7 | #define MAXN 50000 8 | 9 | int n, r; 10 | set s; 11 | 12 | long double bringInRange(long double angle) { 13 | while (angle < 0) { angle += 2 * PI; } 14 | while (angle >= 2 * PI) { angle -= 2 * PI; } 15 | return angle; 16 | } 17 | 18 | int main() { 19 | ifstream cin("sight.in"); 20 | ofstream cout("sight.out"); 21 | cin >> n >> r; 22 | vector> vec; 23 | for (int i = 0; i < n; i++) { 24 | long long x, y; 25 | cin >> x >> y; 26 | long double dis = sqrt((long double)(x * x + y * y)); 27 | long double arg = atan2(y, x); 28 | long double angle = acos(r / dis); 29 | 30 | long double inAngle = bringInRange(arg - angle); 31 | long double outAngle = bringInRange(arg + angle); 32 | if (inAngle > outAngle) { 33 | s.insert(outAngle); 34 | vec.emplace_back(inAngle, outAngle + 2 * PI); 35 | } else { 36 | vec.emplace_back(inAngle, outAngle); 37 | } 38 | } 39 | long long res = 0; 40 | sort(vec.begin(), vec.end()); 41 | for (const pair p : vec) { 42 | double angle = p.first; 43 | while (!s.empty() && *s.begin() < angle) { 44 | s.erase(s.begin()); 45 | } 46 | res += s.size(); 47 | s.insert(p.second); 48 | } 49 | cout << res << endl; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/December/Gold/cowjog.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | int n; 6 | long long t; 7 | 8 | int main() { 9 | ifstream fin("cowjog.in"); 10 | ofstream fout("cowjog.out"); 11 | fin >> n >> t; 12 | vector> vec; 13 | for (int i = 0; i < n; i++) { 14 | long long p, s; 15 | fin >> p >> s; 16 | long long v = p + s * t; 17 | vec.emplace_back(v, -p); 18 | } 19 | sort(vec.begin(), vec.end()); 20 | set front; 21 | int res = 0; 22 | for (int i = 0; i < n; i++) { 23 | long long v = -vec[i].second; 24 | auto ptr = front.lower_bound(v); 25 | if (ptr != front.begin()) { 26 | ptr--; 27 | front.erase(ptr); 28 | } else { res++; } 29 | front.insert(v); 30 | } 31 | fout << res << endl; 32 | fout.close(); 33 | return 0; 34 | } 35 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/December/README.md: -------------------------------------------------------------------------------- 1 | # Gold 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Guard Mark| Solved | C++ | 5 | | Marathon | Solved | C++ | 6 | | Cow Jog | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Piggyback | Unsolved | Missing | 11 | | Marathon | Unsolved | Missing | 12 | | Cow Jog | Unsolved | Missing | 13 | # Bronze 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Marathon| Unsolved | Missing | 17 | | Crosswords | Unsolved | Missing | 18 | | Cow Jog | Unsolved | Missing | 19 | | Learning by Example | Unsolved | Missing | 20 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/Bronze/censor.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | #define MAXN 1000000 45 | 46 | int len = 0; 47 | string s, t; 48 | char res[MAXN]; 49 | 50 | int main() { 51 | ifstream fin("censor.in"); 52 | ofstream fout("censor.out"); 53 | fin >> s >> t; 54 | F0R(i, sz(s)) { 55 | res[len] = s[i]; 56 | len++; 57 | if(len >= sz(t)) { 58 | bool isEq = 1; 59 | F0R(i, sz(t)) if(res[len - sz(t) + i] != t[i]) isEq = 0; 60 | if(isEq) len -= sz(t); 61 | } 62 | } 63 | F0R(i, len) fout << res[i]; 64 | fout << endl; 65 | } 66 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/Bronze/cow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | int n; 45 | ll cnt[3]; 46 | string s; 47 | 48 | int main() { 49 | ifstream fin("cow.in"); 50 | ofstream fout("cow.out"); 51 | fin >> n >> s; 52 | F0R(i, n) { 53 | if(s[i] == 'C') cnt[0]++; 54 | else if(s[i] == 'O') cnt[1] += cnt[0]; 55 | else if(s[i] == 'W') cnt[2] += cnt[1]; 56 | } 57 | fout << cnt[2] << endl; 58 | } 59 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/Bronze/hopscotch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | #define MAXN 15 45 | 46 | int r, c; 47 | char a[MAXN][MAXN]; 48 | 49 | int solve(int i, int j) { 50 | if(i == r - 1 && j == c - 1) return 1; 51 | int res = 0; 52 | FOR(row, i + 1, r) FOR(col, j + 1, c) if(a[i][j] != a[row][col]) res += solve(row, col); 53 | return res; 54 | } 55 | 56 | int main() { 57 | ifstream fin("hopscotch.in"); 58 | ofstream fout("hopscotch.out"); 59 | fin >> r >> c; 60 | F0R(i, r) F0R(j, c) fin >> a[i][j]; 61 | fout << solve(0, 0) << endl; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/README.md: -------------------------------------------------------------------------------- 1 | # Gold 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Cow Hopscotch | Solved | C++ | 5 | | Censoring | Solved | C++ | 6 | | Fencing the Herd | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Censoring | Solved | C++ | 11 | | Cow Hopscotch | Solved | C++ | 12 | | Superbull | Solved | C++ | 13 | # Bronze 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Censoring | Solved | C++ | 17 | | COW | Solved | C++ | 18 | | Cow Hopscotch | Solved | C++ | 19 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/Silver/hopscotch.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | #define MAXN 100 45 | #define MOD 1000000007 46 | 47 | int r, c, k; 48 | ll a[MAXN][MAXN], dp[MAXN][MAXN]; 49 | 50 | void madd(ll& a, ll b) { a = (a + b) % MOD; } 51 | 52 | int main() { 53 | ifstream fin("hopscotch.in"); 54 | ofstream fout("hopscotch.out"); 55 | fin >> r >> c >> k; 56 | F0R(i, r) F0R(j, c) { 57 | fin >> a[i][j]; 58 | a[i][j]--; 59 | } 60 | dp[r - 1][c - 1] = 1; 61 | R0F(i, r - 1) R0F(j, c - 1) FOR(col, j + 1, c) FOR(row, i + 1, r) if(a[row][col] != a[i][j]) madd(dp[i][j], dp[row][col]); 62 | fout << dp[0][0] << endl; 63 | } 64 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/February/Silver/superbull.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | 45 | #define MAXN 2000 46 | 47 | int n, a[MAXN], dis[MAXN]; 48 | bool vis[MAXN]; 49 | ll res; 50 | 51 | int main() { 52 | ifstream fin("superbull.in"); 53 | ofstream fout("superbull.out"); 54 | fin >> n; 55 | F0R(i, n) fin >> a[i]; 56 | F0R(i, n) { 57 | int maxI = 0; 58 | F0R(j, n) if(!vis[j] && dis[j] > dis[maxI]) maxI = j; 59 | res += dis[maxI]; 60 | vis[maxI] = 1; 61 | F0R(j, n) if(!vis[j]) dis[j] = max(dis[j], a[maxI] ^ a[j]); 62 | } 63 | fout << res << endl; 64 | } 65 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/January/README.md: -------------------------------------------------------------------------------- 1 | # Gold 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Cow Rectangles | Solved | C++ | 5 | | Moovie Mooving | Solved | C++ | 6 | | Grass Cownoisseur | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Stampede | Solved | C++ | 11 | | Cow Routing | Solved | C++ | 12 | | Meeting Time | Solved | C++ | 13 | # Bronze 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Cow Routing | Unsolved | Missing | 17 | | Cow Routing II | Unsolved | Missing | 18 | | It's All About the Base | Unsolved | Missing | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/README.md: -------------------------------------------------------------------------------- 1 | # US Open 2 | | Division | Number Implemented | Number Missing | 3 | | ------------- |:---------------:| :--------------:| 4 | | **Gold** | 3 | 0 | 5 | | **Silver** | 3 | 0 | 6 | | **Bronze** | 4 | 0 | 7 | # February 8 | | Division | Number Implemented | Number Missing | 9 | | ------------- |:---------------:| :--------------:| 10 | | **Gold** | 3 | 0 | 11 | | **Silver** | 3 | 0 | 12 | | **Bronze** | 3 | 0 | 13 | # January 14 | | Division | Number Implemented | Number Missing | 15 | | ------------- |:---------------:| :--------------:| 16 | | **Gold** | 3 | 0 | 17 | | **Silver** | 3 | 0 | 18 | | **Bronze** | 0 | 4 | 19 | # December 20 | | Division | Number Implemented | Number Missing | 21 | | ------------- |:---------------:| :--------------:| 22 | | **Gold** | 2 | 1 | 23 | | **Silver** | 0 | 3 | 24 | | **Bronze** | 0 | 4 | 25 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/US Open/Bronze/geteven.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < a; i++) 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | int n; 10 | ll res, b[2], e[2], s[2], i[2], g[2], o[2], m[2]; 11 | 12 | int main() { 13 | ifstream fin("geteven.in"); 14 | ofstream fout("geteven.out"); 15 | fin >> n; 16 | F0R(x, n) { 17 | char c; 18 | int v; 19 | fin >> c >> v; 20 | v = ((v % 2) + 2) % 2; 21 | switch(c){ 22 | case 'B': b[v]++; break; 23 | case 'E': e[v]++; break; 24 | case 'S': s[v]++; break; 25 | case 'I': i[v]++; break; 26 | case 'G': g[v]++; break; 27 | case 'O': o[v]++; break; 28 | case 'M': m[v]++; break; 29 | } 30 | } 31 | F0R(B, 2) F0R(E, 2) F0R(S, 2) F0R(I, 2) F0R(G, 2) F0R(O, 2) F0R(M, 2) if((B+E+S+S+I+E) * (G+O+E+S) * (M+O+O) % 2 == 0) res += b[B] * e[E] * s[S] * i[I] * g[G] * o[O] * m[M]; 32 | fout << res << endl; 33 | } 34 | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/US Open/README.md: -------------------------------------------------------------------------------- 1 | # Gold 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Googol | Solved | C++ | 5 | | Palindromic Paths | Solved | C++ | 6 | | Trapped in the Haybales | Solved | C++ (RMQ and range marking) | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Bessie Goes Moo | Solved | C++ | 11 | | Trapped in the Haybales | Solved | C++ | 12 | | Bessie's Birthday Buffet | Solved | C++ | 13 | # Bronze 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Moocryption | Solved | C++ | 17 | | Bessie Gets Even | Solved | C++ | 18 | | Trapped in the Haybales | Solved | C++ | 19 | | Palindromic Paths | Solved | C++ | -------------------------------------------------------------------------------- /USACO/2014-2015 Season/US Open/SIlver/bgm.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < a; i++) 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | int n; 10 | ll res, b[7], e[7], s[7], i[7], g[7], o[7], m[7]; 11 | 12 | int main() { 13 | ifstream fin("bgm.in"); 14 | ofstream fout("bgm.out"); 15 | fin >> n; 16 | F0R(x, n) { 17 | char c; 18 | int v; 19 | fin >> c >> v; 20 | v = ((v % 7) + 7) % 7; 21 | switch(c){ 22 | case 'B': b[v]++; break; 23 | case 'E': e[v]++; break; 24 | case 'S': s[v]++; break; 25 | case 'I': i[v]++; break; 26 | case 'G': g[v]++; break; 27 | case 'O': o[v]++; break; 28 | case 'M': m[v]++; break; 29 | } 30 | } 31 | F0R(B, 7) F0R(E, 7) F0R(S, 7) F0R(I, 7) F0R(G, 7) F0R(O, 7) F0R(M, 7) if((B+E+S+S+I+E) * (G+O+E+S) * (M+O+O) % 7 == 0) res += b[B] * e[E] * s[S] * i[I] * g[G] * o[O] * m[M]; 32 | fout << res << endl; 33 | } 34 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Bronze/paint.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int a, b, c, d; 4 | int main() { 5 | ifstream fin("paint.in"); 6 | ofstream fout("paint.out"); 7 | fin >> a >> b >> c >> d; 8 | if(c >= b || d <= a) fout << (b - a) + (d - c) << "\n"; 9 | else fout << max(d,b) - min(a , c) << "\n"; 10 | return 0; 11 | } 12 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Bronze/speeding.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int n, m; 26 | vector road, bessie; 27 | 28 | int main() { 29 | ifstream fin("speeding.in"); 30 | ofstream fout("speeding.out"); 31 | 32 | fin >> n >> m; 33 | F0R(i, n) { 34 | int l, s; 35 | fin >> l >> s; 36 | road.PB(MP(s, l)); 37 | } 38 | F0R(i, m) { 39 | int l, s; 40 | fin >> l >> s; 41 | bessie.PB(MP(s, l)); 42 | } 43 | int maxOver = 0; 44 | int i = 0, j = 0; 45 | while(i < n && j < m) { 46 | maxOver = max(maxOver, bessie[j].F - road[i].F); 47 | int travel = min(bessie[j].S, road[i].S); 48 | bessie[j].S -= travel; 49 | road[i].S -= travel; 50 | if(bessie[j].S == 0) j++; 51 | if(road[i].S == 0) i++; 52 | } 53 | fout << maxOver << "\n"; 54 | return 0; 55 | } 56 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Gold/cardgame.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 50000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, numWin; 27 | bool inE[MAXN * 2]; 28 | vi firstHalf, secondHalf; 29 | 30 | int main() { 31 | ifstream fin("cardgame.in"); 32 | ofstream fout("cardgame.out"); 33 | fin >> n; 34 | F0R(i, n / 2) { 35 | int cardNum; 36 | fin >> cardNum; 37 | inE[--cardNum] = true; 38 | firstHalf.PB(cardNum); 39 | } 40 | F0R(i, n / 2) { 41 | int cardNum; 42 | fin >> cardNum; 43 | inE[--cardNum] = true; 44 | secondHalf.PB(cardNum); 45 | } 46 | int candidateCard = -1; 47 | F0R(i, n / 2) { 48 | while(inE[++candidateCard]){} 49 | secondHalf.PB(candidateCard); 50 | } 51 | F0R(i, n / 2) { 52 | while(inE[++candidateCard]){} 53 | firstHalf.PB(candidateCard); 54 | } 55 | sort(firstHalf.begin(), firstHalf.end()); 56 | sort(secondHalf.rbegin(), secondHalf.rend()); 57 | int undef = 0; 58 | F0R(i, n) { 59 | if(inE[firstHalf[i]]) undef++; 60 | else if(undef > 0) { 61 | numWin++; 62 | undef--; 63 | } 64 | } 65 | undef = 0; 66 | F0R(i, n) { 67 | if(inE[secondHalf[i]]) undef++; 68 | else if(undef > 0) { 69 | numWin++; 70 | undef--; 71 | } 72 | } 73 | fout << numWin << "\n"; 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Gold/feast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXT 5000000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | bool possible[MAXT + 1], possibleWithWater[MAXT + 1]; 27 | int t, a, b, maximumFullness; 28 | 29 | int main() { 30 | ifstream fin("feast.in"); 31 | ofstream fout("feast.out"); 32 | 33 | fin >> t >> a >> b; 34 | possible[0] = true; 35 | F0R(i, t + 1) { 36 | if(possible[i]) { 37 | possibleWithWater[i/2] = true; 38 | if(i + a <= t) possible[i + a] = true; 39 | if(i + b <= t) possible[i + b] = true; 40 | maximumFullness = max(maximumFullness, i); 41 | } 42 | } 43 | F0R(i, t + 1) { 44 | if(possibleWithWater[i]) { 45 | if(i + a <= t) possibleWithWater[i + a] = true; 46 | if(i + b <= t) possibleWithWater[i + b] = true; 47 | maximumFullness = max(maximumFullness, i); 48 | } 49 | } 50 | fout << maximumFullness << "\n"; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Fence Painting | Solved | C++ | 5 | | Speeding Ticket | Solved | C++ | 6 | | Contaminated Milk | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Switching on the Lights | Solved | C++ | 11 | | High Card Wins | Solved | C++ | 12 | | Breed Counting | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | High Card Low Card | Solved | C++ | 17 | | Fruit Feast | Solved | C++ | 18 | | Bessie's Dream | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Max Flow | Solved | C++ | 23 | | High Card Low Card | Solved | C++ | 24 | | Counting Haybales | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Silver/bcount.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, q, bcount[3][MAXN + 1]; 27 | 28 | int main() { 29 | ifstream fin("bcount.in"); 30 | ofstream fout("bcount.out"); 31 | fin >> n >> q; 32 | FOR(i, 1, n + 1) { 33 | F0R(j, 3) bcount[j][i] = bcount[j][i - 1]; 34 | int b; 35 | fin >> b; 36 | bcount[--b][i]++; 37 | } 38 | F0R(i, q) { 39 | int a, b; 40 | fin >> a >> b; 41 | fout << bcount[0][b] - bcount[0][a - 1] << " " << bcount[1][b] - bcount[1][a - 1] << " " << bcount[2][b] - bcount[2][a - 1] << "\n"; 42 | } 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/December/Silver/highcard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 50000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n; 27 | bool inE[MAXN * 2]; 28 | 29 | int main() { 30 | ifstream fin("highcard.in"); 31 | ofstream fout("highcard.out"); 32 | fin >> n; 33 | F0R(i, n) { 34 | int cardNum; 35 | fin >> cardNum; 36 | inE[cardNum - 1] = true; 37 | } 38 | int numUnbeaten = 0; 39 | int numWins = 0; 40 | F0R(i, 2 * n) { 41 | if(inE[i]) numUnbeaten++; 42 | if(!inE[i]) 43 | if(numUnbeaten > 0) { 44 | numWins++; 45 | numUnbeaten--; 46 | } 47 | } 48 | fout << numWins << "\n"; 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Bronze/balancing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, b; 27 | vector points; 28 | 29 | int main() { 30 | ifstream fin("balancing.in"); 31 | ofstream fout("balancing.out"); 32 | fin >> n >> b; 33 | set xs, ys; 34 | F0R(i, n) { 35 | int x, y; 36 | fin >> x >> y; 37 | points.PB(MP(x,y)); 38 | xs.insert(x - 1); 39 | xs.insert(x + 1); 40 | ys.insert(y - 1); 41 | ys.insert(y + 1); 42 | } 43 | int M = INT_MAX; 44 | for(const int x: xs) { 45 | for(const int y: ys) { 46 | int inQuad[2][2] {{0, 0}, {0, 0}}; 47 | for(const pii point: points) { 48 | int a, b; 49 | if(point.F < x) a = 0; 50 | else a = 1; 51 | if(point.S < y) b = 0; 52 | else b = 1; 53 | inQuad[a][b]++; 54 | } 55 | M = min(M, max(max(inQuad[0][0], inQuad[0][1]), max(inQuad[1][0], inQuad[1][1]))); 56 | } 57 | } 58 | fout << M << "\n"; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Bronze/cbarn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 1000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, r[MAXN]; 27 | 28 | int main() { 29 | ifstream fin("cbarn.in"); 30 | ofstream fout("cbarn.out"); 31 | fin >> n; 32 | F0R(i, n) fin >> r[i]; 33 | int minTravel = INT_MAX; 34 | F0R(i, n) { 35 | int cTravel = 0; 36 | F0R(j, n) { 37 | int dis = (j + n - i) % n; 38 | cTravel += r[j] * dis; 39 | } 40 | minTravel = min(minTravel, cTravel); 41 | } 42 | fout << minTravel << "\n"; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Bronze/pails.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXM 1000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int x, y, m; 27 | bool possible[MAXM + 1]; 28 | 29 | int main() { 30 | ifstream fin("pails.in"); 31 | ofstream fout("pails.out"); 32 | fin >> x >> y >> m; 33 | possible[0] = true; 34 | int maxPossible = 0; 35 | F0R(i, m + 1) { 36 | if(possible[i]) { 37 | maxPossible = i; 38 | if(i + x <= m) possible[i + x] = true; 39 | if(i + y <= m) possible[i + y] = true; 40 | } 41 | } 42 | fout << maxPossible << "\n"; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Gold/cbarn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, c[MAXN], in[MAXN]; 27 | 28 | void force(int place, int id) { 29 | if(in[place] == -1) { 30 | in[place] = id; 31 | return; 32 | } 33 | int other = in[place]; 34 | if(((place + n - other) % n) >= ((place + n - id) % n)) { 35 | int temp = id; 36 | id = other; 37 | other = temp; 38 | } 39 | in[place] = id; 40 | force((place + 1) % n, other); 41 | } 42 | 43 | int main() { 44 | ifstream fin("cbarn.in"); 45 | ofstream fout("cbarn.out"); 46 | fin >> n; 47 | F0R(i, n) { 48 | fin >> c[i]; 49 | in[i] = -1; 50 | } 51 | F0R(i, n) { 52 | F0R(j, c[i]) { 53 | force((i + j) % n, i); 54 | } 55 | } 56 | ll cost = 0; 57 | F0R(i, n) { 58 | cost += SQ((ll)(i + n - in[i]) % n); 59 | } 60 | fout << cost << "\n"; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Gold/cbarn2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, k; 27 | ll r[MAXN]; 28 | int main() { 29 | ifstream fin("cbarn2.in"); 30 | ofstream fout("cbarn2.out"); 31 | fin >> n >> k; 32 | F0R(i, n) fin >> r[i]; 33 | ll minCost = -1; 34 | F0R(s, n) { 35 | ll dp[n][k + 1]; 36 | F0R(i, n) F0R(j, k) dp[i][j] = -1; 37 | dp[0][0] = 0; 38 | F0R(l, n) { 39 | dp[0][0] += (ll)l * r[(s + l) % n]; 40 | } 41 | F0R(l, n) FOR(j, 1, k) { 42 | ll m = -1; 43 | F0R(last, l) { 44 | if(dp[last][j - 1] != -1) { 45 | ll cost = dp[last][j - 1]; 46 | FOR(l2, l, n) cost -= (l - last) * r[(s + l2) % n]; 47 | if(m == -1 || cost < m) m = cost; 48 | } 49 | } 50 | dp[l][j] = m; 51 | } 52 | F0R(i, n) F0R(j, k) if(dp[i][j] != -1 && (dp[i][j] < minCost || minCost == -1)) minCost = dp[i][j]; 53 | } 54 | fout << minCost << "\n"; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Milk Pails | Solved | C++ | 5 | | Circular Barn | Solved | C++ | 6 | | Load Balancing | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Circular Barn | Solved | C++ | 11 | | Load Balancing | Solved | C++ | 12 | | Milk Pails | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Circular Barn | Solved | C++ | 17 | | Circular Barn Revisited | Solved | C++ | 18 | | Fenced In | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Load Balancing | Solved | C++ | 23 | | Fenced In | Solved | C++ | 24 | | Circular Barn | Solved | C++ (Divide and Conquer) and C++ (Convex Hull) | 25 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Silver/cbarn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 1000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, c[MAXN], in[MAXN]; 27 | 28 | void force(int place, int id) { 29 | if(in[place] == -1) { 30 | in[place] = id; 31 | return; 32 | } 33 | int other = in[place]; 34 | if(((place + n - other) % n) >= ((place + n - id) % n)) { 35 | int temp = id; 36 | id = other; 37 | other = temp; 38 | } 39 | in[place] = id; 40 | force((place + 1) % n, other); 41 | } 42 | 43 | int main() { 44 | ifstream fin("cbarn.in"); 45 | ofstream fout("cbarn.out"); 46 | fin >> n; 47 | F0R(i, n) { 48 | fin >> c[i]; 49 | in[i] = -1; 50 | } 51 | F0R(i, n) { 52 | F0R(j, c[i]) { 53 | force((i + j) % n, i); 54 | } 55 | } 56 | int cost = 0; 57 | F0R(i, n) { 58 | cost += SQ((i + n - in[i]) % n); 59 | } 60 | fout << cost << "\n"; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/February/Silver/pails.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXX 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int x, y, k, m; 27 | int dp[MAXX + 1][MAXX + 1]; 28 | queue> q; 29 | 30 | void tryAdding(int inX, int inY, int moves) { 31 | if(dp[inX][inY] != -1) return; 32 | dp[inX][inY] = moves; 33 | q.push(MT(inX, inY, moves)); 34 | } 35 | 36 | int main() { 37 | ifstream fin("pails.in"); 38 | ofstream fout("pails.out"); 39 | fin >> x >> y >> k >> m; 40 | F0R(i, x + 1) F0R(j, y + 1) dp[i][j] = -1; 41 | dp[0][0] = 0; 42 | q.push(MT(0, 0, 0)); 43 | int closest = INT_MAX; 44 | while(!q.empty()) { 45 | tuple state = q.front(); 46 | q.pop(); 47 | int inX = get<0>(state); 48 | int inY = get<1>(state); 49 | int movesMade = get<2>(state); 50 | closest = min(closest, abs(m - inX - inY)); 51 | if(movesMade < k) { 52 | tryAdding(0, inY, movesMade + 1); 53 | tryAdding(inX, 0, movesMade + 1); 54 | int pourToX = min(x - inX, inY); 55 | int pourToY = min(y - inY, inX); 56 | tryAdding(inX + pourToX, inY - pourToX, movesMade + 1); 57 | tryAdding(inX - pourToY, inY + pourToY, movesMade + 1); 58 | tryAdding(x, inY, movesMade + 1); 59 | tryAdding(inX, y, movesMade + 1); 60 | } 61 | } 62 | fout << closest << "\n"; 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Bronze/angry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n; 27 | int bales[MAXN]; 28 | 29 | int rightEnd(int i, int x) { 30 | int next = UB(bales, bales + n, bales[i] + x) - bales - 1; 31 | if(next > i) return rightEnd(next, x + 1); 32 | return i; 33 | } 34 | 35 | int leftEnd(int i, int x) { 36 | int next = LB(bales, bales + n, bales[i] - x) - bales; 37 | if(next < i) return leftEnd(next, x + 1); 38 | return i; 39 | } 40 | 41 | int main() { 42 | ifstream fin("angry.in"); 43 | ofstream fout("angry.out"); 44 | fin >> n; 45 | F0R(i, n) fin >> bales[i]; 46 | sort(bales, bales + n); 47 | int res = 0; 48 | F0R(i, n) res = max(res, rightEnd(i, 1) - leftEnd(i, 1) + 1); 49 | fout << res << "\n"; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Bronze/mowing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int n; 26 | int changeX[4] {1, 0, -1, 0}, changeY[4] {0, 1, 0, -1}; 27 | map lastTime; 28 | 29 | int idOf(char c) { 30 | int id = 0; 31 | switch(c) { 32 | case 'S': 33 | id++; 34 | case 'W': 35 | id++; 36 | case 'N': 37 | id++; 38 | } 39 | return id; 40 | } 41 | 42 | int main() { 43 | ifstream fin("mowing.in"); 44 | ofstream fout("mowing.out"); 45 | fin >> n; 46 | pii curPos = MP(0, 0); 47 | int time = 0; 48 | lastTime.insert(MP(curPos, time)); 49 | int timeNeeded = -1; 50 | F0R(i, n) { 51 | char dir; 52 | int steps; 53 | fin >> dir >> steps; 54 | int dID = idOf(dir); 55 | F0R(j, steps) { 56 | time++; 57 | curPos.F += changeX[dID]; 58 | curPos.S += changeY[dID]; 59 | if(lastTime.count(curPos) && (timeNeeded > time - lastTime[curPos] || timeNeeded == -1)) 60 | timeNeeded = time - lastTime[curPos]; 61 | if(!lastTime.count(curPos)) lastTime.insert(MP(curPos, time)); 62 | lastTime[curPos] = time; 63 | } 64 | } 65 | fout << timeNeeded << endl; 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Bronze/promote.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | using namespace std; 3 | int totalStart, totalEnd, s[4], e[4], promoted[4]; 4 | int main() { 5 | ifstream fin("promote.in"); 6 | ofstream fout("promote.out"); 7 | for(int i = 0; i < 4; i++) { 8 | fin >> s[i] >> e[i]; 9 | totalStart += s[i]; 10 | totalEnd += e[i]; 11 | } 12 | promoted[0] = totalEnd - totalStart; 13 | for(int i = 1; i < 4; i++) { 14 | promoted[i] = promoted[i - 1] + s[i - 1] - e[i - 1]; 15 | } 16 | for(int i = 1; i < 4; i++) fout << promoted[i] << "\n"; 17 | } 18 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Platinum/fortmoo.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 200 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int h, w; 27 | int lChain[MAXN][MAXN], uChain[MAXN][MAXN]; 28 | 29 | int main() { 30 | ifstream fin("fortmoo.in"); 31 | ofstream fout("fortmoo.out"); 32 | fin >> h >> w; 33 | F0R(i, h) F0R(j, w) { 34 | char c; 35 | fin >> c; 36 | if(j > 0 && c == '.') { 37 | lChain[j][i] = lChain[j - 1][i]; 38 | } 39 | if(i > 0 && c == '.') { 40 | uChain[j][i] = uChain[j][i - 1]; 41 | } 42 | if(c =='.') { 43 | lChain[j][i]++; 44 | uChain[j][i]++; 45 | } 46 | } 47 | int maxArea = 0; 48 | F0R(y2, h) F0R(x2, w) { 49 | if(lChain[x2][y2] == 0) continue; 50 | FOR(width, 1, lChain[x2][y2] + 1) { 51 | ROF(height, 1, min(uChain[x2][y2], uChain[x2 - width + 1][y2]) + 1) { 52 | if(lChain[x2][y2 - height + 1] >= width) { 53 | maxArea = max(maxArea, width * height); 54 | break; 55 | } 56 | } 57 | } 58 | } 59 | fout << maxArea << "\n"; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Promotion Counting | Solved | C++ | 5 | | Angry Cows | Solved | C++ | 6 | | Mowing The Field | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Angry Cows | Solved | C++ | 11 | | Subsequences Summing to Sevens | Solved | C++ | 12 | | Building Gates | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Angry Cows | Solved | C++ | 17 | | Radio Contact | Solved | C++ | 18 | | Lights Out | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Fort Moo | Solved | C++ | 23 | | Mowing The Field | Solved | C++ | 24 | | Lights Out | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Silver/angry.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 50000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, k; 27 | ll bales[MAXN]; 28 | 29 | bool works(int r) { 30 | int i = 0; 31 | int cowsLeft = k; 32 | while(i < n && cowsLeft > 0) { 33 | i = UB(bales, bales + n, bales[i] + 2 * r) - bales; 34 | cowsLeft--; 35 | } 36 | if(i == n) return true; 37 | return false; 38 | } 39 | 40 | int main() { 41 | ifstream fin("angry.in"); 42 | ofstream fout("angry.out"); 43 | fin >> n >> k; 44 | F0R(i, n) fin >> bales[i]; 45 | sort(bales, bales + n); 46 | ll lo = 1, hi = 1e9; 47 | while(lo < hi) { 48 | ll mid = (lo + hi) / 2; 49 | if(works(mid)) { 50 | hi = mid; 51 | } else { 52 | lo = mid + 1; 53 | } 54 | } 55 | fout << lo << "\n"; 56 | return 0; 57 | } 58 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Silver/div7.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 50000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | ll n, num[MAXN], tmod7, l[7], r[7]; 27 | 28 | int main() { 29 | ifstream fin("div7.in"); 30 | ofstream fout("div7.out"); 31 | fin >> n; 32 | F0R(i, n) { 33 | fin >> num[i]; 34 | tmod7 = (tmod7 + num[i]) % 7; 35 | } 36 | F0R(i, 7) { 37 | l[i] = -1; 38 | r[i] = -1; 39 | } 40 | int sumMod7 = 0; 41 | l[0] = 0; 42 | r[0] = 0; 43 | F0R(i, n) { 44 | sumMod7 = (sumMod7 + num[i]) % 7; 45 | if(l[sumMod7] == -1) l[sumMod7] = i + 1; 46 | } 47 | sumMod7 = 0; 48 | F0R(i, n) { 49 | sumMod7 = (sumMod7 + num[n - 1 - i]) % 7; 50 | if(r[sumMod7] == -1) r[sumMod7] = i + 1; 51 | } 52 | ll maxLength = 0; 53 | F0R(i, 7) { 54 | int extraNeeded = (tmod7 - i + 7) % 7; 55 | if(r[i] != -1 && l[extraNeeded] != -1) maxLength = max(maxLength, n - r[i] - l[extraNeeded]); 56 | } 57 | fout << maxLength << "\n"; 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/January/Silver/gates.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int n, changeX[4] {1, 0, -1, 0}, changeY[4] {0, 1, 0, -1}; 26 | set seen; 27 | set> seen2; 28 | 29 | int getID(char c) { 30 | int id = 0; 31 | switch(c) { 32 | case 'S' : 33 | id++; 34 | case 'W' : 35 | id++; 36 | case 'N' : 37 | id++; 38 | } 39 | return id; 40 | } 41 | 42 | int main() { 43 | ifstream fin("gates.in"); 44 | ofstream fout("gates.out"); 45 | fin >> n; 46 | pii curPos = {0, 0}; 47 | seen.insert(curPos); 48 | int numGates = 0; 49 | F0R(i, n) { 50 | char dir; 51 | fin >> dir; 52 | int dID = getID(dir); 53 | pii nextPos = {curPos.F + changeX[dID], curPos.S + changeY[dID]}; 54 | pair edge = {curPos, dID}; 55 | if(seen.count(nextPos) && !seen2.count(edge) && !seen2.count({nextPos, (dID + 2) % 4})) numGates++; 56 | seen.insert(nextPos); 57 | seen2.insert(edge); 58 | curPos = nextPos; 59 | } 60 | fout << numGates << "\n"; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/Bronze/diamond.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define X real() 18 | #define Y imag() 19 | 20 | using namespace std; 21 | 22 | typedef long long ll; 23 | typedef long double ld; 24 | typedef pair pii; 25 | typedef vector vi; 26 | typedef complex point; 27 | 28 | int n, k; 29 | int numBelow[10001]; 30 | 31 | int main() { 32 | ifstream fin("diamond.in"); 33 | ofstream fout("diamond.out"); 34 | fin >> n >> k; 35 | F0R(i, n) { 36 | int a; 37 | fin >> a; 38 | numBelow[a]++; 39 | } 40 | FOR(i, 1, 10001) numBelow[i] += numBelow[i - 1]; 41 | int res = numBelow[k]; 42 | FOR(i, k + 1, 10001) res = max(res, numBelow[i] - numBelow[i - k - 1]); 43 | fout << res << "\n"; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/Gold/248.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define X real() 18 | #define Y imag() 19 | #define MAXN 248 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef long double ld; 25 | typedef pair pii; 26 | typedef vector vi; 27 | typedef complex point; 28 | 29 | int n, dp[MAXN][MAXN]; 30 | 31 | int main() { 32 | ifstream fin("248.in"); 33 | ofstream fout("248.out"); 34 | fin >> n; 35 | int res = 0; 36 | F0R(i, n) { 37 | fin >> dp[i][i]; 38 | res = max(res, dp[i][i]); 39 | } 40 | FOR(length, 1, n) { 41 | F0R(s, n - length) { 42 | int e = s + length; 43 | FOR(i, s, e) { 44 | if(dp[s][i] == dp[i + 1][e]) dp[s][e] = max(dp[s][e], dp[s][i] + 1); 45 | res = max(res, dp[s][e]); 46 | } 47 | } 48 | } 49 | fout << res << "\n"; 50 | return 0; 51 | } 52 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/Platinum/262144.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define X real() 18 | #define Y imag() 19 | #define MAXN 262144 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef long double ld; 25 | typedef pair pii; 26 | typedef vector vi; 27 | typedef complex point; 28 | 29 | int n, ending[60][MAXN + 1]; 30 | 31 | int main() { 32 | ifstream fin("262144.in"); 33 | ofstream fout("262144.out"); 34 | fin >> n; 35 | F0R(i, 60) F0R(j, n + 1) ending[i][j] = -1; 36 | F0R(i, n) { 37 | int a; 38 | fin >> a; 39 | ending[a][i] = i; 40 | } 41 | int res = 0; 42 | F0R(i, 60) { 43 | F0R(j, n) { 44 | if(ending[i][j] != -1) { 45 | res = max(res, i); 46 | if(ending[i][ending[i][j] + 1] != -1) { 47 | ending[i + 1][j] = ending[i][ending[i][j] + 1]; 48 | } 49 | } 50 | } 51 | } 52 | fout << res << "\n"; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Diamond Collector | Solved | C++ | 5 | | Bull in a China Shop | Solved | C++ | 6 | | Field Reduction | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Field Reduction | Solved | C++ | 11 | | Diamond Collector | Solved | C++ | 12 | | Closing the Farm | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Splitting the Field | Solved | C++ | 17 | | Closing the Farm | Solved | C++ | 18 | | 284 | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | 262144 | Solved | C++ | 23 | | Bull in a China Shop | Solved | C++ | 24 | | Landscaping | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/Silver/closing.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define X real() 18 | #define Y imag() 19 | #define MAXN 3000 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef long double ld; 25 | typedef pair pii; 26 | typedef vector vi; 27 | typedef complex point; 28 | 29 | int n, m, q[MAXN], numOpen, numCounted; 30 | bool ans[MAXN], on[MAXN], seen[MAXN]; 31 | vi connections[MAXN]; 32 | 33 | void dfs(int node) { 34 | seen[node] = true; 35 | numCounted++; 36 | for(const int conn : connections[node]) if(on[conn] && !seen[conn]) dfs(conn); 37 | } 38 | 39 | bool connected() { 40 | memset(seen, 0, sizeof seen); 41 | numCounted = 0; 42 | dfs(q[n - 1]); 43 | return numCounted == numOpen; 44 | } 45 | 46 | int main() { 47 | ifstream fin("closing.in"); 48 | ofstream fout("closing.out"); 49 | fin >> n >> m; 50 | F0R(i, m) { 51 | int a, b; 52 | fin >> a >> b; 53 | a--, b--; 54 | connections[a].PB(b); 55 | connections[b].PB(a); 56 | } 57 | F0R(i, n) { 58 | fin >> q[i]; 59 | q[i]--; 60 | } 61 | on[q[n - 1]] = 1; 62 | numOpen = 1; 63 | ans[n - 1] = true; 64 | R0F(i, n - 1) { 65 | int barn = q[i]; 66 | on[barn] = true; 67 | numOpen++; 68 | ans[i] = connected(); 69 | } 70 | F0R(i, n) { 71 | if(ans[i]) fout << "YES\n"; 72 | else fout << "NO\n"; 73 | } 74 | return 0; 75 | } 76 | -------------------------------------------------------------------------------- /USACO/2015-2016 Season/US Open/Silver/diamond.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define X real() 18 | #define Y imag() 19 | 20 | using namespace std; 21 | 22 | typedef long long ll; 23 | typedef long double ld; 24 | typedef pair pii; 25 | typedef vector vi; 26 | typedef complex point; 27 | 28 | int n; 29 | ll k; 30 | ll sizes[50000]; 31 | 32 | int main() { 33 | ifstream fin("diamond.in"); 34 | ofstream fout("diamond.out"); 35 | fin >> n >> k; 36 | F0R(i, n) fin >> sizes[i]; 37 | sort(sizes, sizes + n); 38 | int preBest[n], postBest[n]; 39 | preBest[0] = 1; 40 | postBest[n - 1] = 1; 41 | FOR(i, 1, n) { 42 | int j = LB(sizes, sizes + n, sizes[i] - k) - sizes; 43 | preBest[i] = max(preBest[i - 1], i - j + 1); 44 | } 45 | R0F(i, n - 1) { 46 | int j = UB(sizes, sizes + n, sizes[i] + k) - sizes; 47 | postBest[i] = max(postBest[i + 1], j - i); 48 | } 49 | int res = 0; 50 | F0R(i, n - 1) res = max(res, preBest[i] + postBest[i + 1]); 51 | fout << res << "\n"; 52 | return 0; 53 | } 54 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/December/Platinum/team.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i =0 ; i < a; i++) 4 | #define FOR(i, a, b) for(int i = a; i < b; i++) 5 | 6 | typedef long long ll; 7 | 8 | using namespace std; 9 | 10 | const ll MOD = 1000000009; 11 | 12 | int n, m, k; 13 | ll maxTeam[1001][1001][11]; 14 | vector scores1, scores2; 15 | 16 | int main() { 17 | ifstream fin("team.in"); 18 | ofstream fout("team.out"); 19 | 20 | fin >> n >> m >> k; 21 | 22 | F0R(i, n) { 23 | ll x; 24 | fin >> x; 25 | scores1.push_back(x); 26 | } 27 | F0R(i, m) { 28 | ll x; 29 | fin >> x; 30 | scores2.push_back(x); 31 | } 32 | 33 | sort(scores1.begin(), scores1.end()); 34 | sort(scores2.begin(), scores2.end()); 35 | F0R(i, n+1) F0R(j, m+1) 36 | maxTeam[i][j][0] = 1; 37 | 38 | FOR(sizeOfTeam, 1, k+1) FOR(j, 1, m+1) FOR(i, 1, n+1) { 39 | maxTeam[i][j][sizeOfTeam] = ((maxTeam[i-1][j][sizeOfTeam] + maxTeam[i][j-1][sizeOfTeam] - maxTeam[i-1][j-1][sizeOfTeam])%MOD+MOD)%MOD; 40 | if(scores1[i-1] > scores2[j-1]) 41 | maxTeam[i][j][sizeOfTeam]=(maxTeam[i][j][sizeOfTeam]+maxTeam[i-1][j-1][sizeOfTeam-1])%MOD; 42 | } 43 | 44 | fout << maxTeam[n][m][k]; 45 | 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/December/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Square Pasture | Solved | Java | 5 | | Block Game | Solved | Java | 6 | | The Cow-Signal | Solved | Java | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Counting Haybales | Solved | Java | 11 | | Cities and States | Solved | Java | 12 | | Moocast | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Moocast | Solved | Java | 17 | | Cow Checklist | Solved | Java | 18 | | Lasers and Mirrors | Solved | Java | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Lots of Triangles | Solved | C++ | 23 | | Team Building | Solved | C++ | 24 | | Robotic Cow Herd | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/December/Silver/moocast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 200 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, power[MAXN], maxReach, currReach; 27 | pii cows[MAXN]; 28 | bool seen[MAXN]; 29 | 30 | double dis(int cow1, int cow2) { 31 | return sqrt(SQ(cows[cow1].F - cows[cow2].F) + SQ(cows[cow1].S - cows[cow2].S)); 32 | } 33 | 34 | void dfs(int cowID) { 35 | currReach++; 36 | seen[cowID] = true; 37 | F0R(nextCow, n) if(cowID != nextCow && !seen[nextCow] && dis(cowID, nextCow) <= power[cowID]) dfs(nextCow); 38 | } 39 | 40 | int main() { 41 | ifstream fin("moocast.in"); 42 | ofstream fout("moocast.out"); 43 | 44 | fin >> n; 45 | F0R(i, n) { 46 | int x, y, p; 47 | fin >> cows[i].F >> cows[i].S >> power[i]; 48 | } 49 | int maxReach = 0; 50 | F0R(i, n) { 51 | currReach = 0; 52 | memset(seen, 0, sizeof(bool) * MAXN); 53 | dfs(i); 54 | maxReach = max(maxReach, currReach); 55 | } 56 | fout << maxReach << endl; 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Bronze/circlecross.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int first[26], last[26]; 26 | 27 | int main() { 28 | ifstream fin("circlecross.in"); 29 | ofstream fout("circlecross.out"); 30 | FOR(i, 1, 53) { 31 | char c; 32 | fin >> c; 33 | if(first[c - 'A'] == 0) { 34 | first[c - 'A'] = i; 35 | } else { 36 | last[c - 'A'] = i; 37 | } 38 | } 39 | int num = 0; 40 | F0R(i, 26) { 41 | F0R(j, 26) { 42 | if(first[j] > first[i] && first[j] < last[i] && last[j] > last[i]) num++; 43 | } 44 | } 45 | fout << num << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Bronze/cowqueue.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | typedef vector vpii; 22 | 23 | int n; 24 | vpii cows; 25 | 26 | int main() { 27 | ifstream fin("cowqueue.in"); 28 | ofstream fout("cowqueue.out"); 29 | 30 | fin >> n; 31 | F0R(i, n) { 32 | int arrival, interigationTime; 33 | fin >> arrival >> interigationTime; 34 | cows.PB(MP(arrival, interigationTime)); 35 | } 36 | sort(cows.begin(), cows.end()); 37 | ll currTime = 0; 38 | F0R(i, n) { 39 | currTime = max(currTime, (ll)cows[i].F); 40 | currTime += cows[i].S; 41 | } 42 | fout << currTime; 43 | return 0; 44 | } 45 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Bronze/crossroad.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | int N, currSide[11]; 23 | 24 | int main() { 25 | ifstream fin("crossroad.in"); 26 | ofstream fout("crossroad.out"); 27 | 28 | fin >> N; 29 | FOR(i, 1, 11) currSide[i] = -1; 30 | int crossings = 0; 31 | F0R(i, N) { 32 | int id, side; 33 | fin >> id >> side; 34 | if(currSide[id] != -1 && currSide[id] != side) 35 | crossings++; 36 | currSide[id] = side; 37 | } 38 | fout << crossings; 39 | return 0; 40 | } 41 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Gold/circlecross.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | struct BIT { 23 | 24 | int sz; 25 | int *tree; 26 | 27 | BIT(int _sz) { 28 | sz = _sz; 29 | tree = new int[sz + 1]; 30 | F0R(i, sz + 1) tree[i] = 0; 31 | } 32 | 33 | void update(int idx, int v) { 34 | for(idx++; idx <= sz; idx += idx & -idx) tree[idx] += v; 35 | } 36 | 37 | int query(int idx) { 38 | int res = 0; 39 | for(idx++; idx > 0; idx -= idx & -idx) res += tree[idx]; 40 | return res; 41 | } 42 | 43 | int queryRange(int lo, int hi) { return query(hi) - query(lo - 1); } 44 | 45 | }; 46 | 47 | #define MAXN 50000 48 | 49 | int n, firstPos[MAXN]; 50 | 51 | int main() { 52 | ifstream fin("circlecross.in"); 53 | ofstream fout("circlecross.out"); 54 | fin >> n; 55 | BIT bit(2 * n); 56 | F0R(i, n) firstPos[i] = -1; 57 | int res = 0; 58 | F0R(i, 2 * n) { 59 | int a; 60 | fin >> a; 61 | a--; 62 | if(firstPos[a] == -1) { 63 | res += bit.query(i); 64 | firstPos[a] = i; 65 | bit.update(i, 1); 66 | } else { 67 | bit.update(firstPos[a], -1); 68 | res -= bit.query(firstPos[a]); 69 | } 70 | } 71 | fout << res << endl; 72 | } 73 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Gold/nocross.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 1000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int n, dp[MAXN + 1][MAXN + 1], cowAt[MAXN + 1], posRight[MAXN + 1]; 24 | 25 | int main() { 26 | ifstream fin("nocross.in"); 27 | ofstream fout("nocross.out"); 28 | 29 | int maximum = 0; 30 | fin >> n; 31 | FOR(i, 1, n + 1) { 32 | fin >> cowAt[i]; 33 | } 34 | FOR(i, 1, n + 1) { 35 | int j; 36 | fin >> j; 37 | posRight[j] = i; 38 | } 39 | FOR(i, 1, n + 1) { 40 | FOR(j, max(1, cowAt[i] - 4), min(n, cowAt[i] + 4) + 1) { 41 | dp[i][posRight[j]] = 1; 42 | } 43 | } 44 | FOR(j, 1, n + 1) FOR(i, 1, n + 1) dp[i][j] = max(max(dp[i - 1][j], dp[i][j-1]), dp[i][j] + dp[i - 1][j - 1]); 45 | fout << dp[n][n] << endl; 46 | return 0; 47 | } 48 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Gold/visitfj.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define MAXN 100 16 | 17 | using namespace std; 18 | 19 | typedef long long ll; 20 | typedef long double ld; 21 | typedef pair pii; 22 | typedef vector vi; 23 | 24 | int n, t; 25 | ll dp[MAXN][MAXN][3], eatTime[MAXN][MAXN]; 26 | 27 | int main() { 28 | ifstream fin("visitfj.in"); 29 | ofstream fout("visitfj.out"); 30 | 31 | fin >> n >> t; 32 | F0R(y, n) F0R(x, n) fin >> eatTime[x][y]; 33 | F0R(i, n) F0R(j, n) F0R(k, 3) dp[i][j][k] = -1; 34 | priority_queue, vector>, greater>> pq; 35 | pq.push(MT(0,0,0,0)); 36 | while(!pq.empty()) { 37 | tuple state; 38 | int x, y, mod3; 39 | ll v; 40 | while(!pq.empty()) { 41 | state = pq.top(); 42 | pq.pop(); 43 | x = get<1>(state); 44 | y = get<2>(state); 45 | mod3 = get<3>(state); 46 | v = get<0>(state); 47 | if(dp[x][y][mod3] == -1) 48 | break; 49 | } 50 | dp[x][y][mod3] = v; 51 | if(x == n - 1 && y == n - 1) { 52 | fout << v << endl; 53 | return 0; 54 | } 55 | if(x < n - 1) pq.push(MT(v + t + ((mod3 == 2)?eatTime[x + 1][y]:0), x + 1, y, (mod3 + 1) % 3)); 56 | if(y < n - 1) pq.push(MT(v + t + ((mod3 == 2)?eatTime[x][y + 1]:0), x, y + 1, (mod3 + 1) % 3)); 57 | if(x > 0) pq.push(MT(v + t + ((mod3 == 2)?eatTime[x - 1][y]:0), x - 1, y, (mod3 + 1) % 3)); 58 | if(y > 0) pq.push(MT(v + t + ((mod3 == 2)?eatTime[x][y - 1]:0), x, y - 1, (mod3 + 1) % 3)); 59 | } 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Why Did the Cow Cross the Road | Solved | C++ | 5 | | Why Did the Cow Cross the Road II | Solved | C++ | 6 | | Why Did the Cow Cross the Road III | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Why Did the Cow Cross the Road | Solved | C++ | 11 | | Why Did the Cow Cross the Road II | Solved | C++ | 12 | | Why Did the Cow Cross the Road III | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Why Did the Cow Cross the Road | Solved | C++ | 17 | | Why Did the Cow Cross the Road II | Solved | C++ | 18 | | Why Did the Cow Cross the Road III | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Why Did the Cow Cross the Road | Solved | Java | 23 | | Why Did the Cow Cross the Road II | Solved | Java | 24 | | Why Did the Cow Cross the Road III | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Silver/helpcross.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define UB upper_bound 15 | #define MAXN 20000 16 | 17 | using namespace std; 18 | 19 | typedef long long ll; 20 | typedef long double ld; 21 | typedef pair pii; 22 | typedef vector vi; 23 | 24 | int N, C; 25 | pii cows[MAXN], chickens[MAXN]; 26 | bool used[MAXN]; 27 | 28 | int main() { 29 | ifstream fin("helpcross.in"); 30 | ofstream fout("helpcross.out"); 31 | 32 | fin >> C >> N; 33 | F0R(i, C) { 34 | int T; 35 | fin >> T; 36 | chickens[i] = MP(T, i); 37 | } 38 | F0R(i, N) { 39 | int S,E; 40 | fin >> S >> E; 41 | cows[i] = MP(E, S); 42 | } 43 | sort(chickens, chickens + C); 44 | sort(cows, cows + N); 45 | 46 | int res = 0; 47 | F0R(i, N) { 48 | int k = UB(chickens, chickens + C, MP(cows[i].S, -1)) - chickens; 49 | while(k < C && chickens[k].F <= cows[i].F) { 50 | if(!used[chickens[k].S]) { 51 | used[chickens[k].S] = true; 52 | res++; 53 | break; 54 | } 55 | k++; 56 | } 57 | } 58 | fout << res << endl; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/February/Silver/maxcross.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, K, B, needToFix[MAXN]; 24 | bool broken[MAXN]; 25 | 26 | int main() { 27 | ifstream fin("maxcross.in"); 28 | ofstream fout("maxcross.out"); 29 | 30 | fin >> N >> K >> B; 31 | 32 | F0R(i, B) { 33 | int brokenSign; 34 | fin >> brokenSign; 35 | broken[brokenSign - 1] = 1; 36 | } 37 | int minimumFix = B; 38 | F0R(i, N) { 39 | int base = 0; 40 | if(i > 0) base = needToFix[i - 1]; 41 | base += broken[i]; 42 | if(i >= K) base -= broken[i - K]; 43 | needToFix[i] = base; 44 | if(i >= K - 1) minimumFix = min(minimumFix, needToFix[i]); 45 | } 46 | fout << minimumFix; 47 | return 0; 48 | } 49 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Bronze/cowtip.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | int N; 23 | bool tipped[10][10]; 24 | 25 | int main() { 26 | ifstream fin("cowtip.in"); 27 | ofstream fout("cowtip.out"); 28 | 29 | fin >> N; 30 | F0R(y, N) { 31 | string s; 32 | fin >> s; 33 | F0R(x,N) { 34 | tipped[x][y] = s[x] == '1'; 35 | } 36 | } 37 | int count = 0; 38 | while(true) { 39 | int lastX = -1; 40 | int lastY = -1; 41 | F0R(y, N) F0R(x,N) if(tipped[x][y]) { 42 | lastX = x; 43 | lastY = y; 44 | } 45 | if(lastX == -1) 46 | break; 47 | F0R(x, lastX + 1) F0R(y, lastY + 1) tipped[x][y] = !tipped[x][y]; 48 | count++; 49 | } 50 | fout << count; 51 | return 0; 52 | } 53 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Bronze/hps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | int N , diff, win; 23 | 24 | int main() { 25 | ifstream fin("hps.in"); 26 | ofstream fout("hps.out"); 27 | 28 | fin >> N; 29 | F0R(i, N){ 30 | int first, second; 31 | fin >> first >> second; 32 | if(first != second) diff++; 33 | if(second == (first) % 3 + 1) win++; 34 | } 35 | fout << max(win, diff - win); 36 | 37 | return 0; 38 | } 39 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Bronze/notlast.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int n; 26 | string names[7] {"Bessie", "Elsie", "Daisy", "Gertie", "Annabelle", "Maggie", "Henrietta"}; 27 | unordered_map production; 28 | 29 | int main() { 30 | ifstream fin("notlast.in"); 31 | ofstream fout("notlast.out"); 32 | 33 | fin >> n; 34 | F0R(i, 7) production[names[i]] = 0; 35 | F0R(i, n) { 36 | string s; 37 | int p; 38 | fin >> s >> p; 39 | production[s] += p; 40 | } 41 | int minP = production[names[0]]; 42 | F0R(i, 7) minP = min(minP, production[names[i]]); 43 | string second = ""; 44 | F0R(i, 7) { 45 | if(production[names[i]] > minP && (second == "" || production[names[i]] <= production[second])) { 46 | if(second != "" && production[second] == production[names[i]]) { 47 | fout << "Tie\n"; 48 | return 0; 49 | } 50 | second = names[i]; 51 | } 52 | } 53 | if(second == "") fout << "Tie\n"; 54 | else fout << second << endl; 55 | return 0; 56 | } 57 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Gold/bphoto.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, bit[MAXN + 1], inBIT; 24 | 25 | void add(int i) { 26 | i += 1; 27 | for(; i <= N; i += (i &- i)) 28 | bit[i]++; 29 | inBIT++; 30 | } 31 | 32 | int read(int i) { 33 | i += 1; 34 | int answer = 0; 35 | for(; i > 0; i -= (i & -i)) 36 | answer += bit[i]; 37 | return answer; 38 | } 39 | 40 | int main() { 41 | ifstream fin("bphoto.in"); 42 | ofstream fout("bphoto.out"); 43 | fin >> N; 44 | 45 | priority_queue> pq; 46 | F0R(i, N) { 47 | ll height; 48 | fin >> height; 49 | pq.push(MP(height, i)); 50 | } 51 | 52 | int num = 0; 53 | while(!pq.empty()) { 54 | int loc = pq.top().S; 55 | pq.pop(); 56 | int l = read(loc); 57 | int r = inBIT - l; 58 | if(max(r,l) > 2*min(r,l)) 59 | num++; 60 | add(loc); 61 | } 62 | fout << num; 63 | 64 | return 0; 65 | } 66 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Gold/hps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, K, H[MAXN][21], P[MAXN][21], S[MAXN][21]; 24 | vector moves; 25 | 26 | int main() { 27 | ifstream fin("hps.in"); 28 | ofstream fout("hps.out"); 29 | 30 | fin >> N >> K; 31 | F0R(i, N) { 32 | char c; 33 | fin >> c; 34 | moves.PB(c); 35 | } 36 | 37 | F0R(i, N) { 38 | F0R(j, K+1) { 39 | if(i > 0) { 40 | H[i][j] = H[i-1][j]; 41 | P[i][j] = P[i-1][j]; 42 | S[i][j] = S[i-1][j]; 43 | } 44 | if(j > 0) { 45 | H[i][j] = max(H[i][j], H[i][j-1]); 46 | P[i][j] = max(P[i][j], P[i][j-1]); 47 | S[i][j] = max(S[i][j], S[i][j-1]); 48 | } 49 | if(i > 0 && j > 0) { 50 | H[i][j] = max(H[i][j], max(P[i - 1][j - 1], S[i - 1][j - 1])); 51 | P[i][j] = max(P[i][j], max(H[i - 1][j - 1], S[i - 1][j - 1])); 52 | S[i][j] = max(S[i][j], max(P[i - 1][j - 1], H[i - 1][j - 1])); 53 | } 54 | } 55 | F0R(j, K+1) 56 | if(moves[i] == 'H') H[i][j]++; 57 | else if(moves[i] == 'P') P[i][j]++; 58 | else S[i][j]++; 59 | } 60 | fout << max(H[N-1][K], max(S[N-1][K],P[N-1][K])); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Platinum/promote.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, leftEnd[MAXN + 1], rightEnd[MAXN + 1], answer[MAXN + 1], counter = 1; 24 | ll score[MAXN + 1]; 25 | vi children[MAXN + 1]; 26 | 27 | void work(int node) { 28 | leftEnd[node] = counter; 29 | for(int child: children[node]) { 30 | work(child); 31 | counter++; 32 | } 33 | rightEnd[node] = counter; 34 | } 35 | 36 | int main() { 37 | ifstream fin("promote.in"); 38 | ofstream fout("promote.out"); 39 | 40 | fin >> N; 41 | FOR(i, 1, N + 1) fin >> score[i]; 42 | FOR(i, 2, N + 1) { 43 | int manager; 44 | fin >> manager; 45 | children[manager].PB(i); 46 | } 47 | work(1); 48 | int bit[counter + 1]; 49 | F0R(i, counter + 1) { 50 | bit[i] = 0; 51 | } 52 | vector,pair>> cows; 53 | FOR(i, 1, N + 1) { 54 | cows.PB(MP(MP(score[i], i), MP(leftEnd[i], rightEnd[i]))); 55 | } 56 | sort(cows.rbegin(), cows.rend()); 57 | F0R(i, N) { 58 | int sum = 0; 59 | for(int index = cows[i].S.S; index > 0; index -= (index & -index)) 60 | sum+=bit[index]; 61 | for(int index = cows[i].S.F - 1; index > 0; index -= (index & -index)) 62 | sum-=bit[index]; 63 | answer[cows[i].F.S] = sum; 64 | for(int index = cows[i].S.S; index <= counter; index += (index & -index)) 65 | bit[index]++; 66 | } 67 | FOR(i, 1, N + 1) fout << answer[i] << endl; 68 | return 0; 69 | } 70 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Platinum/tallbarn.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N; 24 | ll workNeeded[MAXN], K; 25 | 26 | ld timeNeeded(ld b) { 27 | ld time = 0; 28 | ll cowsUsed = 0; 29 | F0R(i, N) { 30 | ll cowsOnThisFloor = (ll)((1+sqrt(1+(ld)workNeeded[i]*4/b))/2); 31 | time += (ld)workNeeded[i] / cowsOnThisFloor; 32 | cowsUsed += cowsOnThisFloor; 33 | } 34 | time += b * (cowsUsed - K); 35 | return time; 36 | } 37 | 38 | ll countCowsUsed(ld b) { 39 | ll res = 0; 40 | F0R(i, N) { 41 | res += (ll)((1+sqrt(1+(ld)workNeeded[i]*4/b))/2); 42 | } 43 | return res; 44 | } 45 | 46 | int main() { 47 | ifstream fin("tallbarn.in"); 48 | ofstream fout("tallbarn.out"); 49 | 50 | fin >> N >> K; 51 | ld lo = 0, hi = 0; 52 | F0R(i, N) { 53 | fin >> workNeeded[i]; 54 | hi = (ld)max((ll)hi, workNeeded[i]); 55 | } 56 | F0R(counter, 200) { 57 | ld mid = (lo + hi)/2; 58 | if(countCowsUsed(mid) >= K) { 59 | lo = mid; 60 | } else { 61 | hi = mid; 62 | } 63 | } 64 | fout << (ll)round(timeNeeded(lo)); 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Don't Be Last! | Solved | C++ | 5 | | Hoof, Paper, Scissors | Solved | C++ | 6 | | Cow Tipping | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Cow Dance Show | Solved | C++ | 11 | | Hoof, Paper, Scissors | Solved | C++ | 12 | | Secret Cow Code | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Balanced Photo | Solved | C++ | 17 | | Hoof, Paper, Scissors | Solved | C++ | 18 | | Cow Navigation | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Promotion Counting | Solved | C++ | 23 | | Building a Tall Barn | Solved | C++ | 24 | | Subsequence Reversal | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Silver/cowcode.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | string s; 23 | ll N; 24 | 25 | int locOf(ll N) { 26 | if(N < s.length()) 27 | return N; 28 | ll rep = N/s.length() + 1; 29 | ll powerUnder = exp2(ceil(log2(rep)) - 1); 30 | ll place = N - s.length() * (powerUnder); 31 | return locOf((place - 1 + powerUnder*s.length()) % (powerUnder*s.length())); 32 | } 33 | 34 | int main() { 35 | ifstream fin("cowcode.in"); 36 | ofstream fout("cowcode.out"); 37 | 38 | fin >> s >> N; 39 | 40 | fout << s[locOf(N - 1)]; 41 | 42 | return 0; 43 | } 44 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Silver/cowdance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define NMAX 10000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, tmax, duration[NMAX]; 24 | 25 | int main() { 26 | ifstream fin("cowdance.in"); 27 | ofstream fout("cowdance.out"); 28 | 29 | fin >> N >> tmax; 30 | 31 | F0R(i, N) fin >> duration[i]; 32 | 33 | int lo = 1, hi = N; 34 | while(lo < hi) { 35 | int mid = (lo+hi)/2; 36 | priority_queue,greater> pq; 37 | int curr = mid; 38 | int t; 39 | F0R(i, mid) pq.push(duration[i]); 40 | while(!pq.empty()) { 41 | t = pq.top(); 42 | pq.pop(); 43 | if(curr < N) pq.push(t + duration[curr++]); 44 | if(t > tmax) { 45 | lo = mid + 1; 46 | break; 47 | } 48 | } 49 | if(t <= tmax) 50 | hi = mid; 51 | } 52 | fout << lo; 53 | return 0; 54 | } 55 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/January/Silver/hps.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int N, H[MAXN][21], P[MAXN][21], S[MAXN][21]; 24 | vector moves; 25 | 26 | int main() { 27 | ifstream fin("hps.in"); 28 | ofstream fout("hps.out"); 29 | 30 | fin >> N; 31 | F0R(i, N) { 32 | char c; 33 | fin >> c; 34 | moves.PB(c); 35 | } 36 | 37 | F0R(i, N) { 38 | F0R(j, 2) { 39 | if(i > 0) { 40 | H[i][j] = H[i-1][j]; 41 | P[i][j] = P[i-1][j]; 42 | S[i][j] = S[i-1][j]; 43 | } 44 | if(j > 0) { 45 | H[i][j] = max(H[i][j], H[i][j-1]); 46 | P[i][j] = max(P[i][j], P[i][j-1]); 47 | S[i][j] = max(S[i][j], S[i][j-1]); 48 | } 49 | if(i > 0 && j > 0) { 50 | H[i][j] = max(H[i][j], max(P[i - 1][j - 1], S[i - 1][j - 1])); 51 | P[i][j] = max(P[i][j], max(H[i - 1][j - 1], S[i - 1][j - 1])); 52 | S[i][j] = max(S[i][j], max(P[i - 1][j - 1], H[i - 1][j - 1])); 53 | } 54 | } 55 | F0R(j, 2) 56 | if(moves[i] == 'H') H[i][j]++; 57 | else if(moves[i] == 'P') P[i][j]++; 58 | else S[i][j]++; 59 | } 60 | fout << max(H[N-1][1], max(S[N-1][1],P[N-1][1])); 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/Bronze/cownomics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXM 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, m; 27 | int spotted[MAXM], notSpotted[MAXM]; 28 | 29 | int valOf(char c) { 30 | int v = 1; 31 | switch(c) { 32 | case 'T': 33 | v <<= 1; 34 | case 'C': 35 | v <<= 1; 36 | case 'A': 37 | v <<= 1; 38 | } 39 | return v; 40 | } 41 | 42 | int main() { 43 | ifstream fin("cownomics.in"); 44 | ofstream fout("cownomics.out"); 45 | fin >> n >> m; 46 | F0R(i, n) F0R(j, m) { 47 | char c; 48 | fin >> c; 49 | spotted[j] |= valOf(c); 50 | } 51 | F0R(i, n) F0R(j, m) { 52 | char c; 53 | fin >> c; 54 | notSpotted[j] |= valOf(c); 55 | } 56 | int res = 0; 57 | F0R(i, m) if((spotted[i] & notSpotted[i]) == 0) res++; 58 | fout << res << endl; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/Bronze/lostcow.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | 15 | using namespace std; 16 | 17 | typedef long long ll; 18 | typedef long double ld; 19 | typedef pair pii; 20 | typedef vector vi; 21 | 22 | int x, y; 23 | 24 | int main() { 25 | ifstream fin("lostcow.in"); 26 | ofstream fout("lostcow.out"); 27 | 28 | fin >> x >> y; 29 | int mov = 1, goal = y - x; 30 | int total = 0; 31 | while(true) { 32 | if(abs(goal)/goal == abs(mov)/mov && abs(goal) <= abs(mov)) { 33 | total+=abs(goal); 34 | break; 35 | } 36 | total+=abs(2*mov); 37 | mov*=-2; 38 | } 39 | fout << total; 40 | return 0; 41 | } 42 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/Gold/art2.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int n, color[MAXN + 1], last[MAXN + 1]; 24 | bool seen[MAXN + 1]; 25 | 26 | int main() { 27 | ifstream fin("art2.in"); 28 | ofstream fout("art2.out"); 29 | 30 | fin >> n; 31 | FOR(i, 1, n + 1) { 32 | fin >> color[i]; 33 | last[color[i]] = i; 34 | } 35 | bool on[n + 1]; 36 | int numOn = 0; 37 | memset(on, 0, (n + 1) * sizeof(bool)); 38 | on[0] = true; 39 | stack s; 40 | s.push(0); 41 | int maxLayers = 0; 42 | FOR(i, 1, n + 1) { 43 | if(on[color[i]] && s.top() != color[i]) { 44 | fout << -1 << endl; 45 | return 0; 46 | } 47 | if(!on[color[i]]) { 48 | on[color[i]] = true; 49 | s.push(color[i]); 50 | numOn++; 51 | } 52 | maxLayers = max(maxLayers, numOn); 53 | if(color[i] != 0 && i == last[color[i]]) { 54 | on[color[i]] = false; 55 | s.pop(); 56 | numOn--; 57 | } 58 | } 59 | seen[0] = true; 60 | fout << maxLayers << endl; 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | The Lost Cow | Solved | C++ | 5 | | Bovine Genomics | Solved | C++ | 6 | | Modern Art | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Paired Up | Solved | C++ | 11 | | Bovine Genomics | Solved | C++ | 12 | | Where's Bessie? | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Bovine Genomics | Solved | C++ | 17 | | Modern Art 2 | Solved | C++ | 18 | # Platinum 19 | | Problem | State | Implementation | 20 | | ------------- |:---------------:| :--------------:| 21 | | Modern Art | Solved | C++ | 22 | | Switch Grass | Solved | C++ | 23 | | COWBASIC | Solved | Java and C++ | 24 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/Silver/cownomics.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 500 18 | #define MAXM 50 19 | #define MAXV 64 20 | 21 | using namespace std; 22 | 23 | typedef long long ll; 24 | typedef long double ld; 25 | typedef pair pii; 26 | typedef vector vi; 27 | 28 | int n, m; 29 | string genes[2 * MAXN]; 30 | bool has[MAXM][MAXM][MAXM][MAXV], notPossible[MAXM][MAXM][MAXM]; 31 | 32 | int valOf(char c) { 33 | int v = 0; 34 | switch(c) { 35 | case 'T': 36 | v += 1; 37 | case 'C': 38 | v += 1; 39 | case 'A': 40 | v += 1; 41 | } 42 | return v; 43 | } 44 | 45 | int valOf(char a, char b, char c) { 46 | return 16 * valOf(a) + 4 * valOf(b) + valOf(c); 47 | } 48 | 49 | int main() { 50 | ifstream fin("cownomics.in"); 51 | ofstream fout("cownomics.out"); 52 | fin >> n >> m; 53 | F0R(i, 2 * n) fin >> genes[i]; 54 | F0R(i, n) F0R(a, m) FOR(b, a + 1, m) FOR(c, b + 1, m) has[a][b][c][valOf(genes[i][a], genes[i][b], genes[i][c])] = true; 55 | FOR(i, n, 2 * n) F0R(a, m) FOR(b, a + 1, m) FOR(c, b + 1, m) if(has[a][b][c][valOf(genes[i][a], genes[i][b], genes[i][c])]) notPossible[a][b][c] = true; 56 | int res = 0; 57 | F0R(a, m) FOR(b, a + 1, m) FOR(c, b + 1, m) if(!notPossible[a][b][c]) res++; 58 | fout << res << endl; 59 | return 0; 60 | } 61 | -------------------------------------------------------------------------------- /USACO/2016-2017 Season/US Open/Silver/pairup.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (a) - 1; i >= (b); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MAXN 100000 15 | 16 | using namespace std; 17 | 18 | typedef long long ll; 19 | typedef long double ld; 20 | typedef pair pii; 21 | typedef vector vi; 22 | 23 | int n; 24 | pair cows[MAXN]; 25 | 26 | int main() { 27 | ifstream fin("pairup.in"); 28 | ofstream fout("pairup.out"); 29 | 30 | fin >> n; 31 | F0R(i, n) { 32 | ll time, num; 33 | fin >> num >> time; 34 | cows[i] = MP(time, num); 35 | } 36 | sort(cows, cows + n); 37 | int i = 0, j = n - 1; 38 | ll time = 0; 39 | while(i <= j) { 40 | ll r = min(cows[i].S, cows[j].S); 41 | cows[i].S -= r; 42 | cows[j].S -= r; 43 | time = max(time, cows[i].F + cows[j].F); 44 | if(cows[i].S <= 0) i++; 45 | if(cows[j].S <= 0) j--; 46 | } 47 | fout << time << endl; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Bronze/billboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int x[6], y[6]; 26 | 27 | int main() { 28 | ifstream fin("billboard.in"); 29 | ofstream fout("billboard.out"); 30 | F0R(i, 6) fin >> x[i] >> y[i]; 31 | int area = 0; 32 | F0R(i, 2) { 33 | area += (x[2 * i + 1] - x[2 * i]) * (y[2 * i + 1] - y[2 * i]); 34 | int x1 = max(x[2 * i], x[4]); 35 | int x2 = min(x[2 * i + 1], x[5]); 36 | int y1 = max(y[2 * i], y[4]); 37 | int y2 = min(y[2 * i + 1], y[5]); 38 | area -= max(0, x2 - x1) * max(0, y2 - y1); 39 | } 40 | fout << area; 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Bronze/shuffle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, a[MAXN], start[MAXN]; 27 | 28 | int main() { 29 | ifstream fin("shuffle.in"); 30 | ofstream fout("shuffle.out"); 31 | fin >> n; 32 | F0R(i, n) { 33 | int b; 34 | fin >> b; 35 | b--; 36 | a[b] = i; 37 | } 38 | F0R(i, n) { 39 | int x; 40 | fin >> x; 41 | start[a[a[a[i]]]] = x; 42 | } 43 | F0R(i, n) fout << start[i] << "\n"; 44 | return 0; 45 | } 46 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Gold/barnpainting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | #define MOD 1000000007 19 | 20 | using namespace std; 21 | 22 | typedef long long ll; 23 | typedef long double ld; 24 | typedef pair pii; 25 | typedef vector vi; 26 | 27 | int n, k, color[MAXN]; 28 | vi connections[MAXN]; 29 | ll dp[MAXN][3]; 30 | 31 | void dfs(int node, int parent) { 32 | vector numWays[3]; 33 | for(const int next : connections[node]) { 34 | if(next == parent) continue; 35 | dfs(next, node); 36 | numWays[0].PB((dp[next][1] + dp[next][2]) % MOD); 37 | numWays[1].PB((dp[next][0] + dp[next][2]) % MOD); 38 | numWays[2].PB((dp[next][0] + dp[next][1]) % MOD); 39 | } 40 | F0R(i, 3) { 41 | if(color[node] != 0 && color[node] != i + 1) continue; 42 | dp[node][i] = 1; 43 | for(const ll l : numWays[i]) dp[node][i] = (dp[node][i] * l) % MOD; 44 | } 45 | } 46 | 47 | int main() { 48 | ifstream fin("barnpainting.in"); 49 | ofstream fout("barnpainting.out"); 50 | fin >> n >> k; 51 | F0R(i, n - 1) { 52 | int a, b; 53 | fin >> a >> b; 54 | a--, b--; 55 | connections[a].PB(b); 56 | connections[b].PB(a); 57 | } 58 | F0R(i, k) { 59 | int a; 60 | fin >> a; 61 | a--; 62 | fin >> color[a]; 63 | } 64 | dfs(0, -1); 65 | fout << (dp[0][0] + dp[0][1] + dp[0][2]) % MOD << "\n"; 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Gold/piepie.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, minShares[2 * MAXN]; 27 | ll d, worth[2 * MAXN][2]; 28 | set> gifts[2]; 29 | 30 | int main() { 31 | ifstream fin("piepie.in"); 32 | ofstream fout("piepie.out"); 33 | fin >> n >> d; 34 | F0R(i, 2 * n) minShares[i] = -1; 35 | queue q; 36 | F0R(i, 2 * n) { 37 | fin >> worth[i][0] >> worth[i][1]; 38 | if(worth[i][i < n] != 0) gifts[i < n].insert(MP(worth[i][i < n], i)); 39 | else { 40 | minShares[i] = 1; 41 | q.push(i); 42 | } 43 | } 44 | while(!q.empty()) { 45 | int i = q.front(); 46 | q.pop(); 47 | vector> toErase; 48 | auto hi = gifts[i >= n].LB(MP(worth[i][i >= n] + 1, -1)); 49 | for(auto it = gifts[i >= n].LB(MP(worth[i][i >= n] - d, -1)); it != hi; ++it) { 50 | minShares[(*it).S] = minShares[i] + 1; 51 | q.push((*it).S); 52 | toErase.PB(*it); 53 | } 54 | for(const pair p : toErase) gifts[i >= n].erase(p); 55 | } 56 | F0R(i, n) fout << minShares[i] << "\n"; 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Blocked Billboard | Solved | C++ | 5 | | The Bovine Shuffle | Solved | C++ | 6 | | Milk Measurement | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | My Cow Ate My Homework | Solved | C++ | 11 | | Milk Measurement | Solved | C++ | 12 | | The Bovine Shuffle | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | A Pie for a Pie | Solved | C++ | 17 | | Barn Painting | Solved | C++ | 18 | | Haybale Feast | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Standing Out from the Herd | Solved | C++ (Hashing) and C++ (Suffix Array) | 23 | | Push a Box | Solved | C++ | 24 | | Greedy Gift Takers | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Silver/homework.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n; 27 | ll score[MAXN]; 28 | 29 | int main() { 30 | ifstream fin("homework.in"); 31 | ofstream fout("homework.out"); 32 | fin >> n; 33 | F0R(i, n) fin >> score[i]; 34 | ll minScore = score[n - 1]; 35 | ld total = score[n - 1], maxAvg = 0; 36 | vi res; 37 | ROF(i, 1, n - 1) { 38 | total += score[i]; 39 | minScore = min(minScore, score[i]); 40 | ld avg = (ld)(total - minScore) / (n - 1 - i); 41 | if(avg > maxAvg) { 42 | res.clear(); 43 | maxAvg = avg; 44 | } 45 | if(avg == maxAvg) res.PB(i); 46 | } 47 | reverse(res.begin(), res.end()); 48 | for(const int i : res) fout << i << "\n"; 49 | return 0; 50 | } 51 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Silver/measurement.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | 18 | using namespace std; 19 | 20 | typedef long long ll; 21 | typedef long double ld; 22 | typedef pair pii; 23 | typedef vector vi; 24 | 25 | int n; 26 | ll g; 27 | set> cows; 28 | unordered_map outputs; 29 | 30 | int main() { 31 | ifstream fin("measurement.in"); 32 | ofstream fout("measurement.out"); 33 | fin >> n >> g; 34 | vector> v; 35 | F0R(i, n) { 36 | int day; 37 | ll outputChange, id; 38 | fin >> day >> id >> outputChange; 39 | if(!outputs.count(id)) { 40 | outputs.insert(MP(id, g)); 41 | cows.insert(MP(g, id)); 42 | } 43 | v.PB(MT(day, id, outputChange)); 44 | } 45 | sort(v.begin(), v.end()); 46 | int res = 0; 47 | cows.insert(MP(g, 0)); 48 | for(tuple t : v) { 49 | ll currHighest = (*(--cows.end())).F; 50 | ll cowID = get<1>(t); 51 | ll outputChange = get<2>(t); 52 | bool isCurrHighest = (outputs[cowID] == currHighest); 53 | bool soloTop = (((*(----cows.end())).F) != currHighest); 54 | cows.erase(MP(outputs[cowID], cowID)); 55 | outputs[cowID] += outputChange; 56 | cows.insert(MP(outputs[cowID], cowID)); 57 | ll newHighest = (*(--cows.end())).F; 58 | bool currSoloTop = (((*(----cows.end())).F) != newHighest); 59 | if(isCurrHighest && (!soloTop || outputs[cowID] != newHighest || !currSoloTop) || !isCurrHighest && outputs[cowID] == newHighest) res++; 60 | } 61 | fout << res << "\n"; 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/December/Silver/shuffle.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define F first 11 | #define S second 12 | #define PB push_back 13 | #define MP make_pair 14 | #define MT make_tuple 15 | #define UB upper_bound 16 | #define LB lower_bound 17 | #define MAXN 100000 18 | 19 | using namespace std; 20 | 21 | typedef long long ll; 22 | typedef long double ld; 23 | typedef pair pii; 24 | typedef vector vi; 25 | 26 | int n, a[MAXN], numParents[MAXN]; 27 | 28 | int main() { 29 | ifstream fin("shuffle.in"); 30 | ofstream fout("shuffle.out"); 31 | fin >> n; 32 | F0R(i, n) { 33 | fin >> a[i]; 34 | a[i]--; 35 | numParents[a[i]]++; 36 | } 37 | int res = n; 38 | queue q; 39 | F0R(i, n) if(numParents[i] == 0) q.push(i); 40 | while(!q.empty()) { 41 | int i = q.front(); 42 | q.pop(); 43 | numParents[a[i]]--; 44 | if(numParents[a[i]] == 0) q.push(a[i]); 45 | res--; 46 | } 47 | fout << res << "\n"; 48 | return 0; 49 | } 50 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Bronze/hoofball.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 100 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef complex point; 34 | 35 | int n, x[MAXN], side[MAXN]; 36 | 37 | int main() { 38 | ifstream fin("hoofball.in"); 39 | ofstream fout("hoofball.out"); 40 | fin >> n; 41 | if(n == 1) 42 | fout << 1 << endl; 43 | else { 44 | F0R(i, n) fin >> x[i]; 45 | sort(x, x + n); 46 | F0R(i, n) { 47 | if(i == 0) 48 | side[i] = 1; 49 | else if(i == n - 1) 50 | side[i] = -1; 51 | else 52 | side[i] = (x[i + 1] - x[i] < x[i] - x[i - 1])? 1 : -1; 53 | } 54 | int res = 0; 55 | F0R(i, n) { 56 | if((i == 0 || side[i - 1] == -1) && (i == n - 1 || side[i + 1] == 1)) res++; 57 | else if((i == 0 || side[i - 1] == -1) && i < n - 1 && side[i + 1] == -1 && (i == n - 2 || side[i + 2] == 1) && side[i] == 1) res++; 58 | } 59 | fout << res << endl; 60 | } 61 | return 0; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Bronze/taming.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 100 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef complex point; 34 | 35 | int n, a[MAXN], minArr[MAXN]; 36 | bool isBreakout[MAXN], notBreakout[MAXN]; 37 | 38 | bool possible() { 39 | int lastBreakout = 0; 40 | isBreakout[0] = 1; 41 | F0R(i, n) if(a[i] != -1) { 42 | if(a[i] > i - lastBreakout || notBreakout[i - a[i]]) 43 | return 0; 44 | else { 45 | lastBreakout = i - a[i]; 46 | isBreakout[lastBreakout] = 1; 47 | FOR(k, lastBreakout + 1, i + 1) notBreakout[k] = 1; 48 | } 49 | } 50 | return 1; 51 | } 52 | 53 | int main() { 54 | ifstream fin("taming.in"); 55 | ofstream fout("taming.out"); 56 | fin >> n; 57 | F0R(i, n) fin >> a[i]; 58 | if(!possible()) 59 | fout << -1 << endl; 60 | else { 61 | int mn = 0, mx = 0; 62 | F0R(i, n) if(isBreakout[i]) mn++; 63 | F0R(i, n) if(!notBreakout[i]) mx++; 64 | fout << mn << " " << mx << endl; 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Bronze/teleport.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | 26 | using namespace std; 27 | 28 | typedef long long ll; 29 | typedef long double ld; 30 | typedef pair pii; 31 | typedef vector vi; 32 | typedef complex point; 33 | 34 | int a, b, x, y; 35 | 36 | int main() { 37 | ifstream fin("teleport.in"); 38 | ofstream fout("teleport.out"); 39 | fin >> a >> b >> x >> y; 40 | fout << min(abs(a - b), min(abs(a - x) + abs(b - y), abs(b - x) + abs(a - y))); 41 | return 0; 42 | } 43 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Teleportation | Solved | C++ | 5 | | Hoofball | Solved | C++ | 6 | | Taming the Herd | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Rest Stops | Solved | C++ | 11 | | Snow Boots | Solved | C++ | 12 | | Teleportation | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Snow Boots | Solved | C++ | 17 | | Directory Traversal | Solved | C++ | 18 | | Taming the Herd | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Slingshot | Solved | C++ | 23 | | New Barns | Solved | C++ | 24 | | Cow Gymnasts | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Silver/reststops.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 100000 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef complex point; 34 | 35 | ll L, N, rF, rB, x[MAXN], c[MAXN]; 36 | int maxTaste[MAXN]; 37 | 38 | int main() { 39 | ifstream fin("reststops.in"); 40 | ofstream fout("reststops.out"); 41 | fin >> L >> N >> rF >> rB; 42 | F0R(i, N) fin >> x[i] >> c[i]; 43 | R0F(i, N) { 44 | maxTaste[i] = i; 45 | if(i < N - 1 && c[i] < c[maxTaste[i + 1]]) maxTaste[i] = maxTaste[i + 1]; 46 | } 47 | ll pos = 0; 48 | ll res = 0; 49 | while(pos < x[N - 1]) { 50 | int next = UB(x, x + N, pos) - x; 51 | next = maxTaste[next]; 52 | ll dis = x[maxTaste[next]]; 53 | res += c[next] * (dis - pos) * (rF - rB); 54 | pos = dis; 55 | } 56 | fout << res << endl; 57 | return 0; 58 | } 59 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Silver/snowboots.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 250 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef complex point; 34 | 35 | int n, b; 36 | ll depth[MAXN]; 37 | pair boots[MAXN]; 38 | bool possible[MAXN]; 39 | 40 | int main() { 41 | ifstream fin("snowboots.in"); 42 | ofstream fout("snowboots.out"); 43 | fin >> n >> b; 44 | F0R(i, n) fin >> depth[i]; 45 | F0R(i, b) { 46 | ll s; 47 | int d; 48 | fin >> s >> d; 49 | boots[i] = MP(s, d); 50 | } 51 | possible[0] = 1; 52 | F0R(i, b) { 53 | F0R(j, n) 54 | if(possible[j] && depth[j] <= boots[i].F) 55 | F0R(k, boots[i].S + 1) if(j + k < n && depth[j + k] <= boots[i].F) 56 | possible[j + k] = 1; 57 | if(possible[n - 1]) { 58 | fout << i << endl; 59 | break; 60 | } 61 | } 62 | return 0; 63 | } 64 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/February/Silver/teleport.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 100000 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef complex point; 34 | 35 | int n, a[MAXN], b[MAXN]; 36 | vector> toProcess; 37 | ll curr; 38 | 39 | int main() { 40 | ifstream fin("teleport.in"); 41 | ofstream fout("teleport.out"); 42 | fin >> n; 43 | F0R(i, n) { 44 | ll a, b; 45 | fin >> a >> b; 46 | curr += abs(a - b); 47 | if(abs(a) < abs(a - b)) { 48 | toProcess.PB(MP(b - abs(a - b) + abs(a), -1)); 49 | toProcess.PB(MP(b, 2)); 50 | toProcess.PB(MP(b + abs(a - b) - abs(a), -1)); 51 | } 52 | } 53 | sort(toProcess.begin(), toProcess.end()); 54 | ll res = curr; 55 | ll currInc = 0, lastSpot = INT_MIN; 56 | for(const pair p : toProcess) { 57 | curr += currInc * (p.F - lastSpot); 58 | currInc += p.S; 59 | lastSpot = p.F; 60 | res = min(res, curr); 61 | } 62 | fout << res << endl; 63 | return 0; 64 | } 65 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/January/Bronze/billboard.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define R real() 26 | #define I image() 27 | #define PI acos(-1) 28 | #define MAXN 100 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef unsigned long long ull; 34 | typedef long double ld; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef complex point; 38 | typedef complex cld; 39 | 40 | int n; 41 | pii shifts[MAXN]; 42 | 43 | int solve(int idx) { 44 | bool covered[1000]; 45 | memset(covered, 0, 1000 * sizeof(bool)); 46 | F0R(i, n) if(i != idx) FOR(j, shifts[i].F, shifts[i].S) covered[j] = 1; 47 | int res = 0; 48 | F0R(i, 1000) res += covered[i]; 49 | return res; 50 | } 51 | 52 | int main() { 53 | ifstream fin("lifeguards.in"); 54 | ofstream fout("lifeguards.out"); 55 | fin >> n; 56 | F0R(i, n) fin >> shifts[i].F >> shifts[i].S; 57 | int res = 0; 58 | F0R(i, n) res = max(res, solve(i)); 59 | fout << res << endl; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/January/Bronze/lifeguards.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define R real() 26 | #define I image() 27 | #define PI acos(-1) 28 | #define MAXN 100 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef unsigned long long ull; 34 | typedef long double ld; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef complex point; 38 | typedef complex cld; 39 | 40 | int n; 41 | pii shifts[MAXN]; 42 | 43 | int solve(int idx) { 44 | bool covered[1000]; 45 | memset(covered, 0, 1000 * sizeof(bool)); 46 | F0R(i, n) if(i != idx) FOR(j, shifts[i].F, shifts[i].S) covered[j] = 1; 47 | int res = 0; 48 | F0R(i, 1000) res += covered[i]; 49 | return res; 50 | } 51 | 52 | int main() { 53 | ifstream fin("lifeguards.in"); 54 | ofstream fout("lifeguards.out"); 55 | fin >> n; 56 | F0R(i, n) fin >> shifts[i].F >> shifts[i].S; 57 | int res = 0; 58 | F0R(i, n) res = max(res, solve(i)); 59 | fout << res << endl; 60 | return 0; 61 | } 62 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/January/Gold/spainting.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define R real() 26 | #define I image() 27 | #define PI acos(-1) 28 | #define MAXN 1000000 29 | #define MOD 1000000007 30 | 31 | using namespace std; 32 | 33 | typedef long long ll; 34 | typedef unsigned long long ull; 35 | typedef long double ld; 36 | typedef pair pii; 37 | typedef vector vi; 38 | typedef complex point; 39 | typedef complex cld; 40 | 41 | int n, m, k; 42 | ll dp[MAXN]; 43 | 44 | int main() { 45 | ifstream fin("spainting.in"); 46 | ofstream fout("spainting.out"); 47 | fin >> n >> m >> k; 48 | dp[0] = m; 49 | ll res = m; 50 | FOR(i, 1, n) { 51 | dp[i] = m * dp[i - 1] % MOD; 52 | if(i == k - 1) dp[i] += MOD - m; 53 | if(i > k - 1) dp[i] += MOD - (m - 1) * dp[i - k] % MOD; 54 | dp[i] %= MOD; 55 | res = (res * m) % MOD; 56 | } 57 | fout << (res + MOD - dp[n - 1]) % MOD << "\n"; 58 | return 0; 59 | } 60 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/January/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Blocked Billboard II | Solved | C++ | 5 | | Lifeguards | Solved | C++ | 6 | | Out of Place | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Lifeguards | Solved | C++ | 11 | | Rental Service | Solved | C++ | 12 | | MooTube | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | MooTube | Solved | C++ | 17 | | Cow At Large | Solved | C++ | 18 | | Stamp Painting | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Lifeguards | Solved | C++ | 23 | | Cow at Large | Solved | C++ | 24 | | Sprinklers | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/January/Silver/mootube.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define R real() 26 | #define I image() 27 | #define PI acos(-1) 28 | #define MAXN 5000 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef unsigned long long ull; 34 | typedef long double ld; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef complex point; 38 | typedef complex cld; 39 | 40 | int n, q; 41 | vector connections[MAXN]; 42 | 43 | int solve(int node, int k, int par) { 44 | int res = 1; 45 | for(const pii next : connections[node]) if(next.F != par && next.S >= k) res += solve(next.F, k, node); 46 | return res; 47 | } 48 | 49 | int main() { 50 | ifstream fin("mootube.in"); 51 | ofstream fout("mootube.out"); 52 | fin >> n >> q; 53 | F0R(i, n - 1) { 54 | int u, v, w; 55 | fin >> u >> v >> w; 56 | u--, v--; 57 | connections[u].PB(MP(v, w)); 58 | connections[v].PB(MP(u, w)); 59 | } 60 | F0R(i, q) { 61 | int k, v; 62 | fin >> k >> v; 63 | v--; 64 | fout << solve(v, k, -1) - 1 << "\n"; 65 | } 66 | return 0; 67 | } 68 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/US Open/Gold/sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | #define MAXN 100000 26 | 27 | using namespace std; 28 | 29 | typedef long long ll; 30 | typedef long double ld; 31 | typedef unsigned long long ull; 32 | typedef pair pii; 33 | typedef vector vi; 34 | typedef vector vl; 35 | typedef vector vul; 36 | typedef complex point; 37 | 38 | struct BIT{ 39 | 40 | int SZ; 41 | int *tree; 42 | 43 | BIT(int sz) { 44 | SZ = sz; 45 | tree = new int[sz + 1]; 46 | memset(tree, 0, (sz + 1) * sizeof(int)); 47 | } 48 | 49 | void update(int idx) { for(idx++; idx <= SZ; idx += idx & -idx) tree[idx]++; } 50 | 51 | int query(int idx) { 52 | int res = 0; 53 | for(idx++; idx > 0; idx -= idx & -idx) res += tree[idx]; 54 | return res; 55 | } 56 | 57 | }; 58 | 59 | int n, res; 60 | pii a[MAXN]; 61 | 62 | int main() { 63 | ifstream fin("sort.in"); 64 | ofstream fout("sort.out"); 65 | fin >> n; 66 | F0R(i, n) { 67 | fin >> a[i].F; 68 | a[i].S = i; 69 | } 70 | sort(a, a + n); 71 | BIT x(n); 72 | R0F(i, n - 1) { 73 | x.update(a[i + 1].S); 74 | res = max(res, x.query(i)); 75 | } 76 | fout << max(1, res) << endl; 77 | } 78 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/US Open/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Team Tic Tac Toe | Solved | C++ | 5 | | Milking Order | Solved | C++ | 6 | | Family Tree | Solved | C++ | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Out of Sorts | Solved | C++ | 11 | | Lemonade Line | Solved | C++ | 12 | | Multiplayer Moo | Solved | C++ | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Out of Sorts | Solved | C++ | 17 | | Milking Order | Solved | C++ | 18 | | Talent Show | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Out of Sorts | Solved | C++ | 23 | | Train Tracking | Solved | C++ | 24 | | Disruption | Solved | C++ (HLD) and C++ (set) | 25 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/US Open/Silver/lemonade.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | 26 | using namespace std; 27 | 28 | typedef long long ll; 29 | typedef long double ld; 30 | typedef unsigned long long ull; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef vector vl; 34 | typedef vector vul; 35 | typedef complex point; 36 | 37 | int n; 38 | vi v; 39 | 40 | int main() { 41 | ifstream fin("lemonade.in"); 42 | ofstream fout("lemonade.out"); 43 | fin >> n; 44 | F0R(i, n) { 45 | int w; 46 | fin >> w; 47 | v.PB(w); 48 | } 49 | sort(v.rbegin(), v.rend()); 50 | int res = n; 51 | F0R(i, n) if(v[i] < i) { 52 | res = i; 53 | break; 54 | } 55 | fout << res << endl; 56 | } 57 | -------------------------------------------------------------------------------- /USACO/2017-2018 Season/US Open/Silver/sort.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define SQ(a) (a)*(a) 4 | 5 | #define F0R(i, a) for(int i = 0; i < (a); i++) 6 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 7 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 8 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 9 | 10 | #define ran() (rand() & 0x7FFF) 11 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 12 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 13 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 14 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 15 | 16 | #define F first 17 | #define S second 18 | #define PB push_back 19 | #define MP make_pair 20 | #define MT make_tuple 21 | #define UB upper_bound 22 | #define LB lower_bound 23 | #define X real() 24 | #define Y imag() 25 | 26 | using namespace std; 27 | 28 | typedef long long ll; 29 | typedef long double ld; 30 | typedef unsigned long long ull; 31 | typedef pair pii; 32 | typedef vector vi; 33 | typedef vector vl; 34 | typedef vector vul; 35 | typedef complex point; 36 | 37 | int n; 38 | vector v; 39 | 40 | int main() { 41 | ifstream fin("sort.in"); 42 | ofstream fout("sort.out"); 43 | fin >> n; 44 | F0R(i, n) { 45 | int a; 46 | fin >> a; 47 | v.PB(MP(a, i)); 48 | } 49 | sort(v.begin(), v.end()); 50 | int res = 0; 51 | F0R(i, n) res = max(res, v[i].S - i); 52 | fout << 1 + res << endl; 53 | } 54 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/December/Gold/teamwork.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for(int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for(int i = (a); i < (b); i++) 5 | #define R0F(i, a) for(int i = (a) - 1; i >= 0; i--) 6 | #define ROF(i, a, b) for(int i = (b) - 1; i >= (a); i--) 7 | 8 | #define ran() (rand() & 0x7FFF) 9 | #define rand31() ((ran() << 16) | (ran() << 1) | (ran() & 1)) 10 | #define rand32() ((ran() << 17) | (ran() << 2) | (ran() & 3)) 11 | #define rand63() (((ll)ran() << 48) | ((ll)ran() << 33) | ((ll)ran() << 18) | ((ll)ran() << 3) | ((ll)ran() & 7)) 12 | #define rand64() (((ll)ran() << 49) | ((ll)ran() << 34) | ((ll)ran() << 19) | ((ll)ran() << 4) | ((ll)ran() & 15)) 13 | 14 | #define F first 15 | #define S second 16 | #define PB push_back 17 | #define MP make_pair 18 | #define MT make_tuple 19 | #define UB upper_bound 20 | #define LB lower_bound 21 | #define X real() 22 | #define Y imag() 23 | 24 | #define PI acos(-1) 25 | 26 | #define sz(x) ((int)(x).size()) 27 | #define all(x) (x).begin(), (x).end() 28 | #define SQ(x) ((x) * (x)) 29 | 30 | using namespace std; 31 | 32 | typedef long long ll; 33 | typedef long double ld; 34 | typedef unsigned long long ull; 35 | typedef pair pii; 36 | typedef vector vi; 37 | typedef vector vpii; 38 | typedef vector vll; 39 | typedef vector vul; 40 | typedef complex point; 41 | typedef complex cld; 42 | typedef vector vcld; 43 | 44 | #define MAXN 10000 45 | 46 | int n, k; 47 | ll arr[MAXN], dp[MAXN + 1]; 48 | 49 | int main() { 50 | ifstream fin("teamwork.in"); 51 | ofstream fout("teamwork.out"); 52 | fin >> n >> k; 53 | F0R(i, n) fin >> arr[i]; 54 | F0R(i, n) { 55 | ll rm = 0; 56 | FOR(j, 1, k + 1) if(i + j <= n) { 57 | rm = max(rm, arr[i + j - 1]); 58 | dp[i + j] = max(dp[i + j], dp[i] + j * rm); 59 | } 60 | } 61 | fout << dp[n] << endl; 62 | } 63 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/December/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Mixing Milk | Solved | Missing | 5 | | The Bucket List | Solved | Missing | 6 | | Back and Forth | Solved | Missing | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Convention | Solved | Missing | 11 | | Convention II | Solved | Missing | 12 | | Mooyo Mooyo | Solved | Missing | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Fine Dining | Solved | C++ | 17 | | Cowpatibility | Solved | C++ | 18 | | Teamwork | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Balance Beam | Solved | C++ | 23 | | Sort It Out | Solved | C++ | 24 | | The Cow Gathering | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/February/Gold/dishes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for (int i = 0; i < a; i++) 4 | 5 | #define PB push_back 6 | 7 | using namespace std; 8 | 9 | typedef vector vi; 10 | 11 | #define MAXN 100000 12 | 13 | int n, mx, base[MAXN + 1]; 14 | vi cntr[MAXN + 1]; 15 | 16 | int main() { 17 | ifstream cin("dishes.in"); 18 | ofstream cout("dishes.out"); 19 | cin >> n; 20 | int mx = 0; 21 | F0R(i, n) { 22 | int a; 23 | cin >> a; 24 | if(a < mx) { 25 | cout << i << endl; 26 | exit(0); 27 | } 28 | for(int j = a; j && !base[j]; j--) base[j] = a; 29 | while(!cntr[base[a]].empty() && cntr[base[a]].back() < a) { 30 | mx = max(mx, cntr[base[a]].back()); 31 | cntr[base[a]].pop_back(); 32 | } 33 | cntr[base[a]].PB(a); 34 | } 35 | cout << n << endl; 36 | return 0; 37 | } 38 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/February/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Sleepy Cow Herding | Unsolved | Missing | 5 | | The Great Revegetation | Unsolved | Missing | 6 | | Measuring Traffic | Unsolved | Missing | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Sleepy Cow Herding | Unsolved | Missing | 11 | | Painting the Barn | Unsolved | Missing | 12 | | The Great Revegetation | Unsolved | Missing | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Cow Land | Solved | C++ | 17 | | Dishwashing | Solved | C++ | 18 | | Painting the Barn | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Cow Dating | Solved | C++ | 23 | | Moorio Kart | Solved | C++ | 24 | | Mowing Mischief | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/January/README.md: -------------------------------------------------------------------------------- 1 | # Bronze 2 | | Problem | State | Implementation | 3 | | ------------- |:---------------:| :--------------:| 4 | | Shell Game | Unsolved | Missing | 5 | | Sleepy Cow Sorting | Unsolved | Missing | 6 | | Guess the Animal | Unsolved | Missing | 7 | # Silver 8 | | Problem | State | Implementation | 9 | | ------------- |:---------------:| :--------------:| 10 | | Grass Planting | Unsolved | Missing | 11 | | Icy Perimeter | Unsolved | Missing | 12 | | Mountain View | Unsolved | Missing | 13 | # Gold 14 | | Problem | State | Implementation | 15 | | ------------- |:---------------:| :--------------:| 16 | | Cow Poetry | Solved | C++ | 17 | | Sleep Cow Sorting | Solved | C++ | 18 | | Chortcut | Solved | C++ | 19 | # Platinum 20 | | Problem | State | Implementation | 21 | | ------------- |:---------------:| :--------------:| 22 | | Redistricting | Solved | C++ | 23 | | Exercise Route | Solved | C++ | 24 | | Train Tracking 2 | Solved | C++ | 25 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/US Open/Gold/balance.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAXN 100000 6 | 7 | int n, board[2][MAXN], cnt[2][2]; 8 | vector loc[2][2]; 9 | long long numInv[2]; 10 | 11 | long long cntInversions(int* arr) { 12 | long long num1s = 0, res = 0; 13 | for (int i = 0; i < n; i++) { 14 | num1s += arr[i] == 1; 15 | res += (arr[i] == 0) * num1s; 16 | } 17 | return res; 18 | } 19 | 20 | int main() { 21 | ifstream cin("balance.in"); 22 | ofstream cout("balance.out"); 23 | cin >> n; 24 | for (int j = 0; j < 2; j++) { 25 | for (int i = 0; i < n; i++) { 26 | cin >> board[j][i]; 27 | loc[j][board[j][i]].push_back(i); 28 | cnt[j][board[j][i]]++; 29 | } 30 | numInv[j] = cntInversions(board[j]); 31 | } 32 | long long res = abs(numInv[0] - numInv[1]); 33 | long long running = 0; 34 | for (int i = 0; i < min((int)loc[0][0].size(), (int)loc[1][1].size()); i++) { 35 | int lPos = loc[0][0][loc[0][0].size() - 1 - i]; 36 | int rPos = loc[1][1][i]; 37 | numInv[0] += n - 1 - lPos; 38 | numInv[1] += rPos; 39 | numInv[0] -= cnt[0][1]; 40 | numInv[1] -= cnt[1][0]; 41 | cnt[0][1]++; 42 | cnt[1][0]++; 43 | running += n + rPos - lPos; 44 | res = min(res, running + abs(numInv[0] - numInv[1])); 45 | } 46 | for (int j = 0; j < 2; j++) { 47 | cnt[j][0] = cnt[j][1] = 0; 48 | for(int i = 0; i < n; i++) cnt[j][board[j][i]]++; 49 | numInv[j] = cntInversions(board[j]); 50 | } 51 | running = 0; 52 | for (int i = 0; i < min((int)loc[0][1].size(), (int)loc[1][0].size()); i++) { 53 | int lPos = loc[0][1][loc[0][1].size() - 1 - i]; 54 | int rPos = loc[1][0][i]; 55 | numInv[0] -= n - 1 - lPos; 56 | numInv[1] -= rPos; 57 | numInv[0] += cnt[0][1]; 58 | numInv[1] += cnt[1][0]; 59 | cnt[0][1]--; 60 | cnt[1][0]--; 61 | running += n + rPos - lPos; 62 | res = min(res, running + abs(numInv[0] - numInv[1])); 63 | } 64 | cout << res << endl; 65 | return 0; 66 | } 67 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/US Open/Gold/snakes.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define F0R(i, a) for (int i = 0; i < (a); i++) 4 | #define FOR(i, a, b) for (int i = (a); i < (b); i++) 5 | 6 | #define INF 1e18 7 | 8 | using namespace std; 9 | 10 | typedef long long ll; 11 | 12 | #define MAXN 400 13 | 14 | int n, k; 15 | ll a[MAXN], dp[MAXN + 1][MAXN + 1]; 16 | 17 | int main() { 18 | ifstream cin("snakes.in"); 19 | ofstream cout("snakes.out"); 20 | cin >> n >> k; 21 | F0R(i, n) cin >> a[i]; 22 | F0R(i, n + 1) F0R(j, k + 2) dp[i][j] = INF; 23 | dp[0][0] = 0; 24 | F0R(i, n) F0R(j, k + 1) if(dp[i][j] != INF) { 25 | ll runSum = 0, runMax = 0; 26 | FOR(toAdd, 1, n + 1 - i) { 27 | runSum += a[i + toAdd - 1]; 28 | runMax = max(runMax, a[i + toAdd - 1]); 29 | dp[i + toAdd][j + 1] = min(dp[i + toAdd][j + 1], dp[i][j] + toAdd * runMax - runSum); 30 | } 31 | } 32 | cout << dp[n][k + 1] << endl; 33 | } 34 | -------------------------------------------------------------------------------- /USACO/2018-2019 Season/US Open/Gold/walk.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MOD 2019201997LL 6 | 7 | #define MAXN 7500 8 | 9 | int n, k; 10 | long long mst[MAXN]; 11 | 12 | long long cost(int x, int y) { return MOD - 84 * min(x, y) - 48 * max(x, y); } 13 | 14 | void constMST() { 15 | bool vis[n]; 16 | memset(vis, 0, sizeof(vis)); 17 | for (int i = 1; i < n; i++) mst[i] = 1e15; 18 | mst[0] = 0; 19 | while (true) { 20 | int closest = -1; 21 | for (int i = 0; i < n; i++) if(!vis[i] && (closest == -1 || mst[i] < mst[closest])) closest = i; 22 | if (closest == -1) break; 23 | vis[closest] = 1; 24 | for (int next = 0; next < n; next++) if (!vis[next]) mst[next] = min(mst[next], cost(closest + 1, next + 1)); 25 | } 26 | } 27 | 28 | int main() { 29 | ifstream cin("walk.in"); 30 | ofstream cout("walk.out"); 31 | cin >> n >> k; 32 | constMST(); 33 | sort(mst + 1, mst + n); 34 | cout << mst[n + 1 - k] << endl; 35 | } 36 | -------------------------------------------------------------------------------- /USACO/2019-2020 Season/December/Platinum/pieaters.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | struct cow { 6 | int l, r; 7 | long long w; 8 | 9 | bool operator<(const cow& other) const { 10 | return make_tuple(l, r, w) > make_tuple(other.l, other.r, other.w); 11 | } 12 | 13 | }; 14 | 15 | int n, m; 16 | vector cows; 17 | long long dp[300][300]; 18 | 19 | long long getDP(int l, int r) { 20 | if (r < l) { return 0; } 21 | return dp[l][r]; 22 | } 23 | 24 | int main() { 25 | ifstream cin("pieaters.in"); 26 | cin >> n >> m; 27 | for (int i = 0; i < m; i++) { 28 | long long w; 29 | int l, r; 30 | cin >> w >> l >> r; 31 | l--, r--; 32 | cows.push_back((cow){ l, r, w }); 33 | } 34 | for (int rightEnd = 0; rightEnd < n; rightEnd++) { 35 | for (int leftEnd = rightEnd; leftEnd >= 0; leftEnd--) { 36 | for (const cow& c : cows) { 37 | if (c.l >= leftEnd && c.r <= rightEnd) { 38 | for (int i = c.l; i <= c.r; i++) { 39 | dp[leftEnd][rightEnd] = max(dp[leftEnd][rightEnd], getDP(leftEnd, i - 1) + getDP(i + 1, rightEnd) + c.w); 40 | } 41 | } 42 | } 43 | cout << leftEnd << " " << rightEnd << " " << dp[leftEnd][rightEnd] << endl; 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /USACO/2019-2020 Season/January/Platinum/cave.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | #define MAXN 1000 6 | #define MOD 1000000007 7 | 8 | int par[MAXN * MAXN], sz[MAXN * MAXN]; 9 | long long cnt[MAXN * MAXN]; 10 | 11 | bool grid[MAXN][MAXN]; 12 | 13 | int n, m; 14 | 15 | int root(int x) { 16 | if (par[x] == x) { return x; } 17 | return par[x] = root(par[x]); 18 | } 19 | 20 | void join(int i, int j) { 21 | int r1 = root(i), r2 = root(j); 22 | if (r1 == r2) { return; } 23 | if (sz[r1] < sz[r2]) { swap(r1, r2); } 24 | par[r2] = r1; 25 | cnt[r1] = (cnt[r1] * cnt[r2]) % MOD; 26 | sz[r1] += (sz[r1] == sz[r2]); 27 | } 28 | 29 | void join(int r1, int c1, int r2, int c2) { 30 | join(r1 * m + c1, r2 * m + c2); 31 | } 32 | 33 | int main() { 34 | ifstream fin("cave.in"); 35 | ofstream fout("cave.out"); 36 | fin >> n >> m; 37 | for (int i = n - 1; i >= 0; i--) { 38 | for (int j = 0; j < m; j++) { 39 | char c; 40 | fin >> c; 41 | grid[i][j] = (c == '.'); 42 | cnt[i * m + j] = 1; 43 | par[i * m + j] = i * m + j; 44 | sz[i * m + j] = 1; 45 | } 46 | } 47 | for (int i = 0; i < n; i++) { 48 | for (int j = 0; j < m; j++) { 49 | if (i > 0 && grid[i][j] && grid[i - 1][j]) { 50 | join(i, j, i - 1, j); 51 | } 52 | if (j > 0 && grid[i][j] && grid[i][j - 1]) { 53 | join(i, j, i, j - 1); 54 | } 55 | } 56 | set roots; 57 | for (int j = 0; j < m; j++) { if (grid[i][j]) { roots.insert(root(i * m + j)); } } 58 | for (const int j : roots) { cnt[j] = (cnt[j] + 1) % MOD; } 59 | } 60 | long long res = 1; 61 | for (int i = 0; i < n * m; i++) { if (par[i] == i) { res = (res * cnt[i]) % MOD; } } 62 | fout << res << endl; 63 | } 64 | -------------------------------------------------------------------------------- /USACO/2019-2020 Season/January/Platinum/nondec.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define MAXN 50000 4 | 5 | using namespace std; 6 | 7 | typedef long long ll; 8 | 9 | #define MOD 1000000007 10 | #define NEGHALF 500000003 11 | 12 | int n, k, q, a[MAXN]; 13 | array invPref[MAXN], pref[MAXN]; 14 | array, 20> invRunning, running; 15 | 16 | void madd(ll& a, ll b) { a = (a + b) % MOD; } 17 | 18 | int main() { 19 | ifstream fin("nondec.in"); 20 | ofstream fout("nondec.out"); 21 | fin >> n >> k; 22 | for (int i = 0; i < n; i++) { 23 | fin >> a[i]; 24 | a[i]--; 25 | } 26 | for (int i = 0; i < k; i++) { 27 | invRunning[i][i] = running[i][i] = 1; 28 | } 29 | for (int i = 0; i < n; i++) { 30 | for (int r = 0; r < k; r++) { 31 | ll toAdd = 0; 32 | for (int c = 0; c <= a[i]; c++) { 33 | madd(toAdd, running[r][c]); 34 | } 35 | madd(running[r][a[i]], toAdd); 36 | } 37 | for (int r = 0; r <= a[i]; r++) { 38 | for (int c = 0; c < k; c++) { 39 | madd(invRunning[r][c], (NEGHALF * invRunning[a[i]][c]) % MOD); 40 | } 41 | } 42 | for (int j = 0; j < k; j++) { invPref[i][j] = invRunning[0][j]; } 43 | for (int r = 0; r < k; r++) { 44 | pref[i][r] = 0; 45 | for (int c = 0; c < k; c++) { 46 | madd(pref[i][r], running[r][c]); 47 | } 48 | } 49 | } 50 | fin >> q; 51 | for (int i = 0; i < q; i++) { 52 | int l, r; 53 | fin >> l >> r; 54 | l--, r--; 55 | if (l == 0) { 56 | fout << pref[r][0] << "\n"; 57 | } else { 58 | ll res = 0; 59 | for (int j = 0; j < k; j++) { 60 | madd(res, invPref[l - 1][j] * pref[r][j] % MOD); 61 | } 62 | fout << res << "\n"; 63 | } 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /USACO/2019-2020 Season/US Open/Platinum/exercise.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | using namespace std; 4 | 5 | typedef unsigned long long ull; 6 | typedef long long ll; 7 | 8 | #define MAXN 7500 9 | 10 | int n; 11 | ull m; 12 | ull fact[MAXN + 1]; 13 | bool comp[MAXN + 1]; 14 | 15 | ull madd(ull a, ull b) { return (a + b) % m; } 16 | 17 | ull msub(ull a, ull b) { return (a + m - b) % m; } 18 | 19 | ull mmult(ull a, ull b) { return a * b % m; } 20 | 21 | ull modpow(ull a, ull p) { 22 | return p ? ((p & 1) ? mmult(a, modpow(a, p ^ 1)) : modpow(mmult(a, a), p >> 1)) : 1; 23 | } 24 | 25 | ull without(int z) { 26 | ull res = 1; 27 | for (int i = 1; i <= n; i++) { 28 | if (i % z) { res = res * i % (m - 1); } else{ res = res * (i - 1) % (m - 1); } 29 | } 30 | return res; 31 | } 32 | 33 | ull with(int z) { 34 | return (fact[n] + m - 1 - without(z)) % (m - 1); 35 | } 36 | 37 | int main() { 38 | ifstream fin("exercise.in"); 39 | ofstream fout("exercise.out"); 40 | fin >> n >> m; 41 | fact[0] = 1; 42 | for (int i = 1; i <= n; i++) { fact[i] = fact[i - 1] * i % (m - 1); } 43 | ull res = 1; 44 | for (int i = 2; i <= n; i++) { 45 | if (!comp[i]) { 46 | for (int j = 2 * i; j <= n; j += i) { 47 | comp[j] = true; 48 | } 49 | for (int x = i; x <= n; x *= i) { 50 | res = mmult(res, modpow(i, with(x))); 51 | } 52 | } 53 | } 54 | fout << res << endl; 55 | } 56 | -------------------------------------------------------------------------------- /USACO/README.md: -------------------------------------------------------------------------------- 1 | # Performance on Past Competitions 2 | ## 2019 - 2020 3 | ### US Open 2020 4 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 350 5 | ### February 2020 6 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 833 7 | ### January 2020 8 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 750 9 | ### December 2019 10 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 762 11 | ## 2018 - 2019 12 | ### US Open 2019 13 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 1000 14 | ### February 2019 15 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 640 16 | ### January 2019 17 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 692 18 | ### December 2018 19 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 487 20 | ## 2017 - 2018 21 | ### US Open 2018 22 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 486 23 | ### February 2018 24 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 428 25 | ### January 2018 26 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 697 27 | ### December 2017 28 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 611 29 | 30 | ## 2016 - 2017 31 | ### US Open 2017 32 | ![Platinum](http://www.usaco.org/current/images/medal_platinum.png) **Platinum** 700 33 | ### February 2017 34 | ![Gold](http://www.usaco.org/current/images/medal_gold.png) **Gold** 733 35 | ### January 2017 36 | ![Gold](http://www.usaco.org/current/images/medal_gold.png) **Gold** 567 37 | 38 | ### December 2016 39 | ![Gold](http://www.usaco.org/current/images/medal_gold.png) **Gold** 585 40 | 41 | ![Silver](http://www.usaco.org/current/images/medal_silver.png) **Silver** 1000 42 | 43 | ![Bronze](http://www.usaco.org/current/images/medal_bronze.png) **Bronze** 1000 44 | --------------------------------------------------------------------------------