View
218
Download
0
Category
Tags:
Preview:
Citation preview
By David/Denon
First of All…
Welcome guys
Powered by Google Translator
Agenda
• Contest Format• Taste the challenges• Input /Output• STL introduction• Admin
THE CONTESTACM ICPC
The Contest
ACM International Collegiate Programming Contest (abbreviated as ACM-ICPC or just ICPC) is an annual multi-tiered computer programming competition among the universities of the world. The contest is sponsored by IBM. Headquartered at Baylor University, with autonomous regions on six continents, the ICPC is directed by Baylor Professor William B. Poucher, Executive Director, and operates under the auspices of the Association for Computing Machinery (ACM).(From wikipedia)
The Contest● World Finals is held every year.● To earn the free ticket to this glory battlefield, we need to
excel in the Regional competitions.– To be held in late October and November.
The Contest• Team of three• One Computer per
team• 8 – 13 problems• X hours
• Solve the most problem in the shortest time wins the competition
The Contest
while ( ! isTimeout() ) {read();think();program();//debug();submit();
}
One Balloon as a gift for solving a problem.Each problem is associated with a color.
HOW?Solve Problem
Let’s try
10080 - Gopher II563 - Crimewave
This is a interesting problem but solving it requires some knowledge (i.e. max flow) that you may or may not have acquired at the moment. However, in order for you to see what is a typical ICPC problem looks like, just pretend that you know how to solve the problem on the next slide.
Let’s Try
So Si
4 1
2 3 2
3
4
So = SourceSi = Sink
Let’s Try
So Si
0/4 0/1
1/2 1/3 1/2
0/3
0/4
So = SourceSi = Sink
Is this a possible setting?Does it violate any constraints?
Let’s Try
So Si
0/4 0/1
1/2 1/3 1/2
0/3
0/4
So = SourceSi = Sink
What is the total flow from the Source to the Sink?
Let’s Try
So Si
3/4 3/1
4/2 0/3 1/2
0/3
0/4
So = SourceSi = Sink
How about this one?Is it a valid configuration? (2)
Let’s Try
So Si
1/4 1/1
2/2 1/3 1/2
0/3
0/4
So = SourceSi = Sink
This is another valid configuration.
Max-Flow Problem
Max-Flow Problem• When given a graph, what is the max. valid
flow on it?
Max-Flow Problem
So Si
1/4 1/1
1/2 0/3 2/2
2/3
0/4
So = SourceSi = Sink
Max. Flow = 3
Max-Flow Problem
Now just assume that your teammate know how to program the function() to solve the max-flow problem.
Input: (single source, single sink, non-negative constraints)
So Si
4 1
2 3 2
3
4
Max-Flow Problem
Input: (single source, single sink, non-negative constraints)
S Si
4 1
2 3 2
3
4
Output: (Max total flow, flow on each edge)
S Si
1/4 1/1
1/2 0/3 2/2
2/3
0/4
Max-Flow Problem
• What if there are multiple sources or/and sinks? How can we use the function() to help solving the problem? (Remember function() can only solve problems of single sources and single sources)
So Si
41
2 3 2
3
4
So Si
4
4 3
Max-Flow Problem
• Transformation
S S
4 1
2 3 2
4
S S
4
4 3
Max-Flow Problem
• Transformation
S Si
4 1
2 3 2
4
S Si
4
4 3
S’Si’
Max-Flow Problem
• Transformation
SS SiSi
4 1
2 3 2
4
SS SiSi
4
4 3
S’Si’
Max-Flow Problem
• Transformation
SS SiSi
4 1
2 3 2
4
SS SiSi
4
4 3
S’Si’
infiinfi
infi infi
Can you solve the following problem?10080 - Gopher II
Hints: Use the function() Hints: do a transformation
10080 - Gopher II
Let’s have 5 mins break. (It is not actually a break. It is expected that you read the problem during this given time)
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=12&page=show_problem&problem=1021
10080 - Gopher II Hints: Use the function()
Hints: do a transformation
HoleHole
HoleHole
HoleHole
5s Away
Velocity of Gopher: 1 m/s
3m(3s)
8m(8s)8m(8s)
3m(3s)
8m(8s)
3m(3s)
3m(3s)
3m(3s)
3m(3s)
10080 - Gopher II Hints: Use the function()
Hints: do a transformation
HoleHole
HoleHole
HoleHole
5s Away
Velocity of Gopher: 1 m/s
3m(3s)
8m(8s)8m(8s)
3m(3s)
8m(8s)
3m(3s)
3m(3s)
3m(3s)
3m(3s)
The Constraint is 1 Gopher per hole
10080 - Gopher II Hints: Use the function() Hints: do a transformation Hints: one Gopher = 1 unit of flow
HoleHole
HoleHole
HoleHole
The Constraint is 1 Gopher per hole
10080 - Gopher IISolution:
HoleHole
HoleHole
HoleHole
Source Sink
1
1
1
1
1
1
Let’s Try
• Want More?
563 - Crimewave
10 mins break. (Again, it is not actually a break. It is expected that you read the problem, and try to find the solution with your neighbor. )
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=504
563 - Crimewave
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=7&page=show_problem&problem=504
Banks
Get-away routes
City MapIs it possible that no two get-away routes use the same crossing?
563 - Crimewave
Hint: Use the function()
A
D
E
B
C
Only one route can pass through each node…
But we can have constraints on edge only…
563 - Crimewave
Hint: Use the function()
A
D
E
B
C
Only one route can pass through each node…
Banks as Sources.City Bounds as Sink.
563 - Crimewave
Hint: Use the function()
A
D
E
B
C
Is this okay?
1
1
11
1
1 1
1
563 - Crimewave
Hint: Use the function()
A1
D
E
B
C
Is this okay?
A21
563 - Crimewave
Hint: Use the function()
A1
D
E
B
C
Is this okay?
A21
A
Let’s Try
Don’t worry. It is normal that you cannot get the solution right away. These problems are really difficult. That’s why we have to meet every week.
Review
What processes were used when we solved the problem(s)?– Read and understand the problem– Transform the problem– Apply an “known” algorithm– Get back the solution
Review
What processes were used when we solved the problem(s)?– Read and understand the problem– Transform the problem– Apply an “known” algorithm– Get back the solution– Code the problem– Way to represent the graph in the program (Data-
Structure)
Review
What processes were used when we solved the problem(s)?– Read and understand the problem– Transform the problem– Apply an “known” algorithm– Get back the solution– Code the problem– Way to represent the graph in the program (Data-
Structure)
INPUT /OUTPUTLet’s get the hands dirty
Let’s get the hands dirty
• 483 - Word Scramble– http://uva.onlinejudge.org/index.php?
option=com_onlinejudge&Itemid=8&category=6&page=show_problem&problem=424
This is a warm-up problem. You have to code this time.
Start your notepad/ DEV C++/ vi/ gedit …
483 - Word Scramble
• Common way to read lines string line;while (getline(cin, line)){ // .. to process a line}
483 - Word Scramble
● For each line, we need to get the word (Token).
istringstream ins(line);string word;while (ins >> word){ // reverse word}
483 - Word Scramble
● The last part is to reverse a word.● You may instantly thinking about a loop to
do this.
● Let’s try the STL reverse() function.
for (size_t i = 0; i < words.size(); ++i) cout << words[words.size() - i - 1];
reverse(word.begin(), word.end());cout << word;
reverse(…, …)
• What does the reverse(…, …) actually do?1.Go to http://cplusplus.com/2.Search reverse.
reverse(…, …)
• void reverse ( BidirectionalIterator first, BidirectionalIterator last);
H E L L O
First( String.begin() )
Last( String.end() )
reverse(…, …)H E L L O
First( String.begin() )
Last( String.end() )
Parametersfirst, last Bidirectional iterators to the initial and final positions of the
sequence to be reversed. The range used is [first,last), which contains all the elements between first and last, including the element pointed by first but not the element pointed by last.
STL
Standard Template Library• Provides common library functions like
reverse(…, …)• More Example,
1. Go to http://cplusplus.com/2. See Reference STL Algorithms
STL
Standard Template Library• Provides common container classes• Hey, but, but what is container class?
STL
Standard Template Library• Array is an example of container, but it has
limited functionality such as– The size is fixed– The index must be non-negative integer
STL
Standard Template Library• When I was a child, I always dreamt to have a
non-fixed size array, now the dream comes true. It is called vector
Vector#include <iostream> #include <vector> using namespace std;
int main () { vector<int> myvector (10); // 10 zero-initialized elements
// assign some values: myvector[9] = 5;cout << myvector[9] << endl;
myvector.push_back (15);myvector.push_back (16);
cout << myvector[10] << “, “ << myvector[11] << endl; // 15, 16}
STL
Standard Template Library• When I was a child, I also always dreamt to
have an array that can use “string”, “char”, … as its index.– table[“denon”] = “smart”;– table[“david”] = “stupid”;
• Now the dream comes true again, it is called map
Map#include <iostream> #include <map> using namespace std;
int main () { map<char,int> table; first['a']=10; first['b']=30; first['c']=50;
cout << table[‘a’] << “, “ << table[‘c’] << endl; //10, 50
return 0; }
STL
• More containers and algorithms will be covered in the following trainings
• For more information,– http://cplusplus.com/– http://www.sgi.com/tech/stl
Exercises
• 642 - Word Amalgamation• 401 - Palindromes• 10107 - What is the Median?
ADMINHere is the boring part…
Admin
Website: http://www.cs.hku.hk/~provinci
Mailing list: provinci@cs.hku.hk
Coach: Chan Ho Leung
Email: hlchan@cs.hku.hk
Admin
• We’ll have a Mini Contest 1. test at Mar 11 (Thur) 18:30-21:00.
● To prepare for the Mini Contest 1, you can join our training sessions every Thursday in HW-311.– These training sessions are optional and you can
join the contest directly.
Admin
• Tentative schedule
http://i.cs.hku.hk/~provinci/training.html
PRACTICE MAKE PERFECTFinally…
Practice make perfect
● An archive of problems can be found in the site– http://acm.uva.es/p
● It also has an on-line judge to test your submitted program.– Simply register and apply for an account.
● This archive site will be a major source of training questions.
Practice make perfect
● Useful resources…
http://ctldenon.net/resources.php
Warm-up problems:• 642 - Word Amalgamation• 401 - Palindromes• 10107 - What is the Median?
That’s All
Thank you
Recommended