A chance to “improve” your C/C++ …

1 / 9

# A chance to “improve” your C/C++ … - PowerPoint PPT Presentation

Two ACM programming skills. A chance to “improve” your C/C++ …. Preparation for the ACM competition. Problem Insight and Execution. 2. 1. Anxiety!. Get into the minds of the judges. Get into the minds of the judges. Key Skill #1: mindreading. 100%. 0%.

I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.

## PowerPoint Slideshow about 'A chance to “improve” your C/C++ …' - arin

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Two ACM programming skills

A chance to “improve” your C/C++ …

Preparation for the ACM competition ...

Problem Insight and Execution ...

2

1

Anxiety!

Get into the minds of the judges

Get into the minds of the judges

100%

0%

“What cases should I handle?” spectrum

Dynamic Programming

Strategy: create a table of partial results & build on it.

divis.cc

T(n) = number of steps yet to go

T(n) = T(3n+1) + 1 if n odd

T(n) = T(n/2) + 1 if n even

Dynamic Programming

Keys: create a table of partial results, articulate what each table cell means, then build it up...

divis.cc

j = items considered so far

Table T

0

1

2

3

4

5

6

1 1 6 2 -3

0

the list

1

4

i = possible remainder

the divisor

2

3

T[i][j] is 1 if i is a possible remainder using the first j items in the list.

Dynamic programs can be short

#include <cstdio>

#include <iostream>

#include <vector>

vector<int> v(10000);

vector<bool> m(100); // old mods

vector<bool> m2(100); // new mods

int n, k;

bool divisible()

{

fill(m.begin(),m.end(),false);

m[0] = true;

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

/* not giving away all of the code */

/* here the table is built (6 lines) */

}

return m[0];

}

int main()

{

cin >> n; // garbage

while (cin >> n) {

cin >> k;

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

cin >> v[i];

v[i] = abs(v[i]);

v[i] %= k;

}

cout << (divisible() ? "D" : "Not d")

<< "ivisible\n";

}

cout << endl;

}

acknowledgment: Matt Brubeck

STL: http://www.sgi.com/Technology/STL

General ACM Programming

Try brute force first (or at least consider it)

-- sometimes it will work fine…

-- sometimes it will take a _bit_ too long

-- sometimes it will take _way_ too long

Best bugs from last week:

getting the input in the “pea” problem:

filling in the table in the “divis” problem:

for (int j=1 ; j<N ; ++j)

{

cin >> Array[i];

}

Table[i + n % k] = 1;

Table[i - n % k] = 1;

New Problem

Word Chains

hertz

jazz

hajj

zeroth

doze

aplomb

ceded

dozen

envy

ballistic

yearn

Input A list of words

Output yes or no -- can these words be chained together such that the last letter of one is the first letter of the next… ?

Knapsack Problem

objectwt.val.

1 3 8

2 2 5

3 1 1

4 2 5

Maximize loot w/ weight limit of 4.

w

Weight available for use

Number of objects considered

0

1

2

3

4

1

V(n,w) = max value stealable w/ ‘n’ objects & ‘w’ weight

2

3

4

n

V(n,w) =