USACO 之 Section 2.1 (已解决)
The Castle:
/*
搜索 1A
*/
1 /* 2 ID: Jming 3 PROG: castle 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 55; 32 const int MaxM = 55; 33 34 int castle[MaxN][MaxM]; 35 bool visited[MaxN][MaxM]; 36 int N, M; 37 const int direction[4] = { 1, 2, 4, 8 }; // 西 北 东 南 38 const int step[4][2] = { {0, -1}, {-1, 0}, {0, 1}, {1, 0} }; 39 40 struct Node 41 { 42 // (x, y)代表房间坐标 43 // z代表拆的(x, y)房间哪面墙,z只会代表两种可能N,E 44 int x, y, z; 45 }; 46 47 bool Cmp(const Node &nd1, const Node &nd2) 48 { 49 if (nd1.y == nd2.y) 50 { 51 if (nd1.x == nd2.x) 52 { 53 return (nd1.z < nd2.z); 54 } 55 else 56 { 57 return (nd1.x > nd2.x); 58 } 59 } 60 else 61 { 62 return (nd1.y < nd2.y); 63 } 64 } 65 66 void searchRoom(int x, int y, int &roomSize) 67 { 68 if (!visited[x][y]) 69 { 70 ++roomSize; 71 visited[x][y] = true; 72 for (int i = 0; i < 4; ++i) 73 { 74 if (!(castle[x][y] & direction[i])) 75 { 76 int x1 = x + step[i][0]; 77 int y1 = y + step[i][1]; 78 if ((x1 >= 0) && (x1 < M) && (y1 >= 0) && (y1 < N)) 79 { 80 //cout << x1 << " " << y1 << endl; 81 searchRoom(x1, y1, roomSize); 82 } 83 } 84 } 85 } 86 } 87 88 void removeWall() 89 { 90 vector<Node> vecNd; 91 int mergeRoomSize = 0; 92 for (int x = 0; x < M; ++x) 93 { 94 for (int y = 0; y < N; ++y) 95 { 96 for (int i = 1; i <= 2; ++i) // 只要考虑去拆一个房间的北面、东面墙就好了 97 { 98 if (castle[x][y] & direction[i]) 99 { 100 memset(visited, false, sizeof(visited)); 101 castle[x][y] ^= direction[i]; 102 int roomSize = 0; 103 searchRoom(x, y, roomSize); 104 if (roomSize >= mergeRoomSize) 105 { 106 if (roomSize > mergeRoomSize) 107 { 108 vecNd.clear(); 109 mergeRoomSize = roomSize; 110 } 111 Node nd; 112 nd.x = x; 113 nd.y = y; 114 nd.z = i; 115 vecNd.push_back(nd); 116 } 117 castle[x][y] ^= direction[i]; 118 } 119 } 120 } 121 } 122 printf("%d\n", mergeRoomSize); 123 sort(vecNd.begin(), vecNd.end(), Cmp); 124 printf("%d %d ", vecNd[0].x + 1, vecNd[0].y + 1); 125 if (vecNd[0].z == 1) 126 { 127 printf("N\n"); 128 } 129 else 130 { 131 printf("E\n"); 132 } 133 } 134 135 void Solve() 136 { 137 int roomSum = 0; 138 int roomMaxSize = 0; 139 for (int x = 0; x < M; ++x) 140 { 141 for (int y = 0; y < N; ++y) 142 { 143 if (!visited[x][y]) 144 { 145 //cout << endl << endl << x << " " << y << endl; 146 ++roomSum; 147 int roomSize = 0; 148 searchRoom(x, y, roomSize); 149 roomMaxSize = max(roomMaxSize, roomSize); 150 } 151 } 152 } 153 printf("%d\n", roomSum); 154 printf("%d\n", roomMaxSize); 155 156 } 157 158 int main() 159 { 160 #ifdef HOME 161 freopen("in", "r", stdin); 162 //freopen("out", "w", stdout); 163 #endif 164 165 freopen("castle.in", "r", stdin); 166 freopen("castle.out", "w", stdout); 167 168 scanf("%d %d", &N, &M); 169 memset(visited, false, sizeof(visited)); 170 for (int i = 0; i < M; ++i) 171 { 172 for (int j = 0; j < N; ++j) 173 { 174 scanf("%d", &castle[i][j]); 175 } 176 } 177 Solve(); 178 removeWall(); 179 180 #ifdef HOME 181 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 182 #endif 183 return 0; 184 }
Ordered Fractions:
解法一:
/*
枚举+map
*/
1 /* 2 ID: Jming 3 PROG: frac1 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 55; 32 const int MaxM = 55; 33 34 struct Fraction 35 { 36 int numerator; 37 int denominator; 38 }; 39 40 int N; 41 42 int Gcd(int a, int b) 43 { 44 if (0 == b) return a; 45 return Gcd(b, a%b); 46 } 47 48 // double 比较函数需要自己写,否则在USACO测试平台上,map无法滤掉相同的double键值 49 struct classcomp { 50 bool operator() (const double& lhs, const double& rhs) const 51 { 52 return (lhs < rhs); 53 } 54 }; 55 56 void Solve() 57 { 58 map<double, Fraction, classcomp> ndMap; 59 for (int i = 1; i <= N; ++i) 60 { 61 for (int j = 0; j <= i; ++j) 62 { 63 double val = static_cast<double>(j) / static_cast<double>(i); 64 if (ndMap.find(val) == ndMap.end()) 65 { 66 Fraction fc; 67 int gcd = Gcd(i, j); 68 fc.numerator = j/gcd; 69 fc.denominator = i/gcd; 70 ndMap[val] = fc; 71 } 72 } 73 } 74 for (map<double, Fraction>::const_iterator cnt_it = ndMap.begin(); cnt_it != ndMap.end(); ++cnt_it) 75 { 76 77 cout << (*cnt_it).second.numerator << "/" << (*cnt_it).second.denominator << endl; 78 } 79 } 80 81 int main() 82 { 83 #ifdef HOME 84 freopen("in", "r", stdin); 85 //freopen("out", "w", stdout); 86 #endif 87 88 freopen("frac1.in", "r", stdin); 89 freopen("frac1.out", "w", stdout); 90 91 scanf("%d", &N); 92 Solve(); 93 94 #ifdef HOME 95 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 96 #endif 97 return 0; 98 }
解法二:
/*
枚举+筛选+排序
*/
1 /* 2 ID: Jming 3 PROG: frac1 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 55; 32 const int MaxM = 55; 33 34 struct Fraction 35 { 36 int numerator; 37 int denominator; 38 }; 39 40 int N; 41 // 判断a和b是否互质 42 bool isCoprimes(int a, int b) 43 { 44 int r = a%b; 45 while (r) 46 { 47 a = b; 48 b = r; 49 r = a%b; 50 } 51 return (1 == b); 52 } 53 54 // 分数比较大小(从小到大排序) 55 bool Cmp(const Fraction& f1, const Fraction& f2) 56 { 57 return (f1.numerator*f2.denominator - f1.denominator*f2.numerator < 0); 58 } 59 60 void Solve() 61 { 62 vector<Fraction> vecFra; 63 for (int i = 1; i <= N; ++i) 64 { 65 for (int j = 0; j <= i; ++j) 66 { 67 if (isCoprimes(j, i)) // 滤掉化简相等的分数(i和j的顺序不能变,否则出现除以0的情况) 68 { 69 Fraction fra; 70 fra.numerator = j; 71 fra.denominator = i; 72 vecFra.push_back(fra); 73 } 74 } 75 } 76 sort(vecFra.begin(), vecFra.end(), Cmp); 77 for (int i = 0; i < vecFra.size(); ++i) 78 { 79 printf("%d/%d\n", vecFra[i].numerator, vecFra[i].denominator); 80 } 81 } 82 83 int main() 84 { 85 #ifdef HOME 86 freopen("in", "r", stdin); 87 //freopen("out", "w", stdout); 88 #endif 89 90 freopen("frac1.in", "r", stdin); 91 freopen("frac1.out", "w", stdout); 92 93 scanf("%d", &N); 94 Solve(); 95 96 #ifdef HOME 97 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 98 #endif 99 return 0; 100 }
Sorting a Three-Valued Sequence :
/*
具体参考图Sorting a Three-Valued Sequence.png:
(1)先把需要交换一次的全部找出来
(2)再把需要交换两次的全部找出来
两者次数之和即为答案
*/
Sorting a Three-Valued Sequence.png
1 /* 2 ID: Jming 3 PROG: sort3 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 1010; 32 const int MaxM = 55; 33 34 int seq[MaxN]; 35 int ascOrder[MaxN]; 36 37 int N; 38 39 40 void Solve() 41 { 42 int onceCnt = 0; 43 for (int i = 0; i < N; ++i) 44 { 45 if (seq[i] != ascOrder[i]) 46 { 47 for (int j = i + 1; j < N; ++j) 48 { 49 if ((seq[j] == ascOrder[i]) && 50 (seq[i] == ascOrder[j])) 51 { 52 swap(seq[i], seq[j]); 53 ++onceCnt; 54 break; 55 } 56 } 57 } 58 } 59 int twiceCnt = 0; 60 for (int i = 0; i < N; ++i) 61 { 62 if (seq[i] != ascOrder[i]) 63 { 64 ++twiceCnt; 65 } 66 } 67 twiceCnt = ((twiceCnt/3)<<1); 68 69 printf("%d\n", onceCnt + twiceCnt); 70 } 71 72 int main() 73 { 74 #ifdef HOME 75 freopen("in", "r", stdin); 76 //freopen("out", "w", stdout); 77 #endif 78 79 freopen("sort3.in", "r", stdin); 80 freopen("sort3.out", "w", stdout); 81 82 scanf("%d", &N); 83 for (int i = 0; i < N; ++i) 84 { 85 scanf("%d", &seq[i]); 86 ascOrder[i] = seq[i]; 87 } 88 sort(ascOrder, ascOrder + N); 89 Solve(); 90 91 92 #ifdef HOME 93 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 94 #endif 95 return 0; 96 }
Healthy Holsteins:
/*
枚举所有饲料种类能产生的组合,判断出满足条件的组合。
时间复杂度:N*(2^M)
注:
N:the number of types of vitamins
M:the number of types of feeds available
*/
1 /* 2 ID: Jming 3 PROG: holstein 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 30; 32 const int MaxM = 20; 33 34 int minReq[MaxN]; 35 int feeds[MaxM][MaxN]; 36 int N; 37 int M; 38 int ans[MaxN]; 39 bool isRight = false; 40 41 bool check(const int numOftype) 42 { 43 for (int i = 0; i < N; ++i) 44 { 45 int sum = 0; 46 for (int j = 0; j < numOftype; ++j) 47 { 48 sum += feeds[ans[j]][i]; 49 } 50 if (sum < minReq[i]) 51 { 52 return false; 53 } 54 } 55 return true; 56 } 57 58 void Dfs(int num, int pos, const int numOftype) 59 { 60 if (num == numOftype) 61 { 62 isRight = check(numOftype); 63 return; 64 } 65 for (int i = pos; i < M; ++i) 66 { 67 ans[num] = i; 68 Dfs(num + 1, i + 1, numOftype); 69 if (isRight) 70 { 71 return; 72 } 73 } 74 } 75 76 77 void Solve() 78 { 79 for (int i = 1; i <= M; ++i) 80 { 81 Dfs(0, 0, i); 82 if (isRight) 83 { 84 printf("%d ", i); 85 sort(ans, ans + i); 86 for (int j = 0; j < i; ++j) 87 { 88 printf("%d", ans[j] + 1); 89 if (j != (i - 1)) 90 { 91 printf(" "); 92 } 93 } 94 printf("\n"); 95 return; 96 } 97 } 98 } 99 100 int main() 101 { 102 #ifdef HOME 103 freopen("in", "r", stdin); 104 //freopen("out", "w", stdout); 105 #endif 106 107 freopen("holstein.in", "r", stdin); 108 freopen("holstein.out", "w", stdout); 109 110 scanf("%d", &N); 111 for (int i = 0; i < N; ++i) 112 { 113 scanf("%d", &minReq[i]); 114 } 115 scanf("%d", &M); 116 for (int i = 0; i < M; ++i) 117 { 118 for (int j = 0; j < N; ++j) 119 { 120 scanf("%d", &feeds[i][j]); 121 } 122 } 123 Solve(); 124 125 126 #ifdef HOME 127 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 128 #endif 129 return 0; 130 }
Hamming Codes:
解法一:
/*
枚举
时间复杂度:O(((2^B)-1)*N*B)
*/
1 /* 2 ID: Jming 3 PROG: hamming 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = 30; 32 const int MaxM = 20; 33 34 int N, B, D; 35 vector<int> ans; 36 37 bool isLegal(int x, int y) 38 { 39 int z = x^y; 40 int cnt = 0; 41 while (z) 42 { 43 if (z & 1) 44 { 45 ++cnt; 46 } 47 z >>= 1; 48 } 49 return (cnt >= D); 50 } 51 52 void Solve() 53 { 54 ans.push_back(0); 55 int cnt = 1; 56 int val = 1; 57 while (cnt != N) 58 { 59 bool judge = true; 60 for (int i = 0; i < ans.size(); ++i) 61 { 62 if (!isLegal(val, ans[i])) 63 { 64 judge = false; 65 break; 66 } 67 } 68 if (judge) 69 { 70 ++cnt; 71 ans.push_back(val); 72 } 73 ++val; 74 } 75 } 76 77 void output() 78 { 79 for (int i = 1; i <= ans.size(); ++i) 80 { 81 printf("%d", ans[i - 1]); 82 if (i != ans.size()) 83 { 84 if (i % 10) 85 { 86 printf(" "); 87 } 88 else 89 { 90 printf("\n"); 91 } 92 } 93 } 94 printf("\n"); 95 } 96 97 98 int main() 99 { 100 #ifdef HOME 101 freopen("in", "r", stdin); 102 //freopen("out", "w", stdout); 103 #endif 104 105 freopen("hamming.in", "r", stdin); 106 freopen("hamming.out", "w", stdout); 107 scanf("%d %d %d", &N, &B, &D); 108 Solve(); 109 output(); 110 111 #ifdef HOME 112 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 113 #endif 114 return 0; 115 }
解法二:
/*
枚举
预先处理出codewords间的Hamming distance,保存在数组中。
*/
1 /* 2 ID: Jming 3 PROG: hamming 4 LANG: C++ 5 */ 6 #include <iostream> 7 #include <fstream> 8 #include <sstream> 9 #include <cstdlib> 10 #include <cstdio> 11 #include <cstddef> 12 #include <iterator> 13 #include <algorithm> 14 #include <string> 15 #include <locale> 16 #include <cmath> 17 #include <vector> 18 #include <cstring> 19 #include <map> 20 #include <utility> 21 #include <queue> 22 #include <stack> 23 #include <set> 24 #include <functional> 25 using namespace std; 26 typedef pair<int, int> PII; 27 typedef long long int64; 28 const int INF = 0x3f3f3f3f; 29 const int modPrime = 3046721; 30 const double eps = 1e-9; 31 const int MaxN = (1<<8) + 10; 32 const int MaxM = 20; 33 34 int N, B, D; 35 vector<int> ans; 36 int dis[MaxN][MaxN]; 37 38 int hamDis(int x, int y) 39 { 40 int z = x^y; 41 int cnt = 0; 42 while (z) 43 { 44 if (z & 1) 45 { 46 ++cnt; 47 } 48 z >>= 1; 49 } 50 return cnt; 51 } 52 53 void ini() 54 { 55 int maxVal = (1 << B); 56 for (int i = 0; i < maxVal; ++i) 57 { 58 for (int j = i + 1; j < maxVal; ++j) 59 { 60 dis[i][j] = hamDis(i, j); 61 } 62 } 63 } 64 65 void Solve() 66 { 67 ans.push_back(0); 68 int cnt = 1; 69 int val = 1; 70 while (cnt != N) 71 { 72 bool judge = true; 73 for (int i = 0; i < ans.size(); ++i) 74 { 75 if (dis[ans[i]][val] < D) 76 { 77 judge = false; 78 break; 79 } 80 } 81 if (judge) 82 { 83 ++cnt; 84 ans.push_back(val); 85 } 86 ++val; 87 } 88 } 89 90 void output() 91 { 92 for (int i = 1; i <= ans.size(); ++i) 93 { 94 printf("%d", ans[i - 1]); 95 if (i != ans.size()) 96 { 97 if (i % 10) 98 { 99 printf(" "); 100 } 101 else 102 { 103 printf("\n"); 104 } 105 } 106 } 107 printf("\n"); 108 } 109 110 111 int main() 112 { 113 #ifdef HOME 114 freopen("in", "r", stdin); 115 //freopen("out", "w", stdout); 116 #endif 117 118 freopen("hamming.in", "r", stdin); 119 freopen("hamming.out", "w", stdout); 120 scanf("%d %d %d", &N, &B, &D); 121 ini(); 122 Solve(); 123 output(); 124 125 #ifdef HOME 126 cerr << "Time elapsed: " << clock() / CLOCKS_PER_SEC << " ms" << endl; 127 #endif 128 return 0; 129 }