|
1 |
| -// BEGIN CUT HERE |
2 |
| - |
3 |
| -// END CUT HERE |
4 |
| -#include <iostream> |
5 |
| -#include <sstream> |
6 |
| -#include <string> |
7 |
| -#include <vector> |
8 |
| -using namespace std; |
9 |
| - |
10 |
| - class QuickSums { |
11 |
| - public: |
12 |
| - int minSums(string numbers, int sum) { |
13 |
| - int n = numbers.size(); |
14 |
| - int a0 = (numbers[0]-'0'); |
15 |
| - if(n == 1){ |
16 |
| - if(sum == a0) |
17 |
| - return 0; |
18 |
| - else return -1; |
19 |
| - } |
20 |
| - int a1 = (numbers[1]-'0'); |
21 |
| - if(n == 2){ |
22 |
| - if(sum == 10*a0+a1) |
23 |
| - return 0; |
24 |
| - if(sum == a0+a1) |
25 |
| - return 1; |
26 |
| - return -1; |
27 |
| - } |
28 |
| - string num1 = numbers.substr(2), num2 = numbers.substr(1); |
29 |
| - int sum1 = 10*a0+a1, sum2 = a0; |
30 |
| - int rsum1 = minSums(num1, sum-sum1), rsum2 = minSums(num2, sum-sum2); |
31 |
| - if(sum1 != 0 && rsum1 != -1) rsum1++; |
32 |
| - if(sum2 != 0 && rsum2 != -1) rsum2++; |
33 |
| - if(rsum1 != -1 && rsum2 != -1) |
34 |
| - return min(rsum1, rsum2); |
35 |
| - if(rsum1 != -1) |
36 |
| - return rsum1; |
37 |
| - if(rsum2 != -1) |
38 |
| - return rsum2; |
39 |
| - return -1; |
40 |
| - } |
41 |
| - |
42 |
| -// BEGIN CUT HERE |
43 |
| - public: |
44 |
| - void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } |
45 |
| - private: |
46 |
| - template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
47 |
| - void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
48 |
| - void test_case_0() { string Arg0 = "99999"; int Arg1 = 45; int Arg2 = 4; verify_case(0, Arg2, minSums(Arg0, Arg1)); } |
49 |
| - void test_case_1() { string Arg0 = "1110"; int Arg1 = 3; int Arg2 = 3; verify_case(1, Arg2, minSums(Arg0, Arg1)); } |
50 |
| - void test_case_2() { string Arg0 = "0123456789"; int Arg1 = 45; int Arg2 = 8; verify_case(2, Arg2, minSums(Arg0, Arg1)); } |
51 |
| - void test_case_3() { string Arg0 = "99999"; int Arg1 = 100; int Arg2 = -1; verify_case(3, Arg2, minSums(Arg0, Arg1)); } |
52 |
| - void test_case_4() { string Arg0 = "382834"; int Arg1 = 100; int Arg2 = 2; verify_case(4, Arg2, minSums(Arg0, Arg1)); } |
53 |
| - void test_case_5() { string Arg0 = "9230560001"; int Arg1 = 71; int Arg2 = 4; verify_case(5, Arg2, minSums(Arg0, Arg1)); } |
54 |
| - |
55 |
| -// END CUT HERE |
56 |
| - |
57 |
| - }; |
58 |
| - |
59 |
| - // BEGIN CUT HERE |
60 |
| -int main(){ |
61 |
| - QuickSums ___test; |
62 |
| - ___test.run_test(-1); |
63 |
| -} |
64 |
| - // END CUT HERE |
| 1 | +#include <iostream> |
| 2 | +#include <sstream> |
| 3 | +#include <vector> |
| 4 | +#include <algorithm> |
| 5 | +#include <set> |
| 6 | +#include <map> |
| 7 | +#include <queue> |
| 8 | +#include <cstring> |
| 9 | +#include <climits> |
| 10 | +#include <cstdio> |
| 11 | +using namespace std; |
| 12 | +typedef pair<int,int> pi; |
| 13 | +typedef set<int> si; |
| 14 | +typedef vector<int> vi; |
| 15 | +typedef vector<vi> vvi; |
| 16 | +typedef vector<string> vs; |
| 17 | +int num[15]; |
| 18 | +int dp[15][15][105]; |
| 19 | +int go(int i, int j, int sum){ |
| 20 | + if(dp[i][j][sum] != -1) return dp[i][j][sum]; |
| 21 | + int mina = 1000000, s = 0; |
| 22 | + for(int k = i; k <= j; k++) s = (10*s + num[k]); |
| 23 | + if(s == sum) return 0; |
| 24 | + |
| 25 | + for(int t = 0; t <= sum; t++){ |
| 26 | + for(int k = i; k < j; k++){ |
| 27 | + int a = go(i, k, t) + go(k+1, j, sum-t) + 1; |
| 28 | + mina = min(mina, a); |
| 29 | + } |
| 30 | + } |
| 31 | + dp[i][j][sum] = mina; |
| 32 | + return dp[i][j][sum]; |
| 33 | +} |
| 34 | + |
| 35 | +class QuickSums{ |
| 36 | + public: |
| 37 | + int minSums(string numbers, int sum) { |
| 38 | + for(int i = 0; i <= 10; i++) |
| 39 | + for(int j = 0; j <= 10; j++) |
| 40 | + for(int k = 0; k <= 100; k++) |
| 41 | + dp[i][j][k] = -1; |
| 42 | + int n = numbers.size(); |
| 43 | + for(int i = 0; i < n; i++) num[i] = (numbers[i]-'0'); |
| 44 | + int val = go(0, n-1, sum); |
| 45 | + if(val == 1000000) return -1; |
| 46 | + return val; |
| 47 | + } |
| 48 | + |
| 49 | +// BEGIN CUT HERE |
| 50 | + public: |
| 51 | + void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); if ((Case == -1) || (Case == 5)) test_case_5(); } |
| 52 | + private: |
| 53 | + template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '\"' << *iter << "\","; os << " }"; return os.str(); } |
| 54 | + void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } |
| 55 | + void test_case_0() { string Arg0 = "99999"; int Arg1 = 45; int Arg2 = 4; verify_case(0, Arg2, minSums(Arg0, Arg1)); } |
| 56 | + void test_case_1() { string Arg0 = "1110"; int Arg1 = 3; int Arg2 = 3; verify_case(1, Arg2, minSums(Arg0, Arg1)); } |
| 57 | + void test_case_2() { string Arg0 = "0123456789"; int Arg1 = 45; int Arg2 = 8; verify_case(2, Arg2, minSums(Arg0, Arg1)); } |
| 58 | + void test_case_3() { string Arg0 = "99999"; int Arg1 = 100; int Arg2 = -1; verify_case(3, Arg2, minSums(Arg0, Arg1)); } |
| 59 | + void test_case_4() { string Arg0 = "382834"; int Arg1 = 100; int Arg2 = 2; verify_case(4, Arg2, minSums(Arg0, Arg1)); } |
| 60 | + void test_case_5() { string Arg0 = "9230560001"; int Arg1 = 71; int Arg2 = 4; verify_case(5, Arg2, minSums(Arg0, Arg1)); } |
| 61 | + |
| 62 | +// END CUT HERE |
| 63 | + |
| 64 | +}; |
| 65 | + |
| 66 | +// BEGIN CUT HERE |
| 67 | +int main(){ |
| 68 | + |
| 69 | + QuickSums ___test; |
| 70 | + ___test.run_test(-1); |
| 71 | +} |
| 72 | +// END CUT HERE |
0 commit comments