0% found this document useful (0 votes)
201 views

Advanced Programming Spring Week1

The document provides an introduction to competitive programming. It defines competitive programming as solving 3-12 algorithmic problems within a time limit of 1-5 hours. Competitions are held online and at events like the UKIEPC and ICPC. The document recommends resources for learning competitive programming and explains why it is useful for practice in problem solving, coding, and preparing for coding interviews. It provides tips on getting started, including using online judges for practice and competing in online contests.

Uploaded by

Karan Deep Batra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
201 views

Advanced Programming Spring Week1

The document provides an introduction to competitive programming. It defines competitive programming as solving 3-12 algorithmic problems within a time limit of 1-5 hours. Competitions are held online and at events like the UKIEPC and ICPC. The document recommends resources for learning competitive programming and explains why it is useful for practice in problem solving, coding, and preparing for coding interviews. It provides tips on getting started, including using online judges for practice and competing in online contests.

Uploaded by

Karan Deep Batra
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 34

Competitive Programming

An Introduction
Paul Grigoras
[email protected]

About Me

First Attempt

Learning Outcome
Solve interesting problems
See
new tools
new languages
cool tricks

$whatis Competitive Programming

solve & implement 3 - 12 algorithmic problems


1 - 5 hours
individual or teams of 2 - 3
limited run-time ~1s
limited memory ~large enough
100% correct (no edge case bugs)

$whereis Competitive Programming

UKIEPC http://www.cs.nott.ac.uk/~mlw/ukiepc/2013/
ICPC Regionals - Northwestern Europe
ICPC World Finals
Online:
codeforces
topcoder

Competitive Programming@Imperial
Winter Contest @ Imperial
1st February - Sign Up NOW!

Workshops with past ICPC contestants


starting 29th of January
Christian Ledig [email protected]
practice: www.doc.ic.ac.uk/icpc

Why - The Dreaded Coding Interview

Why - Fun

Why - Really Useful


Great Practice

Problem Solving
Complexity Analysis
Coding
Use APIs

For 2nd, 3rd, 4th year courses

How - Online Judges

amazing problem archive - UVA


neatly structured - uhunt
instant feedback
great for practice

How - Online Contests


real-time contests (up to 1-2k contestants):
Codeforces (almost weekly)
TopCoder (Prestigious - TopCoder Open)

usually two divisions (pros and amateurs:) )


some are really prestigious:
Facebook Hacker Cup
Google Code Jam

How - Books
Competitive Programming, Steven Halim
Introduction to Algorithms, CLRS
The Algorithm Design Manual, Skiena

Who
Mainly Beginners
no competitive programming experience

But also
people that want coding interview-like practice
people that need a refresher before going back to
competitive programming

Overview
Implementation

This Week...

Complete Search

Week 2

Divide and Conquer

Week 3

Greedy, Dynamic Programming

Week 4

Graphs

Weeks 5 & 6

Maths

Week 7

Geometry

Week 8

Languages
Haskell - cool, but
rarely available on OJs
Java - not so cool, but
available (well use it)
C++ - even better (but
you dont know it.. yet)

1 Implementation

Minesweeper

Minesweeper UVA 10189

Problem statement
on UVA

Count how many mines are adjacent to each


tile of an N X M minesweeper map.
4 4
*...
....
.*..
....

*100
2210
1*10
1110

Minesweeper - Solution

Analyze the Problem


Problem statement:
1. What is it asking
a. try examples on paper

2. What is the I/O format


a. this often tricks people and is really frustrating

3. What are the time/memory constraints


4. What are the input constraints

Minesweeper - Solution
Pretty easy:
1. Keep a count matrix
2. Loop through the board
if we find a mine (*) at i, j
i. increment count for all non-mine adjacent fields

3. Print the count matrix

Minesweeper - Solution
Think before you write any code!
Is your solution correct?
Try it on the sample input and a few examples
Are there any EDGE CASES?
ICPC is all or nothing - one tiny edge case can
cost you the whole problem
For minesweeper - edges of the board

Minesweeper - Solution
Think before you write any code!
Is your solution fast enough?

Minesweeper - Solution
Think before you write any code!
Is your solution fast enough?
N

Complexity

Complexity

10

O(n!), O(n^6)

100

O(n^4)

15

O(2^n * n^2)

1000

O(n^2)

20

O(2^n), O(n^5)

100K

O(nlogn)

50

O(n^4)

1M

O(n), O(logn), O(1)

Minesweeper - Solution
Think before you write any code!
How long will it take to write it?
Teammates want to know that.
Essential for team contests:
can prioritize actions/problems based on estimates
quick problems first
debugging vs implementation

Minesweeper - Solution
Think before you write any code!
How long will it take to write it?
Teammates want to know that.
Essential for team contests:
can prioritize actions/problems based on estimates
ICPC ranking: first by score, then by penalty

penalty = time submitted + wrong_submissions * 20

Minesweeper - Solution Revisited


Pretty easy:
1. Keep a count matrix
2. Loop through the board

Correct? Yes
Fast? O(n^2)
Yes
if we find a mine (*) at i, j
Time? ~10 mins
increment count for all non-mine Lets code it!
adjacent fields

3. Print the count matrix

Java I/O
Scanner - useful functions, slower
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int n = sc.nextInt();
// OR
// double d = sc.nextDouble();
// BigInteger bi = sc.nextBigInteger();
}

Java I/O
BufferedReader - faster, no parsing
functions
BufferedReader br =
new BufferedReader(new InputStreamReader(System.in));
String line;
while ( (line = br.readLine()) != null) {
int n = Integer.parseInt(line);
}
// useful function for parsing: line.split(delimiter)

Minesweeper - Solution
for (int i = 0; i < n; i++) {
char[] line = sc.nextLine().toCharArray();
for (int j = 0; j < m; j++) {
if ( line[j] != '*' ) continue;
for (int l = i - 1; l <= i + 1

&& l < n; l++)

for (int c = j - 1; c <= j + 1 && c < m; c++)


if (l >= 0 && c >= 0 && count[l][c] != -1)
count[l][c]++;
count[i][j] = -1;
}
}

Minesweeper - Testing
Write tests for:
1. Any edge case
2. Very small inputs
3. Very large inputs, catch:
a. Array out of bounds
b. Overflow

4. Repeated test - makes sure you clear any


intermediary data between test cases

Minesweeper - Testing
Generating large test cases - Python
import sys, random
MAX=100
print '{} {}'.format(MAX, MAX)
for i in range(MAX):
for j in range(MAX):
sys.stdout.write('.' if random.randint(0, 1) == 0 else '*')
print ''
print '0 0'

You might also like