标签:info dex using display str main 使用 begin 不能
A 1148 Werewolf - Simple Version
思路比较直接:模拟就行。因为需要序列号最小的两个狼人,所以以狼人为因变量进行模拟。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 7 using namespace std; 8 int N; 9 vector<int> stateVec; 10 vector<int> inputVec; 11 bool judgeRight(int a, int b) 12 { 13 int liarCnt = 0, wereLiarCnt = 0, tmpNum; 14 fill(stateVec.begin(), stateVec.end(), 0); 15 stateVec[a] = stateVec[b] = 1; 16 for(int i = 1; i <= N; ++ i) 17 { 18 tmpNum = inputVec[i]; 19 if((tmpNum > 0 && stateVec[tmpNum] == 1) || (tmpNum < 0 && stateVec[-tmpNum] == 0)) 20 { 21 liarCnt ++; 22 if(i == a || i == b) 23 wereLiarCnt ++; 24 } 25 } 26 if(liarCnt == 2 && wereLiarCnt == 1) 27 return true; 28 else 29 return false; 30 } 31 int main() 32 { 33 cin >> N; 34 inputVec.resize(N+1, 0); 35 stateVec.resize(N+1, 0); 36 for(int i = 1; i <= N; ++ i) 37 cin >> inputVec[i]; 38 for(int i = 1; i < N; ++i) 39 { 40 for(int j = i+1; j <= N; ++ j) 41 { 42 if(judgeRight(i,j)) 43 { 44 cout << i << " " << j; 45 return 0; 46 } 47 } 48 } 49 cout << "No Solution"; 50 return 0; 51 }
A 1149 Dangerous Goods Packaging
这个最开始使用图+二重for循环直接判,发现超时很严重。后来改用邻接表存储,每个物品更新一下与其互斥物品的标志。成功通过。
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <unordered_map> 7 using namespace std; 8 int N, M; 9 typedef long long LL; 10 unordered_map<int, vector<int>> goodsMap; 11 vector<int> goodsVec; 12 bool judgeRight(void) 13 { 14 int tmpNum; 15 int len = goodsVec.size(); 16 unordered_map<int, int> incomFlagMap; 17 for(int i = 0; i < len; ++ i) 18 { 19 tmpNum = goodsVec[i]; 20 if(incomFlagMap[tmpNum] == 1) 21 return false; 22 for(auto it = goodsMap[tmpNum].begin(); it != goodsMap[tmpNum].end(); ++it) 23 incomFlagMap[*it] = 1; 24 } 25 return true; 26 } 27 int main() 28 { 29 cin >> N >> M; 30 int tmpNum2, tmpNum1; 31 while(N--) 32 { 33 cin >> tmpNum1 >> tmpNum2; 34 goodsMap[tmpNum1].push_back(tmpNum2); 35 goodsMap[tmpNum2].push_back(tmpNum1); 36 } 37 while(M--) 38 { 39 cin >> N; 40 goodsVec.resize(N,0); 41 while(N--) 42 cin >> goodsVec[N]; 43 if(judgeRight()) 44 cout << "Yes" << endl; 45 else 46 cout << "No" << endl; 47 } 48 return 0; 49 }
A 1150 Travelling Salesman Problem
这个理解题意就行。需要注意的地方:
1.不能通过的路径,距离输出为NA
2.最小距离是在TS cycle 和 TS simple cycle中选出
3.TS cycle 需要最后一个城市为出发城市
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 using namespace std; 7 int N, M, K; 8 const int INF = 0x7f7f7f7f; 9 int route[210][210]={0}; 10 int main() 11 { 12 cin >> N >> M; 13 int tmpSt, tmpEnd, tmpDis, tmpNum, lastCity, nowCity, minDis = INF, minIndex; 14 while(M--) 15 { 16 cin >> tmpSt >> tmpEnd >> tmpDis; 17 route[tmpSt][tmpEnd] = tmpDis; 18 route[tmpEnd][tmpSt] = tmpDis; 19 } 20 cin >> K; 21 for(int i = 1; i <= K; ++i) 22 { 23 tmpDis = 0; 24 cin >> tmpNum; 25 cin >> tmpSt; 26 lastCity = tmpSt; 27 vector<int> flagVec(N+1,0); 28 bool simpleFlag = true; 29 int travelCityCnt = 0; 30 for(int j = 1; j < tmpNum; j++) 31 { 32 cin >> nowCity; 33 if(flagVec[nowCity] == 0) 34 { 35 flagVec[nowCity] = 1; 36 travelCityCnt ++; 37 } 38 else 39 simpleFlag = false; 40 if(tmpDis >= 0 && route[lastCity][nowCity] > 0) 41 tmpDis += route[lastCity][nowCity]; 42 else 43 tmpDis = -1; 44 lastCity = nowCity; 45 } 46 if(lastCity == tmpSt && simpleFlag && tmpDis >= 0 && travelCityCnt == N) 47 printf("Path %d: %d (TS simple cycle)\n", i, tmpDis); 48 else if(tmpDis >= 0 && lastCity == tmpSt && travelCityCnt >= N) 49 printf("Path %d: %d (TS cycle)\n", i, tmpDis); 50 else 51 { 52 tmpDis >= 0 ? printf("Path %d: %d (Not a TS cycle)\n", i, tmpDis) : printf("Path %d: NA (Not a TS cycle)\n", i); 53 tmpDis = -1; 54 } 55 if(tmpDis > 0 && tmpDis < minDis) 56 { 57 minDis = tmpDis; 58 minIndex = i; 59 } 60 } 61 printf("Shortest Dist(%d) = %d", minIndex, minDis); 62 return 0; 63 }
A 1151 LCA in a Binary Tree
只得到了22分,剩下的8分找不到原因了。╮(╯▽╰)╭
1 #include <cstdio> 2 #include <cstdlib> 3 #include <iostream> 4 #include <algorithm> 5 #include <vector> 6 #include <unordered_map> 7 using namespace std; 8 int N, M, K; 9 vector<int> preOrderVec, inOrderVec; 10 unordered_map<int, int> valToIndexMap; 11 unordered_map<int, int> indexToValMap; 12 void insertVal(int inL, int inR, int preL, int preR, int index) 13 { 14 if(inL > inR || preL > preR) 15 return; 16 int tmpNum = preOrderVec[preL]; 17 int tmpIndex = inL; 18 while(inOrderVec[tmpIndex] != tmpNum) 19 tmpIndex++; 20 //treeNodeVec[index] = tmpNum; 21 valToIndexMap[tmpNum] = index; 22 indexToValMap[index] = tmpNum; 23 insertVal(inL, tmpIndex-1, preL+1, preL+tmpIndex-inL, index*2); 24 insertVal(tmpIndex+1, inR, preR-inR+tmpIndex+1,preR, index*2+1); 25 return; 26 } 27 void getNodeInfo(int a, int b) 28 { 29 int tmpIndex1 = valToIndexMap[a], tmpIndex2 = valToIndexMap[b]; 30 if(tmpIndex1 == 0 && tmpIndex2 == 0) 31 printf("ERROR: %d and %d are not found.\n", a, b); 32 else if(tmpIndex1 == 0 || tmpIndex2 == 0) 33 tmpIndex1 == 0 ? printf("ERROR: %d is not found.\n", a) : printf("ERROR: %d is not found.\n", b); 34 else 35 { 36 while(tmpIndex1 != tmpIndex2) 37 tmpIndex1 > tmpIndex2 ? tmpIndex1 /= 2 : tmpIndex2 /= 2; 38 int tmpNum = indexToValMap[tmpIndex1]; 39 if(tmpNum == a || tmpNum == b) 40 tmpNum == a ? printf("%d is an ancestor of %d.\n", a, b) : printf("%d is an ancestor of %d.\n", b, a); 41 else 42 printf("LCA of %d and %d is %d.\n", a, b, tmpNum); 43 } 44 } 45 int main() 46 { 47 cin >> M >> N; 48 int tmpNum1, tmpNum2; 49 preOrderVec.resize(N); 50 inOrderVec.resize(N); 51 for(int i = 0; i < N; ++i) 52 cin >> inOrderVec[i]; 53 for(int i = 0; i < N; ++ i) 54 cin >> preOrderVec[i]; 55 insertVal(0, N-1, 0, N-1, 1); 56 while(M--) 57 { 58 cin >> tmpNum1 >> tmpNum2; 59 getNodeInfo(tmpNum1, tmpNum2); 60 } 61 return 0; 62 }
标签:info dex using display str main 使用 begin 不能
原文地址:https://www.cnblogs.com/codewars/p/11374301.html