第一题ac
同一花色判断是否相连,不同花色要判断数字是否重复
#include <iostream> #include <unordered_map> #include <string> #include <algorithm> using namespace std; int main() { int n; cin >> n; while (n--) { int a[10] = {0}; int flag = 1; unordered_map<char, vector<int>> v; for (int i = 0; i < 7; ++i) { string str; cin >> str; v[str[1]].push_back(str[0] - '0'); } for (auto c : v) { if (c.second.size() > 3) { flag = 0; break; } sort(c.second.begin(), c.second.end()); a[c.second[0]] = 1; for (int i = 1; i < c.second.size(); ++i) { if (c.second[i] == c.second[i - 1] + 1 || c.second[i] == c.second[i - 1] + 2) { flag = 0; break; } if (a[c.second[i]] == 1) { flag = 0; break; } else { a[c.second[i]] = 1; } } } if (flag) { cout << "YES" << endl; } else { cout << "NO" << endl; } } }
第二题ac
暴力存行和和列和,最后判断最大和。
#include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<vector<int>> v(n + 1, vector<int>(n + 1)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { cin >> v[i][j]; } } while (n >= 1) { int posI = 0, posJ = 0; int maxx = -1; vector<int> hang(n + 1, 0); vector<int> lie(n + 1, 0); for (int i = 1; i <= n; ++i) { for (int j = 1;j <= n; ++j) { hang[i] += v[i][j]; } } for (int j = 1; j <= n; ++j) { for (int i = 1; i <= n; ++i) { lie[i] += v[j][i]; } } for (int i = 1; i <= n; ++i) { for (int j = 1;j <= n; ++j) { if (hang[i] + lie[j] - v[i][j] > maxx) { posI = i; posJ = j; maxx = hang[i] + lie[j] - v[i][j]; } } } cout << posI << " " << posJ << endl; n--; vector<vector<int>> val(n + 1, vector<int>(n + 1)); for (int i = 1; i <= n; ++i) { for (int j = 1; j <= n; ++j) { if (i < posI && j < posJ) { val[i][j] = v[i][j]; } else if (i < posI && j >= posJ) { val[i][j] = v[i][j + 1]; } else if (i >= posI && j < posJ) { val[i][j] = v[i + 1][j]; } else { val[i][j] = v[i + 1][j + 1]; } } } v = val; } return 0; }第三题100ac
利用栈存储这个树的结构,对每个节点减去其子节点的时间的自身时间
#include <iostream> #include <vector> #include <unordered_map> #include <stack> using namespace std; struct node { vector<int> sons; int beginTime; int wasteTime; int selfTime; }; int main () { int n; cin >> n; while (n--) { int k; cin >> k; stack<int> s; unordered_map<int, node> v; for (int i = 0; i < k; ++i) { int nowTime, id, flag; cin >> nowTime >> id >> flag; if (!flag) { v[id].beginTime = nowTime; if (!s.empty()) { v[s.top()].sons.push_back(id); } s.push(id); } else { v[id].wasteTime = nowTime - v[id].beginTime; v[id].selfTime = v[id].wasteTime; s.pop(); } } int maxx = -1; int ans = -1; for (auto &c: v) { for (auto d: c.second.sons) { c.second.selfTime -= v[d].wasteTime; } if (c.second.selfTime > maxx) { maxx = c.second.selfTime; ans = c.first; } else if (c.second.selfTime == maxx) { if (c.first < ans) { ans = c.first; } } } cout << ans << endl; } }
第四题 0ac
刚开始看错题了,血崩,我没看到要先找最短路径 :(
代码不贴了去,求补充~
全部评论
(1) 回帖