Two ACM programming skills A chance to improve

  • Slides: 9
Download presentation
Two ACM programming skills A chance to “improve” your C/C++ … Preparation for the

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

Key Skill #1: mindreading Get into the minds of the judges 100% 0% “What

Key Skill #1: mindreading Get into the minds of the judges 100% 0% “What cases should I handle? ” spectrum

Key Skill #2: Anxiety! anxiety

Key Skill #2: Anxiety! anxiety

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

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(3 n+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

Dynamic Programming Keys: create a table of partial results, articulate what each table cell means, then build it up. . . i = possible remainder Table T 0 divis. cc j = items considered so far 0 1 2 3 4 5 6 1 1 6 2 -3 the list 1 2 4 the divisor 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>

Dynamic programs can be short #include <cstdio> #include <iostream> #include <vector> vector<int> v(10000); vector<bool> m(100); // old mods vector<bool> m 2(100); // new mods int n, k; int main() { cin >> n; while (cin >> n) { cin >> k; bool divisible() { fill(m. begin(), m. end(), false); m[0] = true; for (int i=0; i<n; i++) { cin >> v[i]; v[i] = abs(v[i]); v[i] %= k; } for (int i=0; i<n; i++) { cout << (divisible() ? "D" : "Not d") << "ivisiblen"; /* not giving away all of the code */ /* here the table is built (6 lines) */ } return m[0]; } // garbage } 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

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: for (int j=1 ; j<N ; ++j) { cin >> Array[i]; } filling in the table in the “divis” problem: Table[i + n % k] = 1; Table[i - n % k] = 1;

New Problem Word Chains Input A list of words hertz doze jazz aplomb hajj

New Problem Word Chains Input A list of words hertz doze jazz aplomb hajj ceded zeroth dozen envy ballistic yearn 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 Maximize loot w/ weight limit of 4. Number of objects considered 1

Knapsack Problem Maximize loot w/ weight limit of 4. Number of objects considered 1 2 3 n 4 V(n, w) = 1 2 wt. val. 3 8 2 5 1 1 2 5 w Weight available for use 0 object 1 2 3 4 V(n, w) = max value stealable w/ ‘n’ objects & ‘w’ weight