welcoming lecture meeting/discussion/party by david/denon

68
By David/Denon

Upload: derek-reynolds

Post on 20-Jan-2016

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Welcoming Lecture Meeting/Discussion/Party By David/Denon

By David/Denon

Page 2: Welcoming Lecture Meeting/Discussion/Party By David/Denon

First of All…

Welcome guys

Powered by Google Translator

Page 3: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Agenda

• Contest Format• Taste the challenges• Input /Output• STL introduction• Admin

Page 4: Welcoming Lecture Meeting/Discussion/Party By David/Denon

THE CONTESTACM ICPC

Page 5: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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)

Page 6: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 7: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 8: Welcoming Lecture Meeting/Discussion/Party By David/Denon

The Contest

while ( ! isTimeout() ) {read();think();program();//debug();submit();

}

Page 9: Welcoming Lecture Meeting/Discussion/Party By David/Denon

One Balloon as a gift for solving a problem.Each problem is associated with a color.

Page 10: Welcoming Lecture Meeting/Discussion/Party By David/Denon

HOW?Solve Problem

Page 11: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 12: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Let’s Try

So Si

4 1

2 3 2

3

4

So = SourceSi = Sink

Page 13: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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?

Page 14: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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?

Page 15: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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)

Page 16: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 17: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Max-Flow Problem

Max-Flow Problem• When given a graph, what is the max. valid

flow on it?

Page 18: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 19: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 20: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 21: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 22: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Max-Flow Problem

• Transformation

S S

4 1

2 3 2

4

S S

4

4 3

Page 23: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Max-Flow Problem

• Transformation

S Si

4 1

2 3 2

4

S Si

4

4 3

S’Si’

Page 24: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Max-Flow Problem

• Transformation

SS SiSi

4 1

2 3 2

4

SS SiSi

4

4 3

S’Si’

Page 25: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Max-Flow Problem

• Transformation

SS SiSi

4 1

2 3 2

4

SS SiSi

4

4 3

S’Si’

infiinfi

infi infi

Page 26: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Can you solve the following problem?10080 - Gopher II

Hints: Use the function() Hints: do a transformation

Page 27: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 28: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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)

Page 29: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 30: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 31: Welcoming Lecture Meeting/Discussion/Party By David/Denon

10080 - Gopher IISolution:

HoleHole

HoleHole

HoleHole

Source Sink

1

1

1

1

1

1

Page 32: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Let’s Try

• Want More?

Page 33: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 34: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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?

Page 35: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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…

Page 36: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 37: Welcoming Lecture Meeting/Discussion/Party By David/Denon

563 - Crimewave

Hint: Use the function()

A

D

E

B

C

Is this okay?

1

1

11

1

1 1

1

Page 38: Welcoming Lecture Meeting/Discussion/Party By David/Denon

563 - Crimewave

Hint: Use the function()

A1

D

E

B

C

Is this okay?

A21

Page 39: Welcoming Lecture Meeting/Discussion/Party By David/Denon

563 - Crimewave

Hint: Use the function()

A1

D

E

B

C

Is this okay?

A21

A

Page 40: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 41: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 42: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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)

Page 43: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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)

Page 44: Welcoming Lecture Meeting/Discussion/Party By David/Denon

INPUT /OUTPUTLet’s get the hands dirty

Page 45: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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 …

Page 46: Welcoming Lecture Meeting/Discussion/Party By David/Denon

483 - Word Scramble

• Common way to read lines string line;while (getline(cin, line)){ // .. to process a line}

Page 47: Welcoming Lecture Meeting/Discussion/Party By David/Denon

483 - Word Scramble

● For each line, we need to get the word (Token).

istringstream ins(line);string word;while (ins >> word){ // reverse word}

Page 48: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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;

Page 49: Welcoming Lecture Meeting/Discussion/Party By David/Denon

reverse(…, …)

• What does the reverse(…, …) actually do?1.Go to http://cplusplus.com/2.Search reverse.

Page 50: Welcoming Lecture Meeting/Discussion/Party By David/Denon

reverse(…, …)

• void reverse ( BidirectionalIterator first, BidirectionalIterator last);

H E L L O

First( String.begin() )

Last( String.end() )

Page 51: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 52: Welcoming Lecture Meeting/Discussion/Party By David/Denon

STL

Standard Template Library• Provides common library functions like

reverse(…, …)• More Example,

1. Go to http://cplusplus.com/2. See Reference STL Algorithms

Page 53: Welcoming Lecture Meeting/Discussion/Party By David/Denon

STL

Standard Template Library• Provides common container classes• Hey, but, but what is container class?

Page 54: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 55: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 56: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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}

Page 57: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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

Page 58: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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; }

Page 59: Welcoming Lecture Meeting/Discussion/Party By David/Denon

STL

• More containers and algorithms will be covered in the following trainings

• For more information,– http://cplusplus.com/– http://www.sgi.com/tech/stl

Page 60: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Exercises

• 642 - Word Amalgamation• 401 - Palindromes• 10107 - What is the Median?

Page 61: Welcoming Lecture Meeting/Discussion/Party By David/Denon

ADMINHere is the boring part…

Page 62: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Admin

Website: http://www.cs.hku.hk/~provinci

Mailing list: [email protected]

Coach: Chan Ho Leung

Email: [email protected]

Page 63: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 64: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Admin

• Tentative schedule

http://i.cs.hku.hk/~provinci/training.html

Page 65: Welcoming Lecture Meeting/Discussion/Party By David/Denon

PRACTICE MAKE PERFECTFinally…

Page 66: Welcoming Lecture Meeting/Discussion/Party By David/Denon

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.

Page 67: Welcoming Lecture Meeting/Discussion/Party By David/Denon

Practice make perfect

● Useful resources…

http://ctldenon.net/resources.php

Warm-up problems:• 642 - Word Amalgamation• 401 - Palindromes• 10107 - What is the Median?

Page 68: Welcoming Lecture Meeting/Discussion/Party By David/Denon

That’s All

Thank you