Presentation is loading. Please wait.

Presentation is loading. Please wait.

How to win in ACM/ICPC? 2011-09-14. Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦

Similar presentations


Presentation on theme: "How to win in ACM/ICPC? 2011-09-14. Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦"— Presentation transcript:

1 How to win in ACM/ICPC? 2011-09-14

2 Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦ Design good solutions 3. Software engineering ◦ manage different components and people 4. World ◦ How can I change the world?

3 How to win in ACM/ICPC? Be excellent in implementation and algorithms Important warning! ACM cannot test your ability beyond algorithms To change the world requires many more things. See the bigger picture and keep learning. Implementation and algorithms are necessary, but they are not the end. :)

4 What kind of implementation skills are needed? Variables, loops, functions Exhaustion ◦ Try all permutations ◦ Try all subsets ◦ Try all paths, etc Classes, operator overloading, STL Persistence (don't be afraid of long problems and programs)

5 Example. POJ 1564 Given a target T and an integer array A[1..n] (with possible repetition), find all subsets of A that sum to T //A[0..n] is descending //B[0..m] is selected void gen( int A[], int n, int B, int m, int T, int skip ){ if( target==0 ){ //output B, return } if( n==0 ) return; if( A[0]>target || skip==A[0] ){ gen( A+1, n-1, B, m, T, skip ); }else{ //try to pick A[0] B[m] = input[0]; m++; gen( A+1, n-1, B, m, T-A[0], skip ); //try not to pick A[0] m--; gen( A+1, n-1, B, m, T, input[0] ); }

6 Example. POJ 1146 Given a string s. Rearrange the characters to s' so that s' is just lexicographically larger than s. ◦ E.g., abc -> acb, acba -> baac string s; cin >> s; next_permutation( s.begin(), s.end() ); cout << s; Time complexity? O(n) If called n! times, O(n!) time

7 Examples. How to read a line of integers (where the number of integers are unknown)? How to sort integers in descending order? string line; getline( cin, line ); stringstream ss( line ); int i; while( ss >> i ) cout << i << endl; int a[100]; sort( a, a+100, greater () );

8 How to improve implementation? Write more programs Read more books Don't afraid of new tools ◦ STL ◦ Debugger Exercises. 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1010, 1012, 1013, 1016, 1019

9 Algorithmic skills (1/4)? 1. Standard algorithm design principles ◦ Divide-and-conquer ◦ Dynamic programming  Save up the partial results  The optimal solution can be found by backtracking ◦ Greedy  Sometimes the seemingly best choice is the only best choice These are principles that we don't need to memorize

10 Example. POJ1015 Given m jury members and to select n, each with p i, d i. Find a subset that minimizes |∑p i - ∑d i |. What can we achieve if we select r from the first k jury members? possible[0..200][0..20][-400..400]: where possible[k][r][d] = 1 if we can select r from first k jury members and achieve diff = d

11 Example. POJ1024 Given a 2D map, decide whether there is a unique path from s to t. Calculate the shortest path to each position by DP. Then do backtracking to see if the path is unique.

12 Example. POJ1018 We need to build a channel with n parts. Part i has c i choices (b i1, p i1 ), (b i2,p i2 ) Final bandwidth = min bandwidth of a part. Final cost = total cost Find maximum B/P For each possible B, the minimum cost is formed by picking the choice with b >= B and minimum price.

13 Algorithmic skills (2/4)? 2. Running time analysis

14 Algorithmic skills (3/4)? 3. Observation and creativity ◦ Find some properties about the problem

15 Example. POJ1021 Given two 2D objects. Determine if they have the same shape under rotation or reflection. Start from the top left position, do a flooding and record the path. Compare if the two paths are the same. string fill( int x, int y, string path ){ if( visited[x][y] ){ path += 'E'; return s; } visited[x][y] = true; path += '.'; s = fill( x-1, y, s ); //repeat 4 times return s; }

16

17 Example. POJ1009 Given a run length encoded 2D board. Transform each cell to max{ diff with 8 direction }. Output the run length encoded 2D board. Label the cell linearly. Observation. If all of i's neighbors are not start or end of a new segment. Then the value of i equals the value of i-1.

18

19 Break 1. Standard algorithm design principles 2. Running time analysis 3. Observation and creativity ◦ Find some properties about the problem How to improve creativity? ◦ Be imaginative. ◦ Try to indentify properties of the problem.

20 Algorithmic skills (4/4)? 4. Common algorithms ◦ Algorithms that have been studied before. ◦ Memorize the algorithms ◦ Sorting, graphs, network flow, coordinate geometry, math Be hardworking, read more books

21 Example. POJ3391 Given n points on a 2D coordinate plane. Find the minimum spanning tree. I try to teach you an average O( n log n ) time algorithm.

22 Delaunay Triangulation Definition. Given a set of points in 2D. A triangulation is a division of the convex hull so that all regions are triangles. Definition. A Delaunay triangulation is a triangulation so that each circumcircle contains no points in the in interior.

23 Immediate questions Does a Delaunay triangulation always exist? How is it related to finding spanning tree? How to find the Delaunay triangulation efficiently?

24 Delaunay triangulation and minimum spanning tree Theorem. The minimum spanning tree containing only edges in the Delaunay Triangulation.

25 Other applications Theorem. The closest neighbor of each point p is its Delaunay neighbor. Theorem. The point within the convex hull that is furthest from any other point is the circumcenter of a Delaunay triangle.

26 Slow algorithm. Edge flip Theorem. We can continue to remove illegal triangle and the resulting triangulation is Delaunay.

27 Fast Algorithm p 0 = the top-right site, p -1 = (∞, -∞), p -2 = (-∞, ∞) be the initial triangle. Random shuffle the remaining sites. For each remaining point p ◦ Find the triangle containing p ◦ Only new triangle may violate Delaunay property

28 The legalizeTriangle Legalize( p, pi, pj, pk ){ if( pipj is ilegal ) flip pipj and ppk Legalize( p, pipk, pi' ); Legalize( p, pjpk, pj' ); }

29 Find triangle containing p

30 Schedule for this year MonTueWedThurFri 5/9 12/9HL 19/9 26/9 Individual contest 3/10 10/10 Team contest 17/10 Team contest Possible regionals: Kuala Lumpur (Nov 12 - 13) or Hsinchu (Nov26)

31 Summary Implementation ◦ Persistence Algorithms ◦ Running time analysis ◦ Design techniques ◦ Observation and creativity ◦ Common algorithms Prepare for internal contest and regional Algorithms != software


Download ppt "How to win in ACM/ICPC? 2011-09-14. Four levels of programmers 1. Implementation ◦ know the language well, translate idea to programs 2. Algorithms ◦"

Similar presentations


Ads by Google