20110914 Four levels of programmers Implementation know the language well translate idea to programs Algorithms Design good solutions Software engineering manage different components and people ID: 443844
Download Presentation The PPT/PDF document "How to win in ACM/ICPC?" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
How to win in ACM/ICPC?
2011-09-14Slide2
Four levels of programmers
Implementation
know the language well, translate idea to programs
Algorithms
Design good solutions
Software engineering
manage different components and people
World
How can I change the world?Slide3
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. :)Slide4
What kind of implementation skills are needed?
Variables, loops, functions
Exhaustion
Try all permutationsTry all subsetsTry all paths, etc
Classes, operator overloading, STLPersistence (don't be afraid of long problems and programs)Slide5
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] );
}
}Slide6
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 ->
baacstring s;
cin >> s;next_permutation
( s.begin(), s.end() );cout
<< s;
Time complexity?
O(n)
If called n! times, O(n!) timeSlide7
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
>() );Slide8
How to improve implementation?
Write more programs
Read more books
Don't afraid of new toolsSTLDebugger
Exercises. 1000, 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1010, 1012, 1013, 1016, 1019Slide9
Algorithmic skills (1/4)?
Standard algorithm design principles
Divide-and-conquer
Dynamic programmingSave up the partial results
The optimal solution can be found by backtrackingGreedy
Sometimes the seemingly best choice is the only best choiceThese are principles that we don't need to memorizeSlide10
Example. POJ1015
Given m jury members and to select n, each with p
i
, di. 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 = dSlide11
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.Slide12
Example. POJ1018
We 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.Slide13
Algorithmic skills (2/4)?
Running time analysisSlide14
Algorithmic skills (3/4)?
Observation and creativity
Find some properties about the problemSlide15
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;}Slide16Slide17
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.Slide18Slide19
Break
Standard algorithm design principles
Running time analysis
Observation and creativity
Find some properties about the problem
How to improve creativity?Be imaginative.Try to indentify properties of the problem.Slide20
Algorithmic skills (4/4)?
Common algorithms
Algorithms that have been studied before.
Memorize the algorithms
Sorting, graphs, network flow, coordinate geometry, mathBe hardworking, read more booksSlide21
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.Slide22
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.Slide23
Immediate questions
Does a Delaunay triangulation always exist?
How is it related to finding spanning tree?
How to find the Delaunay triangulation efficiently?Slide24
Delaunay triangulation and minimum spanning tree
Theorem. The minimum spanning tree containing only edges in the Delaunay Triangulation.Slide25
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.Slide26
Slow algorithm. Edge flip
Theorem. We can continue to remove illegal triangle and the resulting triangulation is Delaunay.Slide27
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 pFind the triangle containing p
Only new triangle may violate Delaunay propertySlide28
The legalizeTriangle
Legalize( p, pi,
pj
,
pk ){
if( pipj is ilegal ) flip
pipj and
ppk Legalize( p, pipk, pi' ); Legalize( p,
pjpk, pj
' );}Slide29
Find triangle containing pSlide30
Schedule for this year
Mon
Tue
Wed
Thur
Fri
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)Slide31
Summary
Implementation
Persistence
AlgorithmsRunning time analysisDesign techniques
Observation and creativityCommon algorithmsPrepare for internal contest and regional
Algorithms != software