last meeting of the year. last week's potw solution: #include... map dp; int getdp(int a) {...

14
Last Meeting Of The Year

Upload: myrtle-stevens

Post on 30-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Last Meeting Of The Year

Page 2: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Last Week's POTWSolution:#include <map>...map<int, int> DP;int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c > 0; c /= 10) digsum += c % 10; int M = digsum; for(int d1 = 2; d1 * d1 <= a; ++d1) if(a % d1 == 0) M = max(M, digsum + getdp(d1) + getdp(a / d1)); return DP[a] = M;}int main() { int n; cin >> n; cout << getdp(n) << "\n"; return 0;}

Page 3: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Problem

• I have N circles and K demands.

• I want to color the N circles.o Demand i states that circle A_i and B_i must be colored the same

color.

• I want to use as many colors as possible cause I'm fabulous

• How many different colors can I use?

• Solutiono Union Find!o Circles that must be the same color are in the same set.o For each demand, merge A_i and B_i.o At the end, count how many sets there are.

Page 4: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

The Union-Find Algorithm

• Maintain a collection of disjoint sets.o Each element belongs to a set.o Each set has a "representative" element.

• What you can do with Union-Findo FIND(X)

Find which set element X belongs to. Returns representative of X's set.

o UNION(X, Y) Merge the two sets containing elements X and Y. If X and Y are already in the same set, nothing happens.

o O(inverse-ackermann(n)) amortized runtime In other words, very fast!! (almost constant time)

Page 5: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

How It Works

• Each set is stored as a tree.o The root is the representative element of its set.

• Every element has a parent.o The parent of a root is itself.o Initially, each element is in its own set.

• FIND(X) returns the root of X.if (parent[X] == X) return X;else return FIND(parent[X]);

• UNION(A,B) merges A and B's trees.o Put A's root under B's root.

parent[FIND(A)] = FIND(B);

• http://research.cs.vt.edu/AVresearch/UF/

Page 6: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Example Solution

int[] par;int FIND(int x) {

if (par[x] == x) {return x;

}return FIND(par[x]);

}void UNION(int x, int y) {

x = FIND(x);y = FIND(y);par[x] = y; // if x == y, this does nothing!

}

Page 7: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Example Solution Continued

for(int i = 0; i < N; ++i)par[i] = i; // initially, all circles are disjoint.

for(int i = 0; i < K; ++i) {int a, b;...UNION(a, b); // a and b must be in the same set.

}int cnt = 0; // number of sets is just number of roots.for(int i = 0; i < N; ++i) {

if (par[i] == i) { // check if i is a root.cnt++;

}}return cnt;

Page 8: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

How to make it fast

• Union Find needs optimizations to become fast.

• Optimization 1 - Path Compressiono Once you find an element's root, set that root as its parent.

• Optimization 2 - Union By Ranko The rank of a set is the height of its tree.o When merging sets A and B, put B under A if B is shorter than A.o This keeps the trees shorter

• These two optimizations combined make the overall average runtime nearly constant.

Page 9: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

UNION-FIND With Optimization

int[] par, rank;int FIND(int x) {

if (par[x] == x) return x;return par[x] = FIND(par[x]);

}void UNION(int a, int b) {

a = FIND(a); b = FIND(b);if (a == b)

return;if (rank[a] > rank[b])

swap(a, b);par[a] = b;rank[b] = max(rank[b], rank[a] + 1);

}

Page 10: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Applications

• Union-Find is used in Kruskal's algorithm for Minimum-Spanning Trees.

• Union-Find can be used in place of DFS when searching for connected components or cycles.

• Union-Find can be used in many other problems.o Very common in USACO Silver and Gold contests.o Also on Codeforces

• Path-Compression can be extended to other trees sometimes.

Page 11: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

POTW

• There is a river, represented by a grid with width Wo All squares (x,y) with 0 <= y < W are part of the river.

• In the i-th minute, Andy throws a rock into the river, which fills a unit square with coordinate (x_i, y_i). [0 <= y_i < W]

• Andy wants to block off flow from west to east. How many rocks will he throw before this goal is first achieved?o Water cannot flow over stones or between stoneso It can, however, flow east to west if necessary

Page 12: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Beautiful POTW Diagram

Page 13: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Input Format and Constraints

• Input Formato Line 1: Two integers, W, T

The width of the river The number of minutes Andy spends throwing rocks.

o Lines 2...T+1: Two integers x_i, y_i.

• Output Formato Line 1: The number of rocks Andy throws before blocking off the flow,

or -1 if it cannot be done.

• Constraintso 15 points: W, T < 10. |x_i| < 10o 35 points: W, T < 1000. |x_i| < 1000o 50 points: W, T < 100000, |x_i| < 10^9

Page 14: Last Meeting Of The Year. Last Week's POTW Solution: #include... map DP; int getdp(int a) { if(DP[a]) return DP[a]; int digsum = 0; for(int c = a; c >

Sample CaseInput:3 51 21 13 12 0-1 1

Output:4

width