ACM-ICPCTCGroupB EngForProblemSolving Printversion
description
Transcript of ACM-ICPCTCGroupB EngForProblemSolving Printversion
ACM-‐ICPC Thailand Central Group B Training Program
@ Faculty of ICT, Mahidol University
Apirak Hoonlor July 14, 2012
1
Credits
• Halim et al., CompeJJve Programming • Skiena et al, Programming Challenges
• Miguel A. Revilla, Art of Programming Contest
• UVa ACM-‐ICPC quesJons archive
2
CompeJJve Programming • Given well-‐known Computer Science problems, – Solve them as fast as possible – SoPware development processes Forget about it. – Good style of programming with comments Limited amount of comments.
• Well-‐known == not research problems – Has soluJons – Can be solved using known algorithms.
4
Tip 1: Type Fast & Correct
• No Kidding, this can be VERY important. • h\p://www.typingtest.com
• Familiarize yourself with the posiJons of { } [ ] < > ’ ” & | ! , ; . ( ) etc
• How to be be\er? Program more. A lot more.
6
Tip 2: Quickly read the quesJon and IdenJfying problem types
• Ad Hoc • Complete Search
• Divide and Conquer • Greedy • Dynamic Programming
7
• Graph • MathemaJcs
• String Processing • ComputaJonal Geometry
• Others
Understanding the quesJon • Read the problem carefully • Skim the quesJon to filter out background/history that does not impact the soluJon.
• Pay a\enJon to input/output descripJon and the sample input/output.
• Do not assume (just because the examples have certain properJes) – Make note of special inputs (break sequence, end of line, etc)
8
Tip 3: Do Algorithm Analysis
• Constraints in the problem statement – MUST KNOW THEM
• Don’t be afraid to use the simplest that works – Efficiency is key??? Not really – Check maximum possible size and decide if brute force will be ok.
• At least do some basic analysis to convince that it will work BEFORE begin coding.
9
Tip 4: Master Programming Language(s)
• Goal: Able to QUICKLY translate the soluJon into a bug-‐free code.
• Master AT LEAST ONE programming languages – Save Jme of checking references – Use shortcuts, macros – Use libraries whenever possible – Know basic funcJons by heart
10
Programming Hints
• Use symbolic constants and variable names – Four suites of card club, heart, spade, diamond. – Simply use C, H, S, D
• Avoid redundant code – use subrouJne
• Get to know the debugger • In long program, it may help to write key comments
12
Tip 5: Be master tester UlJmately we want “Accepted (AC)” verdict. • Test the given input • Test incorrect input • Test boundary condiJons • Test examples that you know of correct answer
• Test Big Examples if the data can be produced quickly
13
UVa 12346 -‐ Water Gate Management A dam has n water gates to let out water when necessary. Each water gate has its own capacity, water path and affected areas in the downstream. The affected areas may have a risk of flood when the water gate is open. The cost of potenJal damage caused by a water gate is measured in number calculated from the number of people and areas esJmated to get affected. Suppose a water gate Gi has the volumetric flow rate of Fi m3/hour and the damage cost of Ci. In a certain situaJon, the dam has the volume V m3 of water to flush out within T hours. Your task is to manage the opening of the water gates in order to get rid of at least the specified volume of water within a limited Jme in condiJon that the damage cost is minimized. For example, a dam has 4 water gates and their properJes are shown in the following table.
Water Gate G1 G2 G3 G4
Flow rate (m3/hour) 720,000 50,000 130,000 1,200,000
Cost 120,000 60,000 50,000 150,000
Case 1: You have to flush out the water 5 million m3 within 7 hours. The minimum cost will be 120,000 by lezng the water gate G1 open for 7 hours.
Case 2: You have to flush out the water 5 million m3 within 30 hours. The minimum cost will be 110,000 by lezng the water gates G2 and G3 open, for example, G2 is open for 29 hours and G3 is open for 28 hours. Note that each water gate is independent and it can be open only in a unit of whole hour (no fracJon of hour).
16
Input: The first line includes an integer n indicaJng number of water gates (1 ≤ n ≤ 20). Then the next n lines contain, in each line, two integers: Fi and Ci corresponding to the flow rate (m3/hour) and the damage cost of the water gate Gi respecJvely. The next line contains the number m which is the number of test cases (1 ≤ m ≤ 50). The following m lines contain, in each line, two integers: V and T corresponding to the volume (m3) of water to let out within T hours. (1 ≤ Fi ,V, Ci ≤ 109 , 1 ≤ T ≤ 1000)
Output: For each test case, print out the minimum cost in the exact format shown in the sample output below. If it is not possible to let out the water of volume V in T hours from the dam, print out “IMPOSSIBLE” (without quotaJon marks).
17
Sample input Sample output
4 Case 1: 120000
720000 120000 Case 2: 110000
50000 60000 Case 3: IMPOSSIBLE
130000 50000
1200000 150000
3
5000000 7
5000000 30
63000000 24
18
Thailand NaJonal Contest 2009 -‐ My Ancestor Your great great ... great grandfather 'Tiger' was a famous Muay Thai (Thai Boxing) master. He was renowned for his trademark 'Teh Chiang' (Diagonal Kick). This skill has surely well preserved within your family as you have inherited his Teh Chiang plus all other skills from your ancestors and many more, e.g. Chok (Punch), Tee Sok (Elbow), Teh (Kick), Tee Kao (Knee), Teep (Foot-‐thrust) techniques. Your family has a very good Muay Thai training program so that all children in your family always have strictly greater level of skill than their parents. Given a family structure (only males are listed) and the skill level of each family member, you want to know which of your greatest ancestor (the man closest to 'Tiger' in family structure and may include yourself) is the first man to reach some level of skill.
19
Input and Output The input contains mulJple test cases; please process each test case unJl end of file. There is NO blank line between test cases. The first line of each test case contains two integers: N, Q. N denotes the number of person in your family. 1 ≤ N ≤ 200000. Q denotes the number of queries. 1 ≤ Q ≤ 100000. Then, N-‐1 lines appear, line 1 unJl line N-‐1. The i-‐th line contains two integers: pri and lvi. 0 ≤ pri ≤ N-‐2; 1 ≤ lvi ≤ 231-‐1. This means person i has skill level lvi and has pri as his parent. Finally, Q lines appear, line 1 unJl line Q. The i-‐th line contains two integers: person and P. 0 < person ≤ N-‐1; 1 ≤ P ≤ lvperson.
For each query, please print the index of greatest ancestor of person with skill level greater than or equal to P.
Tiger has index = 0, pr0 = -‐1, and lv0 = 1, but there will be no query with person = 0, so this boundary case can be ignored.
20
21
ExplanaJon: Observe that there is no blank line between the outputs of these two idenJcal test cases!
Tips and Tricks • Locate keywords: – for comparison/boundary: greater than, strictly less than, more than, at least, at most, exactly.
– for quanJficaJon: cost, value, age, rate, level. – for input/output: ignored, lines, appear, number, print, no empty line.
– for symbolic terms: n, s, lv, F, C, etc.
• Look for constraints – 1 ≤ N ≤ 200000
• Ignore the (boring) story. 22