90 likes | 196 Views
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%.
E N D
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% “What cases should I handle?” spectrum
Key Skill #2: anxiety Anxiety!
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) =