昨天发疯了一天,博客一直没补上,这两天赶快写几篇补上吧。表示要写出让自己看着舒服的博客也挺累。。。
| Complete Search |
The Idea
Solving a problem using complete search is based on the ``Keep It Simple,Stupid'' principle. The goal of solving contest problems is to write programsthat work in the time allowed, whether or not there is a faster algorithm.
Complete search exploits the brute force, straight-forward, try-them-allmethod of finding the answer. This method should almost always be the firstalgorithm/solution you consider. If this works within time and spaceconstraints, then do it: it's easy to code and usually easy to debug. Thismeans you'll have more time to work on all the hard problems, where brute forcedoesn't work quickly enough.
In the case of a problem with only fewer than a couple millionpossibilities, iterate through each one of them, and see if the answer works.
Careful, Careful
Sometimes, it's not obvious that you use this methodology.
Problem: Party Lamps [IOI 98]
You are given N lamps and four switches. The first switch toggles alllamps, the second the even lamps, the third the odd lamps, and last switchtoggles lamps 1, 4, 7, 10, ... .
Given the number of lamps, N, the number of button presses made (upto 10,000), and the state of some of the lamps (e.g., lamp 7 is off), outputall the possible states the lamps could be in.
Naively, for each button press, you have to try 4 possibilities, for atotal of 410000 (about 106020 ), which means there's noway you could do complete search (this particular algorithm would exploitrecursion).
Noticing that the order of the button presses does not matter gets thisnumber down to about 100004 (about 1016 ), still too bigto completely search (but certainly closer by a factor of over 106000).
However, pressing a button twice is the same as pressing the button notimes, so all you really have to check is pressing each button either 0 or 1times. That's only 24 = 16 possibilities, surely a number ofiterations solvable within the time limit.
Problem 3: The Clocks [IOI 94]
A group of nine clocks inhabits a 3 x 3 grid; each is set to 12:00, 3:00,6:00, or 9:00. Your goal is to manipulate them all to read 12:00.Unfortunately, the only way you can manipulate the clocks is by one of ninedifferent types of move, each one of which rotates a certain subset of theclocks 90 degrees clockwise.
Find the shortest sequence of moves which returns all the clocks to 12:00.
The ``obvious'' thing to do is a recursive solution, which checks to seeif there is a solution of 1 move, 2 moves, etc. until it finds a solution. Thiswould take 9k time, where k is the number of moves.Since k might be fairly large, this is not going to run with reasonabletime constraints.
Note that the order of the moves does not matter. This reduces the timedown to k9 , which isn't enough of an improvement.
However, since doing each move 4 times is the same as doing it no times,you know that no move will be done more than 3 times. Thus, there are only 49possibilities, which is only 262,144, which, given the rule of thumb forrun-time of more than 10,000,000 operations in a second, should work in time.The brute-force solution, given this insight, is perfectly adequate.
Sample Problems
Milking Cows [USACO 1996 Competition Round]
Given a cow milking schedule (Farmer A milks from time 300 to time 1000,Farmer B from 700 to 1200, etc.), calculate
- The longest time interval in which at least one cow was being milked
- The longest time interval in which no cow is being milked
Perfect Cows & Perfect Cow Cousins [USACO 1995Final Round]
A perfect number is one in which the sum of the proper divisors add up tothe number. For example, 28 = 1 + 2 + 4 + 7 + 14. A perfect pair is a pair ofnumbers such that the sum of the proper divisor of each one adds up to theother. There are, of course, longer perfect sets, such that the sum of thedivisors of the first add up to the second, the second's divisors to the third,etc., until the sum of the last's proper divisors add up to the first number.
Each cow in Farmer John's ranch is assigned a serial number. from 1 to32000. A perfect cow is one which has a perfect number as its serial. A groupof cows is a set of perfect cow cousins if their serial numbers form a perfectset. Find all perfect cows and perfect cow cousins.
初次一看这个吓尿了。。。辣么长一篇课文,还加上了几道IOI的题目来做例题吓人,还他喵的没有题解= =!要这玩意有甚卵用。。。好了不说废话,把我看得懂的翻译粗来吧。。。
枚举应遵循KISS(Keep It Simple and Stupid)原则,反正程序的目的在于在题目规定的时间得出答案,在这个前提下,代码应该越简单越好,管它有什么更好的算法(←_←好像很对的样子)总之就是简单粗暴,直来直去!
然后。。。就没有了,一堆题目,不会做。。。翻译那作甚