Pro.ID10110 TitleCrafting Winning Solutions Title链接http://10.20.2.8/oj/exercise/problem?problem_id=10110 AC0 Submit0 Ratio- 时间&空间限制描述A good way to get a competitive edge is to write down a game plan for what you're going to do in a contest round. This will help you script out your actions, in terms of what to do both when things go right and when things go wrong. This way you can spend your thinking time in the round figuring out programming problems and not trying to figure out what the heck you should do next... it's sort of like precomputing your reactions to most situations. Mental preparation is also important. Game Plan For A Contest RoundRead through ALL the problems FIRST; sketch notes with algorithm, complexity, the numbers, data structs, tricky details, ...
Coding a problem - For each, one at a time:
Time management strategy and "damage control" scenariosHave a plan for what to do when various (foreseeable!) things go wrong; imagine problems you might have and figure out how you want to react. The central question is: "When do you spend more time debugging a program, and when do you cut your losses and move on?". Consider these issues:
Have a checklist to use before turning in your solutions:
Tips & Tricks
ComplexityBasics and order notationThe fundamental basis of complexity analysis revolves around the notion of "big oh" notation, for instance: O(N). This means that the algorithm's execution speed or memory usage will double when the problem size doubles. An algorithm of O(N2) will run about four times slower (or use 4x more space) when the problem size doubles. Constant-time or space algorithms are denoted O(1). This concept applies to time and space both; here we will concentrate discussion on time. One deduces the O() run time of a program by examining its loops. The most nested (and hence slowest) loop dominates the run time and is the only one mentioned when discussing O() notation. A program with a single loop and a nested loop (presumably loops that execute N times each) is O(N2), even though there is also a O(N) loop present. Of course, recursion also counts as a loop and recursive programs can have orders like O(bN), O(N!), or even O(NN). Rules of thumb
ExamplesA single loop with N iterations is O(N): A double nested loop is often O(N2): Consider this well balanced binary tree with four levels: An algorithm that traverses a general binary tree will have complexity O(2 N). Solution ParadigmsGenerating vs. FilteringPrograms that generate lots of possible answers and then choose the ones that are correct (imagine an 8-queen solver) are filters. Those that hone in exactly on the correct answer without any false starts are generators. Generally, filters are easier (faster) to code and run slower. Do the math to see if a filter is good enough or if you need to try and create a generator. PrecomputationSometimes it is helpful to generate tables or other data structures that enable the fastest possible lookup of a result. This is called precomputation (in which one trades space for time). One might either compile precomputed data into a program, calculate it when the program starts, or just remember results as you compute them. A program that must translate letters from upper to lower case when they are in upper case can do a very fast table lookup that requires no conditionals, for example. Contest problems often use prime numbers - many times it is practical to generate a long list of primes for use elsewhere in a program. Decomposition (The Hardest Thing At Programming Contests)While there are fewer than 20 basic algorithms used in contest problems, the challenge of combination problems that require a combination of two algorithms for solution is daunting. Try to separate the cues from different parts of the problem so that you can combine one algorithm with a loop or with another algorithm to solve different parts of the problem independently. Note that sometimes you can use the same algorithm twice on different (independent!) parts of your data to significantly improve your running time. SymmetriesMany problems have symmetries (e.g., distance between a pair of points is often the same either way you traverse the points). Symmetries can be 2-way, 4-way, 8-way, and more. Try to exploit symmetries to reduce execution time. For instance, with 4-way symmetry, you solve only one fourth of the problem and then write down the four solutions that share symmetry with the single answer (look out for self-symmetric solutions which should only be output once or twice, of course). Forward vs. BackwardSurprisingly, many contest problems work far better when solved backwards than when using a frontal attack. Be on the lookout for processing data in reverse order or building an attack that looks at the data in some order or fashion other than the obvious. SimplificationSome problems can be rephrased into a somewhat different problem such that if you solve the new problem, you either already have or can easily find the solution to the original one; of course, you should solve the easier of the two only. Alternatively, like induction, for some problems one can make a small change to the solution of a slightly smaller problem to find the full answer. 输入This is NOT a problem , but a course. Read it , but not Submit. 输出Description A good way to get a competitive edge is to write down a game plan for what you're going to do in a contest round. This will help you script out your actions, in terms of what to do both when things go right and when things go wrong. This way you can spend your thinking time in the round figuring out programming problems and not trying to figure out what the heck you should do next... it's sort of like precomputing your reactions to most situations. Mental preparation is also important. Game Plan For A Contest RoundRead through ALL the problems FIRST; sketch notes with algorithm, complexity, the numbers, data structs, tricky details, ...
Coding a problem - For each, one at a time:
Time management strategy and "damage control" scenariosHave a plan for what to do when various (foreseeable!) things go wrong; imagine problems you might have and figure out how you want to react. The central question is: "When do you spend more time debugging a program, and when do you cut your losses and move on?". Consider these issues:
Have a checklist to use before turning in your solutions:
Tips & Tricks
ComplexityBasics and order notationThe fundamental basis of complexity analysis revolves around the notion of "big oh" notation, for instance: O(N). This means that the algorithm's execution speed or memory usage will double when the problem size doubles. An algorithm of O(N2) will run about four times slower (or use 4x more space) when the problem size doubles. Constant-time or space algorithms are denoted O(1). This concept applies to time and space both; here we will concentrate discussion on time. One deduces the O() run time of a program by examining its loops. The most nested (and hence slowest) loop dominates the run time and is the only one mentioned when discussing O() notation. A program with a single loop and a nested loop (presumably loops that execute N times each) is O(N2), even though there is also a O(N) loop present. Of course, recursion also counts as a loop and recursive programs can have orders like O(bN), O(N!), or even O(NN). Rules of thumb
ExamplesA single loop with N iterations is O(N): A double nested loop is often O(N2): Consider this well balanced binary tree with four levels: An algorithm that traverses a general binary tree will have complexity O(2 N). Solution ParadigmsGenerating vs. FilteringPrograms that generate lots of possible answers and then choose the ones that are correct (imagine an 8-queen solver) are filters. Those that hone in exactly on the correct answer without any false starts are generators. Generally, filters are easier (faster) to code and run slower. Do the math to see if a filter is good enough or if you need to try and create a generator. PrecomputationSometimes it is helpful to generate tables or other data structures that enable the fastest possible lookup of a result. This is called precomputation (in which one trades space for time). One might either compile precomputed data into a program, calculate it when the program starts, or just remember results as you compute them. A program that must translate letters from upper to lower case when they are in upper case can do a very fast table lookup that requires no conditionals, for example. Contest problems often use prime numbers - many times it is practical to generate a long list of primes for use elsewhere in a program. Decomposition (The Hardest Thing At Programming Contests)While there are fewer than 20 basic algorithms used in contest problems, the challenge of combination problems that require a combination of two algorithms for solution is daunting. Try to separate the cues from different parts of the problem so that you can combine one algorithm with a loop or with another algorithm to solve different parts of the problem independently. Note that sometimes you can use the same algorithm twice on different (independent!) parts of your data to significantly improve your running time. SymmetriesMany problems have symmetries (e.g., distance between a pair of points is often the same either way you traverse the points). Symmetries can be 2-way, 4-way, 8-way, and more. Try to exploit symmetries to reduce execution time. For instance, with 4-way symmetry, you solve only one fourth of the problem and then write down the four solutions that share symmetry with the single answer (look out for self-symmetric solutions which should only be output once or twice, of course). Forward vs. BackwardSurprisingly, many contest problems work far better when solved backwards than when using a frontal attack. Be on the lookout for processing data in reverse order or building an attack that looks at the data in some order or fashion other than the obvious. SimplificationSome problems can be rephrased into a somewhat different problem such that if you solve the new problem, you either already have or can easily find the solution to the original one; of course, you should solve the easier of the two only. Alternatively, like induction, for some problems one can make a small change to the solution of a slightly smaller problem to find the full answer. Input This is NOT a problem , but a course. Read it , but not Submit. Output This is NOT a problem , but a course. Read it , but not Submit. Sample Input This is NOT a problem , but a course. Read it , but not Submit. Sample Output This is NOT a problem , but a course. Read it , but not Submit. Source 样例输入This is NOT a problem , but a course. Read it , but not Submit. 样例输出This is NOT a problem , but a course. Read it , but not Submit. 作者 |