Introduction to Competitive Programming

Mattox Beckman

Introduction and Logistics

In which we discuss how the course will work…

Welcome to CS 491 CAP!

Your Objectives:

  • Describe the goals and prerequisites of this course.
  • Describe the grading scheme.
  • Be able to practice effectively.

Course Goals

Why take this course?

  • Primary course goal: make you good at competitive programming!
  • Why should you want to do that?
    • It’s fun!
    • Opportunity to learn:
      • useful data structures, algorithms, and mathematical insights;
      • practical applications of data structures and algorithms;
      • how to code and debug effectively; and
      • how to work well on a team.
    • You’ll do really well on job interviews!
  • But what if I’m not as good as those others?

Am I ready for this?

Do I Need CS 225 or 374?

  • No! They help, but it’s more important to know how to use the algorithms than to implement them.
  • Skills Needed
    • Familiarity with C, C++, or Java (CS 125)
    • Willing to learn basic data structures (CS 225).
    • Comfortable with recursion and algorithmic explanations (CS 173).
    • Most important: eagerness to learn and practice!!
  • Textbooks
    • Competitive Programming 4 by Steven and Felix
    • Optional: Guide to Competitive Programming by Antti Laaksonen

Lecture Format

  • Each period will have the following workflow:
    • Lecture Video
      • A short lecture video or two will introduce the topic.
      • Meant as a big-picture introduction, not to teach the algorithm.
      • You get to teach yourself by studying the implementation!
    • Sample Problem(s)
      • One “easy” sample problem to do before class
        • Class will begin with a short discussion of the problems.
      • A more interesting problem to do during class.
        • You can work in groups of up to three, but everyone must turn it in themselves!
        • Hints given the last 10 minutes of class

Assignments

  • Problem Sets You will also get a biweekly problem set.
    • Typical format: 10 problems. You must solve at least 6 of them.
    • Submit all problems to the corresponding online judge.
    • The in-class problems together count as two problem sets for grading.
  • Contests You can also participate in some contests.
    • A 5 hour contest will replace one problem set.

NB: Please do not copy-paste code from other sources. You are only hurting yourself if you do!

Grading

Course is Pass/Fail: Passing is 70%.

  • Attendance: 20%.
    • Measured by submission of ``pre-class’’ practice problems.
    • You get four ``excused absences’’ for attendance and participation.
  • Completion of problems: 70%.
  • Team Reference Document is worth 10%.
    • Due at the end of the semester, but you will want to start working on it.

Extra Credit

There are opportunities for extra credit here too!

  • Attending a tryout counts as one contest or problem set.
  • You can get points by contributing new problems to our problem sets.

ICPC, IPL, and Contests

You have many opportunities to get involved!

Programming Contests

  • UIUC ICPC tryouts and practice
    • The NAQ is this Saturday!
  • ACM ICPC
    • Regional contest will be Saturday, March 5, 2022.
    • North American Championship will probably be around November.
    • World Finals…?
  • Online contests
    • TopCoder SRMs, CodeForces
    • Facebook Hacker Cup
    • Google Code Jam
    • TopCoder Open
    • … and many others …

Online Judges

Online Contests

UIUC ICPC Team Meetings

Approach to Solving ICPC Problems

  1. Read the problem statement carefully!
    • Pay attention to the input/output format specification.
  2. Abstract the problem.
  3. Design an algorithm.
  4. Implement and debug.
  5. Submit.
  6. AC!
    • (else GO TO 4… or maybe even 3)
  7. If you want to improve rapidly:
    • Read the problem commentary afterwards.
    • After a contest, ``upsolve’’ any problems you couldn’t finish.

Example Problem

  • POJ 1000: A + B Problem
    • Input: two space separated integers, \(a\) and \(b\).
    • Constraints: \(0 \le a, b \le 10\).
    • Output: \(a + b\)

C Code for POJ 1000

    #include <stdio.h>

    int main() {
      int a, b;

      scanf("%d %d", &a, &b);
      printf("%d\n", a + b);
      return 0;
    }

C++ Code for POJ 1000

    #include <bits/stdc++.h>

    using namespace std;

    int main() {
      int a, b;

      cin >> a >> b;
      cout << a+b << endl;
    }

Java Code for POJ 1000

    import java.io.*;
    import java.util.*;

    public class Main {
      public static void main(String args[])
      throws Exception{
        Scanner cin=new Scanner(System.in);
        int a=cin.nextInt(), b=cin.nextInt();
        System.out.println(a+b);
      }
    }

Example Problem

  • POJ 1004 — Financial Management
    • Input: 12 floating-point numbers, each on a separate line
    • Output: Average of the numbers, rounded to two decimal places
    • Note that the answer must be preceeded by a dollar sign (\$)!

C Code for POJ 1004

    #include<stdio.h>

    int main() {
      double sum = 0, buf;
      for(int i = 0; i < 12; i++) {
        scanf("%f", &buf);
        sum += buf;
      }
      printf("$%.2f\n", sum / 12.0);
      return 0;
    }

C Code for POJ 1004

    #include <bits/stdc++.h>

    using namespace std;

    int main() {
      double sum = 0, buf;
      for(int i = 0; i < 12; i++) {
        cin >> buf;
        sum += buf;
      }
      printf("$%.2f\n", sum / 12.0);
      return 0;
    }

Java Code for POJ 1004

    import java.util.*;

    class Main {
      public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        double d = 0;
        for (int i = 0; i < 12; ++i) {
          d += in.nextDouble();
        }
        System.out.printf("$%.2f\n", d/12.0);
      }
    }

Course Resources

Questions?