Transcript Document

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));
int numCows = Integer.parseInt(in.readLine());
int ninjaCow = numCows;
for (int i = 1; i < numCows; i++)
ninjaCow = ninjaCow ^ i ^ Integer.parseInt(
in.readLine());
}
}
System.out.println(ninjaCow);
General Info
• Main US high school programming contest
o Sign up online - http://contest.usaco.org
o Training pages - http://train.usaco.org
o Similar to USA(J)MO
• 6 contests each year
• Three divisions - Bronze, Silver, Gold
o Silver and Gold are by invitation only
o Higher divisions are more difficult
• Exact schedule TBD
o Qualification round probably at end of October
o 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
o Occasionally need some math
• Given a problem statement
o
o
o
o
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
o This test data is unknown to you
o Get points for each test case you pass
Details
• Example: if the name of the task is "cowmilk"
• I/O
o Scan from a file - "cowmilk.in"
o 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.*;
BufferedReader br = 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;
fstream i("cowmilk.in"),o("cowmilk.out");
(yes, really)
Time Flies!
• Time limit is very important
o Depends on the efficiency of your algorithm
o e.g. how quickly can you add up the numbers from 1 - N?
• one step - N*(N+1)/2
o Java can do about 200,000,000 operations per second (grader scales to
700MHz)
• dividing doubles, calling methods, casting classes, etc.
o Estimate how many operations your solution will do
o C/C++ and Pascal are often faster than Java
• Memory is not that important
• Do not waste time making premature optimizations
in speed
o Often okay to sacrifice a little efficiency for ease of coding
o Only make optimizations that can increase speed by an order of
magnitude
STRATEGY
• Practice
o Preferably before the contest
• Read each problem:
o Read the problem again
o Read the time / memory constraints
o Read the sample input / output
• Think logically. Notice small details. Be flexible and
creative.
• So you think you have a solution?
o
o
o
o
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
o 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"):
" abcd dcba\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.