90 likes | 232 Views
Moooooo. Or: How I learned to stop worrying and love USACO. Or: How I learned the solution to last week’s PotW. 20 point solution: import java.io.*; public class PotW1 { public static void main(String[] Args ) throws IOException { BufferedReader in = new BufferedReader (
E N D
Moooooo Or: How I learned to stop worrying and love USACO
Or: How I learned the solution to last week’s PotW 20 point solution: import java.io.*;public class PotW1{ public static void main(String[] Args) throws IOException {BufferedReader in = new BufferedReader( new InputStreamReader(System.in));intnumCows = Integer.parseInt(in.readLine());intninjaCow = numCows; for (inti = 1; i < numCows; i++)ninjaCow = ninjaCow ^ i ^ Integer.parseInt( in.readLine()); System.out.println(ninjaCow); }}
General Info • Main US high school programming contest • Sign up online - http://contest.usaco.org • Training pages - http://train.usaco.org • Similar to USA(J)MO • 6 contests each year • Three divisions - Bronze, Silver, Gold • Silver and Gold are by invitation only • Higher divisions are more difficult • Exact schedule TBD • Qualification round probably at end of October • The format for qualification round is different • Again, TBD, so look for updates
Format • 3 - 5 problems • 3 - 5 hours (that's right: 1 - 2 hours per problem!) • Problems are algorithmic • Occasionally need some math • Given a problem statement • And sample input / output • Write a program! • Subject to time and memory constraints • Multiple submissions allowed • Last submission by end of contest will be tested • This test data is unknown to you • Get points for each test case you pass
Details • Example: if the name of the task is "cowmilk" • I/O • Scan from a file - "cowmilk.in" • Write to another file - "cowmilk.out" • Header (place this comment at the top of code): /* ID: username TASK: cowmilk LANG: JAVA ( or C++ ) */ • Read the rules for more details
Java I/O import java.io.*; import java.util.*; BufferedReaderbr = new BufferedReader(new FileReader("cowmilk.in")); Scanner s = new Scanner(new File("cowmilk.in")); PrintWriter o = new PrintWriter(new BufferedWriter(new FileWriter("cowmilk.out"))); In C++: #include <fstream> using namespace std; fstreami("cowmilk.in"),o("cowmilk.out"); (yes, really)
Time Flies! • Time limit is very important • Depends on the efficiency of your algorithm • e.g. how quickly can you add up the numbers from 1 - N? • one step - N*(N+1)/2 • Java can do about 200,000,000 operations per second (grader scales to 700MHz) • dividing doubles, calling methods, casting classes, etc. • Estimate how many operations your solution will do • C/C++ and Pascal are often faster than Java • Memory is not that important • Do not waste time making premature optimizations in speed • Often okay to sacrifice a little efficiency for ease of coding • Only make optimizations that can increase speed by an order of magnitude
STRATEGY • Practice • Preferably before the contest • Read each problem: • Read the problem again • Read the time / memory constraints • Read the sample input / output • Think logically. Notice small details. Be flexible and creative. • So you think you have a solution? • Make sure you can actually code your solution • Check if your solution works by hand before coding • Look for corner cases • Test your solution on your own data • Feel free to skip problems - they are never in order of difficulty • Partial solutions are also good
PotW – The White Textfile • In a fit of schizophrenic hatred for Microsoft, you decide to go "creep" around in Windows Explorer, normalizing all of your text files into a less "revolting" format. (sorry, no cows this time) • Your goal is to take a text file, which is composed of words delimited by any whitespace (' ', '\n', '\t', '\r'), and convert it into a text file that is composed of words delimited by '\n' only. We will be using USACO conventions for I/O. Sample input ("whitetext.in"): " abcddcba\nuvw\r\nz \t" Sample output ("whitetext.out"): "abcd\ndcba\nuvw\nz" - no ending or starting newlines (without the quotations, of course) You will receive 8 pts for each solution you submit (up to 16 total) The two solutions must be radically different.