110 likes | 464 Views
Intro to USACO Strategy. U-SA-CO. USACO Basics. If "prog" is the name of the problem: Read from "prog.in" and write to "prog.out" Use freopen if you use C/C++ Use BufferedReader if you use Java Make sure class and source names match E.g. "Main.java" and "public class Main {" Templates:
E N D
Intro to USACO Strategy U-SA-CO
USACO Basics • If "prog" is the name of the problem: • Read from "prog.in" and write to "prog.out" • Use freopen if you use C/C++ • Use BufferedReader if you use Java • Make sure class and source names match • E.g. "Main.java" and "public class Main {" • Templates: • C++: http://pastebin.com/NP1GuePX • has some useful macros • Java: http://pastebin.com/tD8z5iMy • has some simplifications for i/o • Yes, putting 30 #include statements and "throws Exception" everywhere is bad style :P
Preparing for the USACO • train.usaco.org • Organized in chapters and sections • Has explanations for necessary algorithms • The problems get hard quickly. You do not need to know all six chapters • past USACO problems • Google them! • Miscellaneous online websites • codeforces.com • Highly recommended • Want to make gold? Try doing 300 problems :D • poj.org • Our PotWs • Free lectures, yo Cows <3
USACO Tips • Read the problem CAREFULLY • Check boundaries • Running time is VERY important • ~108 operations per sec (conservative estimate) • Do not start coding until you know how to solve the problem • Of course, if you have no idea or are running out of time, brute force • Code! • Use Google wisely • Test your program if you have time • Check small cases by hand • Make a random test case generator
Random Coding Tips • Watch out for off-by-one errors • Make arrays slightly bigger than needed • Watch out for overflow • int = ~109 maximum • long = ~1018 maximum • Debugging • Avoid it. • Use print statements • "Rubber duck debugging" • Variable names • l is the worst variable name ever • Use pseudocode/scratch paper • Use shortcuts Speak to the duck
Things to Avoid • Spending too much time on one problem • You can always come back • You need near perfect to be promoted to higher divisions • Coding a solution only to discover your method doesn't work on the sample case • Premature optimization • Being sketchy • Using long variable names • Typing takes longer than you think • Excessive commenting • Just a few words for yourself • Multitasking on your computer :'( Avoid this.
What happens after my timer is over? • Congrats on your hard work • Results announced the following week • ~Thursday • Rankings can be checked on tjhsst's website • ~Following weekend...? • Go practice more
PotW! • Bessie is going trick or treating. • There are N consecutive houses, numbered 1 ... N. • The i_th house gives a_i pieces of candy. • Bessie must visit a nonempty consecutive sequence of houses. • Find the maximum amount of candy she can get. • Input Format • Line 1: A single integer: N • Line 2: N space separated integers: a_1 ... a_N • Output Format • Line 1: The maximum amount of candy she can get. • Constraints • 15 Points: N < 1000, |a_i| < 1000 • 25 Points: N < 100,000, |a_i| < 10^9 • 35 Points: Maximize the amount of candy mod M. • Line 1: Two space separated integers, N, M • N < 100,000, 0 < a_i < M < 10^9
Hint for PotW • Bessie is going trick or treating. • There are N consecutive houses, numbered 1 ... N. • The i_th house gives a_i pieces of candy. • Bessie must visit a nonempty consecutive sequence of houses. • Find the maximum amount of candy she can get. • Example • N = 6, a = {1, -2, 3, -1, 4, -3} • The best sum is 3 + (-1) + 4 = 6 • Notice that this is a difference of prefix-sums • (1 + -2 + 3 + -1 + 4) - (1 + -2) = 5 - (-1) = 6 • How do you maximize this? • X - Y is maximized when Y is minimized. • Keep track of what sum to subtract -- you always want to subtract the most negative one.