第三次出现二分法了,崩溃,还是当时没有想到,伤不起啊
第一个很简单,解方程的题。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <queue> 12 using namespace std; 13 14 class PlatypusDuckAndBeaver { 15 public: 16 int minimumAnimals(int webbedFeet, int duckBills, int beaverTails) { 17 int duck = 0; 18 duck = (webbedFeet - 4 * beaverTails) / 2; 19 int yzs = 0; 20 yzs = duckBills - duck; 21 int hl = 0; 22 hl = beaverTails - yzs; 23 int sum = 0; 24 sum = duck + yzs + hl; 25 return sum; 26 27 } 28 };
第二个就是二分查找,注意一下边界条件就好了,比赛的时候闪过一个二分法的念头,不过还是没想到。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <queue> 12 #include <stack> 13 using namespace std; 14 typedef long long ll; 15 class Suminator { 16 public: 17 ll judge(vector<int> num) { 18 stack<ll> mystack; 19 for (ll i = 0; i < 150; i++) { 20 mystack.push(0); 21 } 22 for (ll i = 0; i < num.size(); i++) { 23 if (num[i] == 0) { 24 ll x = mystack.top(); 25 mystack.pop(); 26 ll y = mystack.top(); 27 mystack.pop(); 28 ll tmp = x + y; 29 mystack.push(tmp); 30 } else { 31 mystack.push(num[i]); 32 } 33 } 34 ll res = mystack.top(); 35 return res; 36 } 37 int findMissing(vector<int> program, int wantedResult) { 38 ll mys_size = program.size(); 39 ll pos; 40 ll res, res2; 41 for (ll i = 0; i < mys_size; i++) { 42 if (program[i] == -1) { 43 program[i] = 0; 44 pos = i; 45 } 46 } 47 program[pos] = 0; 48 res = judge(program); 49 if (res == wantedResult) 50 return 0; 51 program[pos] = 1; 52 res = judge(program); 53 program[pos] = 1000000000; 54 res2 = judge(program); 55 56 if (res != res2) { 57 if (res == wantedResult) 58 return 1; 59 else if (res > wantedResult) { 60 return -1; 61 } 62 63 if (res2 == wantedResult) 64 return 1000000000; 65 else if (res2 < wantedResult) { 66 return -1; 67 } 68 69 ll left = 0; 70 ll right = 1000000000; 71 ll middle; 72 while (left < right) { 73 middle = (left + right) / 2; 74 program[pos] = middle; 75 ll tmpres = judge(program); 76 if (tmpres < wantedResult) 77 left = middle + 1; 78 else { 79 right = middle; 80 } 81 } 82 return int(left); 83 } else { 84 return -1; 85 } 86 87 } 88 };
第三题就是动态规划的题,递归式搞出来就容易多了,然后在可行方案中计算就可以了。
1 #include <iostream> 2 #include <string> 3 #include <vector> 4 #include <cstdlib> 5 #include <cmath> 6 #include <map> 7 #include <algorithm> 8 #include <list> 9 #include <ctime> 10 #include <set> 11 #include <queue> 12 #define FOR(i,n) for(i=0;i<n;i++) 13 using namespace std; 14 int judge[51][51][51][51]; 15 int num[4]; 16 class SafeRemoval { 17 public: 18 void cal(int zero, int one, int two, int three, int used, int des, 19 int remain) { 20 if (judge[zero][one][two][three] != 0) 21 return; 22 if (used == des) { 23 judge[zero][one][two][three] = 1; 24 return; 25 } 26 27 if (remain % 4 == 1) { 28 if (zero > 0) { 29 cal(zero - 1, one, two, three, used + 1, des, remain); 30 } 31 if (two > 0) { 32 cal(zero, one, two - 1, three, used + 1, des, 33 ((remain + 2) % 4)); 34 } 35 if (three > 0) { 36 cal(zero, one, two, three - 1, used + 1, des, 37 ((remain + 1 ) % 4)); 38 } 39 judge[zero][one][two][three] = 2; 40 } 41 if (remain % 4 == 2) { 42 if (zero > 0) { 43 cal(zero - 1, one, two, three, used + 1, des, remain); 44 } 45 if (one > 0) { 46 cal(zero, one - 1, two, three, used + 1, des, 47 ((remain +3) % 4)); 48 } 49 if (three > 0) { 50 cal(zero, one, two, three - 1, used + 1, des, 51 ((remain +1) % 4)); 52 } 53 judge[zero][one][two][three] = 2; 54 } 55 if (remain % 4 == 3) { 56 if (zero > 0) { 57 cal(zero - 1, one, two, three, used + 1, des, remain); 58 } 59 if (one > 0) { 60 cal(zero, one - 1, two, three, used + 1, des, 61 ((remain +3) % 4)); 62 } 63 if (two > 0) { 64 cal(zero, one, two - 1, three, used + 1, des, 65 ((remain + 2) % 4)); 66 } 67 judge[zero][one][two][three] = 2; 68 } 69 } 70 int removeThem(vector<int> seq, int k) { 71 int i, j, l, m; 72 vector<vector<int> > x(4, vector<int>(0, 0)); 73 FOR(i,51) 74 FOR(j,51) 75 FOR(l,51) 76 FOR(m,51) 77 judge[i][j][l][m] = 0; 78 FOR(i,4) 79 num[i] = 0; 80 int remain = 0; 81 for (i = 0; i < seq.size(); i++) { 82 remain = (remain + seq[i]) % 4; 83 x[(seq[i] % 4)].push_back(seq[i]); 84 num[seq[i] % 4]++; 85 } 86 for (i = 0; i < 4; i++) { 87 sort(x[i].begin(), x[i].end()); 88 } 89 for (i = 0; i < 4; i++) { 90 for (j = x[i].size() - 2; j >= 0; j--) { 91 x[i][j] = x[i][j] + x[i][j + 1]; 92 } 93 } 94 for (i = 0; i < 4; i++) { 95 for (j = 0; j < x[i].size(); j++) { 96 cout << x[i][j]<<" "; 97 } 98 cout << endl; 99 } 100 101 102 cal(num[0], num[1], num[2], num[3], 0, k, remain); 103 int res = -1; 104 int i1, j1, l1, m1; 105 i1 = x[0].size(); 106 j1 = x[1].size(); 107 l1 = x[2].size(); 108 m1 = x[3].size(); 109 110 FOR(i,51) 111 FOR(j,51) 112 FOR(l,51) 113 FOR(m,51) { 114 if (judge[i][j][l][m] == 1) { 115 cout << i << " " << j << " " << l << " " << m 116 << " "; 117 int tmp = 0; 118 if (i > 0) { 119 tmp += x[0][i1 - i]; 120 } 121 if (j > 0) { 122 tmp += x[1][j1 - j]; 123 } 124 if (l > 0) { 125 tmp += x[2][l1 - l]; 126 } 127 if (m > 0) { 128 tmp += x[3][m1 - m]; 129 } 130 131 res = max(res, tmp); 132 } 133 } 134 return res; 135 } 136 };