How to win in ACM/ICPC? 2011-09-14. Four levels of programmers 1. Implementation ◦ know the...

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

Transcript of How to win in ACM/ICPC? 2011-09-14. Four levels of programmers 1. Implementation ◦ know the...

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◦ Design good solutions

3. Software engineering◦ manage different components and

people

4. World◦ How can I change the

world?

How to win in ACM/ICPC?Be excellent in implementation and

algorithms

Important warning!ACM cannot test your ability beyond

algorithmsTo 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. :)

What kind of implementation skills are needed?Variables, loops, functionsExhaustion

◦Try all permutations◦Try all subsets◦Try all paths, etc

Classes, operator overloading, STL

Persistence (don't be afraid of long problems and programs)

Example. POJ 1564Given 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 selectedvoid 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] ); }}

Example. POJ 1146Given 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

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<int>() );

How to improve implementation?Write more programsRead more booksDon't afraid of new tools

◦ STL◦ Debugger

Exercises. 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1010, 1012, 1013, 1016, 1019

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

Example. POJ1015Given m jury members and to select

n, each with pi, di. Find a subset that minimizes |∑pi - ∑di|.

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

Example. POJ1024Given 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.

Example. POJ1018We need to build a channel with n

parts.Part i has ci choices (bi1, pi1), (bi2,pi2)Final bandwidth = min bandwidth of

a part. Final cost = total costFind maximum B/P

For each possible B, the minimum cost is formed by picking the choice with b >= B and minimum price.

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

Algorithmic skills (3/4)?

3. Observation and creativity◦ Find some properties about the

problem

Example. POJ1021Given 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;}

Example. POJ1009Given 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.

Break1. Standard algorithm design principles2. Running time analysis3. Observation and creativity

◦ Find some properties about the problem

How to improve creativity?◦ Be imaginative.◦ Try to indentify properties of the problem.

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

Example. POJ3391Given 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.

Delaunay TriangulationDefinition. 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.

Immediate questionsDoes a Delaunay triangulation always exist?How is it related to finding spanning tree?How to find the Delaunay triangulation

efficiently?

Delaunay triangulation and minimum spanning treeTheorem. The minimum spanning tree

containing only edges in the Delaunay Triangulation.

Other applicationsTheorem. 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.

Slow algorithm. Edge flip

Theorem. We can continue to remove illegal triangle and the resulting triangulation is Delaunay.

Fast Algorithmp0 = 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

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

Find triangle containing p

Schedule for this yearMon Tue Wed Thu

rFri

5/9

12/9 HL

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)

SummaryImplementation

◦PersistenceAlgorithms

◦Running time analysis◦Design techniques◦Observation and creativity◦Common algorithms

Prepare for internal contest and regional

Algorithms != software