CPSC 490 Problem Solving in Computer Science Introduction Lucca - - PowerPoint PPT Presentation

cpsc 490 problem solving in computer science
SMART_READER_LITE
LIVE PREVIEW

CPSC 490 Problem Solving in Computer Science Introduction Lucca - - PowerPoint PPT Presentation

CPSC 490 Problem Solving in Computer Science Introduction Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/07 University of British Columbia Coordinators: Lucca Siaudzionis ICPC World Finalist in 2018 and 2017 Interned twice at


slide-1
SLIDE 1

CPSC 490 Problem Solving in Computer Science

Introduction

Lucca Siaudzionis and Jack Spalding-Jamieson 2020/01/07

University of British Columbia

slide-2
SLIDE 2

Coordinators: Lucca Siaudzionis

  • ICPC World Finalist in 2018

and 2017

  • Interned twice at Airbnb

and once at Google

  • Two-time Silver medallist in

NAIPC (2018 and 2017)

  • IOI Silver (2016) and

Bronze (2015) Medallist

  • Wesbrook Scholar

1

slide-3
SLIDE 3

Coordinators: Jack Spalding-Jamieson

  • Published algorithms

researcher

  • CRA Outstanding

Undergraduate Researcher Honourable Mention

  • ICPC regional placements
  • f 1st (Div. 2), 40th, 10th
  • Honourable mention in

NAIPC (2018)

  • Interned at Jane Street

2

slide-4
SLIDE 4

Why You Should Take This Course

  • Learn useful algorithms and data structures not taught in any other

course in UBC

  • Implement those algorithms and see them working
  • Practice problem solving, coding, and debugging
  • Rock at programming competitions and coding interviews

3

slide-5
SLIDE 5

Why You Should NOT Take This Course

  • Credits from CPSC 490 no longer satisfy the CPSC 4xx requirement

for graduation (with exception of BA students)

  • Please check with the Computer Science department and/or your

faculty to determine if this course can satisfy requirements in other programs.

  • If you think this is the only path to get a job

4

slide-6
SLIDE 6

Prerequisites

  • Knowledge of algorithms (especially from CPSC 320) will help you a

lot, but it is not required

  • Being comfortable with Big-O analysis
  • Assignments may require writing code on C, C++, or Java. Python

will be fine for most assignments, and extra languages may be provided on request

5

slide-7
SLIDE 7

Course Information

Course website: students.cs.ubc.ca/~cs-490/2019W2/problem-solving

  • Proposal and syllabus
  • Lecture slides
  • Assignments
  • Presentation topics
  • Useful readings and links

Piazza:

https://piazza.com/ubc.ca/winterterm22019/cpsc490202204/home

Access code: algorithms How to get help: after class or Piazza

6

slide-8
SLIDE 8

Grading Scheme

  • Assignment 1: 15%
  • Assignment 2: 15%
  • Assignment 3: 15%
  • Assignment 4: 15%
  • Assignment 5: 15%
  • Written Report: 15%
  • Presentation: 10%

No exams!

7

slide-9
SLIDE 9

Assignments

There will be 5 assignments, and they will have similar structure:

  • Each assignment will be a problem set
  • You have to write code to solve each problem
  • Our online judge will provide near-instant feedback on your solution
  • You can attempt each problem multiple times until the assignment

deadline

  • There will be no partial marks for each problem

8

slide-10
SLIDE 10

Assignment Feedback

Every time you attempt a solution, the judge will respond with one of the following:

  • Accepted: Congrats! You solved the problem
  • Wrong Answer: At least one of your outputs was incorrect
  • Time Limit Exceeded: Your solution is too slow
  • Memory Limit Exceeded: Your solution is consuming too much

memory (recursing too much can lead to this)

  • Runtime Error: Can be a variety of errors, and most likely is a

segmentation fault

9

slide-11
SLIDE 11

Written Report

You will be required to write one detailed report this term:

  • You will pick one algorithm or data structure that really interests you
  • Each of you will have to pick a different topic
  • The report will be similar to a paper on it (but not as formal)
  • More details on this will be posted on the course website

10

slide-12
SLIDE 12

Presentation

You will give one presentation to all of your classmates

  • The presentation will be on the same topic you chose for your

written report

  • It will last around 12 minutes (plus questions)
  • The presentations will be after the due date of the report

11

slide-13
SLIDE 13

Academic Policy

Using other people’s code

  • Try to code everything yourself
  • Make sure you understand everything your code does
  • Don’t blindly copy from the internet or other students. We will run

plagiarism detectors Collaboration

  • Discuss ideas and approaches with your classmates!
  • Don’t, however, share code with anyone

Citations

  • Cite all sources other than: lectures, slides, UBC/Stanford Code

Archives

  • Cite everyone you collaborated with

12

slide-14
SLIDE 14

Input/Output (IO)

Almost every problem in this course will use Standard Input/Output

13

slide-15
SLIDE 15

Standard IO

C++

  • cin/cout: easy to use but slow
  • De-sync with scanf/printf to get faster performance:

ios base::sync with stdio(0); cin.tie(0);

  • Don’t flush buffer: cout << ’\n’ is faster than cout << endl
  • scanf/printf: faster than cin/cout

Java

  • Scanner: versatile but slow
  • has nextBigInteger(), nextLine(), etc.
  • BufferedReader/Writer: fast, but have to parse manually

Python

  • raw input/print: easy to use but slow
  • stdin/stdout: fast, slightly annoying to use

14

slide-16
SLIDE 16

Using cin/cout in C++

1

#include <iomanip>

2

#include <iostream>

3

using namespace std;

4

int main() {

5

// un-sync with scanf/printf for fast performance

6

ios_base::sync_with_stdio(0); cin.tie(0);

7 8

int a; long long b; double c; string s;

9

cin >> a >> b >> c >> s;

10

cout << setprecision(16) << fixed

11

<< a << " " << b << " " << c << " "

12

<< s << endl;

13

return 0;

14

}

15

slide-17
SLIDE 17

Using scanf/printf in C++

1

#include "stdio.h"

2

int main() {

3

int a; long long b; double c; char s[100];

4

scanf("%d %lld %lf %s", &a, &b, &c, s);

5

printf("%d %lld %.16f %s\n", a, b, c, s);

6

return 0;

7

}

16

slide-18
SLIDE 18

Using Scanner in Java

1 import java.util.*; 2 public class A { 3

public static void main(String[] args) {

4

Scanner sc = new Scanner(System.in);

5 6

int a = sc.nextInt();

7

long b = sc.nextLong();

8

double c = sc.nextDouble();

9

String s = sc.next();

10 11

System.out.println(a + " " + b + " " +

12

String.format("%.16f", c) + " " + s);

13

// OR

14

System.out.printf("%d %d %.16f %s\n", a, b, c, d);

15 16

sc.close();

17

}

18 }

17

slide-19
SLIDE 19

Using BufferedReader in Java

1 import java.io.*; 2 import java.util.*; 3 public class A { 4

public static void main(String[] args) {

5

BufferedReader reader = new BufferedReader(

6

new InputStreamReader(System.in));

7

PrintWriter writer = new PrintWriter(System.out);

8 9

StringTokenizer toks = new StringTokenizer(reader.readLine());

10

int a = Integer.parseInt(toks.nextToken());

11

long b = Long.parseLong(toks.nextToken());

12

double c = Double.parseDouble(toks.nextToken());

13

String s = toks.nextToken();

14 15

writer.println(a + " " + b + " " +

16

String.format("%.16f", c) + " " + s);

17

// OR

18

writer.printf("%d %d %.16f %s\n", a, b, c, d);

19

reader.close(); writer.close();

20

}

21 }

18

slide-20
SLIDE 20

Using raw input() in Python

1

def main():

2

a, b, c, s = raw_input().split()

3

a = int(a)

4

b = int(b)

5

c = float("{0:.16f}".format(float(c)))

6

print("{} {} {} {}".format(a, b, c, s))

7

# OR

8

print(a,b,c,s)

9 10

if __name__ == "__main__":

11

main()

19

slide-21
SLIDE 21

Using stdin/stdout in Python

1

from sys import stdin, stdout

2 3

def main():

4

a, b, c, s = stdin.readline().split()

5

a = int(a)

6

b = int(b)

7

c = float("{0:.16f}".format(float(c)))

8

stdout.write("{} {} {} {}\n".format(a, b, c, s))

9 10

if __name__ == "__main__":

11

main()

20

slide-22
SLIDE 22

Sample Problems

Let’s solve some problems!

21

slide-23
SLIDE 23

Problem 1 - Simple Multiplication

Input: A single integer, n (1 ≤ n ≤ 10), followed by n lines containing each two integers ai and bi (1 ≤ ai, bi ≤ 103). Output: n lines, where the i-th line contains the value of ai × bi. Time Limit: 1 Second.

22

slide-24
SLIDE 24

Problem 2 - [Not So] Simple Multiplication

Input: A single integer, n (1 ≤ n ≤ 1, 000, 000), followed by n lines containing each two integers ai and bi (1 ≤ ai, bi ≤ 107). Output: n lines, where the i-th line contains the value of ai × bi. Time Limit: 1 Second.

23

slide-25
SLIDE 25

Problems 1 & 2 - Sample Solution[?]

1

#include <iostream>

2

using namespace std;

3 4

int main() {

5

int n;

6

cin >> n;

7 8

for (int i = 0; i < n; i++) {

9

int a, b;

10

cin >> a >> b;

11

cout << a * b << endl;

12

}

13 14

return 0;

15

}

24

slide-26
SLIDE 26

Sample Solution - What Could Go Wrong?

  • This will correctly solve Problem 1
  • However, it will fail Problem 2 in two different ways
  • Overflows integers
  • Exceeds time limit

25

slide-27
SLIDE 27

Common Pitfall 1: Overflow

  • In, C++, an int stores values from −231 to 231 − 1 (≈ 2 × 109)
  • Multiplying two ints of up to 107 can result in 1014
  • This overflow will lead to undesired behaviour (thus the wrong

answer!)

  • How to fix this (in C++):
  • Easiest way would be to use long longs

26

slide-28
SLIDE 28

Common Pitfall 2: Time Limit Exceeded

  • Recall that each problem had a limit limit of 1 second
  • This requires reading input + calculating output + printing output

to be in 1 second

  • Using cin/cout and endl can lead to a lot of unwanted time
  • How to fix this (in C++):
  • De-sync with scanf/printf to get faster performance:

ios base::sync with stdio(0); cin.tie(0);

  • Don’t flush buffer: cout << ’\n’ is faster than cout << endl

27

slide-29
SLIDE 29

Problem 3 - Simple Addition

Input: A single integer, n (1 ≤ n ≤ 1, 000), followed by a line containing n space-separated decimals a1, a2, ..., an, where 10−9 ≤ ai ≤ 109. Output: A single line, containing the sum of all ais.

28

slide-30
SLIDE 30

Common Pitfall 3: Floating Point Error

Numerical Errors

  • Cancellation error: 1.00000000001234 - 1.0000000001233
  • Add/subtract big and small numbers: 100000000 + 0.000000001
  • Error amplification: yn = 1/n − 10yn−1
  • Overflow (NaN!), underflow (denormal numbers = slow!)

Round / Floor / Ceil This is surprisingly hard

  • Rounding strategies: up, down, to nearest even
  • 0.1 + 0.1 + 0.1 + 0.1 + 0.1 == 0.5 ??
  • Before rounding up, we should add small epsilon

Comparing floating point numbers

  • This will probably not work: if (a == b) { ...

}

  • This is the better way: if (abs(a-b) < 1e-8) { ...

}

29

slide-31
SLIDE 31

Problem 4 - Revisiting Binary Search

Given an increasing function f on [l, r], what is the smallest x such that f (x) ≥ 0?

30

slide-32
SLIDE 32

Binary search

Evaluate f at a point x0...

31

slide-33
SLIDE 33

Binary search

f (x0) is too big, so cut off the right half! Search space is halved every iteration.

32

slide-34
SLIDE 34

Binary search

Still works for non-decreasing functions, functions defined on Z, etc.

33

slide-35
SLIDE 35

Binary search – continuous

1

def binary_search(l, r, f):

2

while r - l > EPS:

3

mid = (l + r) / 2

4

if f(mid) >= 0:

5

r = mid

6

else:

7

l = mid

8

return l Time complexity: O

  • log

r−l

ǫ

  • 34
slide-36
SLIDE 36

Binary search – discrete

This can be tricky to get right!

1

def binary_search(l, r, f):

2

ans = null

3

while l <= r:

4

mid = (l + r) / 2

5

if f(mid) >= 0:

6

ans = mid

7

r = mid - 1

8

else:

9

l = mid + 1

10

return ans Time complexity: O(log(r − l))

35

slide-37
SLIDE 37

Common Pitfalls 4: Errors with Binary Search

  • Choosing initial search space [l, r]
  • Infinite loop, due to stopping criterion
  • Off-by-one errors
  • Edge cases: f < 0 everywhere, f ≥ 0 everywhere

36

slide-38
SLIDE 38

Very Common Bugs

Input / Output

  • Read input incorrectly (spaces in string, 32 vs. 64-bit)
  • Forget to read all input
  • Forget to print newline

General

  • Forget to reset variables per test case
  • Array index out of bounds
  • Always make array size slightly bigger to avoid this
  • Aliasing: declare local variable with same name as global
  • Custom comparator ill-defined
  • Forget to save file / submit wrong file / select wrong language

37

slide-39
SLIDE 39

Debugging Tips

  • Turn on all compile warnings (lots of code that look like they

shouldn’t compile actually do!)

  • Use print statements, debugger to trace execution
  • Valgrind is very helpful for memory errors
  • Make hard test cases (sample input is often deceptive)
  • Run your code in judge environment

38

slide-40
SLIDE 40

Optimizations

  • Use an asymptotically faster algorithm
  • As a rule of thumb a computer performs roughly 1e8 operations in a

second.

  • For the homework problems try to keep under this bound!
  • Optimize constant factors
  • Memory allocation / reset
  • Copying data structures
  • Complicated data structures: e.g. std::set
  • Cache locality: array vs. hash map
  • Branch misprediction
  • Java: Object allocation, immutable vs. mutable
  • int/float vs. long long/double
  • Bitmasks

39

slide-41
SLIDE 41

Future Problems

What are some harder problems we’ll see throughout the term?

40

slide-42
SLIDE 42

Network flow

How many railroads do we need to destroy to cut off supply?

41

slide-43
SLIDE 43

Dynamic programming

How well do these genes match up?

42

slide-44
SLIDE 44

Data structures

How much paint to cover all the rectangles?

43

slide-45
SLIDE 45

Computational geometry

How many aliens can we kill with one bomb?

44

slide-46
SLIDE 46

How to Practice

  • UBC ACM Practice
  • Every Wednesday 4:30-7:30pm, Saturday 11am-4pm
  • Free food!
  • Online judges (CodeForces, TopCoder, SPOJ, Kattis, UVa, etc.)
  • Google Code Jam, Facebook Hacker Cup (Usually in January)

45

slide-47
SLIDE 47

Dicusssion Problems

Let’s solve some [more] problems!

46

slide-48
SLIDE 48

Discussion Problem 1: Steps

Lucca and Jack are on opposite ends of a staircase with N steps. Lucca is at the bottom, and starts going up. At the t-th second, he is at step L(t) =

  • max(t2 − t, 0)
  • . Jack starts at the top, and heads down. At the

t-th second, he is at step J(t) =

  • N − t2/4
  • .

They want to coordinate an epic high-five, and need your help finding out at which second the of them will be at the same step. Input: A number 0 ≤ N ≤ 1018 denoting the number of steps. Output: The second t at which Lucca and Jack high-five, or −1 if they do not.

47

slide-49
SLIDE 49

Discussion Problem 2: Bagel Bags

Jack and his adopted twin brother Edward each have a bag of bagels. Each bagel is described by an amount of cheese (a non-negative integer), an amount of crunchyness (a non-negative integer), and a number of grains on top (a non-negative integer). They are sitting at a coffee shop about to eat these bagels. While they eat the bagels from their respective bags, they will do so simultaneously, and take the same amount of time to finish. Since Jack and Edward are twins, they will only eat a bagel if the other is eating a bagel with the same amount of cheese, crunchyness, and grains on top. They also want to eat as many bagels as possible. Help Jack and Edward find out how many bagels they will eat. Input: 0 ≤ n ≤ 106 descriptions of Jack’s bagels, and 0 ≤ m ≤ 106 descriptions of Edward’s bagels. Output: The number of bagels that Jack and Edward will eat.

48