这次题目好长啊,读题得心累。。
第一题(100%)
#include <bits/stdc++.h> using namespace std; const long long infl = 0x3f3f3f3f3f3f3f3fll; int main() { #ifdef __LOCAL_WONZY__ freopen("input-1.txt", "r", stdin); #endif // __LOCAL_WONZY__ int T; cin >> T; while (T--) { vector<int> w, b, t; for (int i = 0; i < 7; ++i) { string s; cin >> s; if (s[1] == 'W') w.push_back(s[0] - '0'); else if (s[1] == 'B') b.push_back(s[0] - '0'); else if (s[1] == 'T') t.push_back(s[0] - '0'); else assert(true); } sort(w.begin(), w.end()); sort(b.begin(), b.end()); sort(t.begin(), t.end()); w.erase(unique(w.begin(), w.end()), w.end()); b.erase(unique(b.begin(), b.end()), b.end()); t.erase(unique(t.begin(), t.end()), t.end()); bool ans = true; ans &= (w.size() + b.size() + t.size() == 7); ans &= (w.size() <= 3 && b.size() <= 3 && t.size() <= 3); for (auto& x : w) ans &= (x % 3 == w[0] % 3); for (auto& x : b) ans &= (x % 3 == b[0] % 3); for (auto& x : t) ans &= (x % 3 == t[0] % 3); vector<int> ar; if (!w.empty()) ar.push_back(w[0] % 3); if (!b.empty()) ar.push_back(b[0] % 3); if (!t.empty()) ar.push_back(t[0] % 3); for (int i = 0; i < ar.size(); ++i) { for (int j = 0; j < i; ++j) { ans &= (ar[i] != ar[j]); } } cout << (ans ? "YES" : "NO") << endl; } return 0; }
第二题(100%)
暴力dfs 就好了。
注意递归的空间复杂度。。
注意释放原数组空间。。
#include <bits/stdc++.h> using namespace std; const long long infl = 0x3f3f3f3f3f3f3f3fll; const int MAXN = 505; void dfs(vector<vector<int>>& G, vector<pair<int, int>>& ans) { if(G.size() == 0) return; int n = G.size(); vector<int> row(n, 0), col(n, 0); for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { row[i] += G[i][j]; col[j] += G[i][j]; } } int x = 0, y = 0, mval = row[0] + col[0] - G[0][0]; for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { if(row[i] + col[j] - G[i][j] <= mval) continue; mval = row[i] + col[j] - G[i][j]; x = i, y = j; } } ans.push_back(make_pair(x, y)); vector<vector<int>> Gnext; for(int i = 0; i < n; ++i) { if(i == x) continue; vector<int> buf; for(int j = 0; j < n; ++j) { if(j == y) continue; buf.push_back(G[i][j]); } Gnext.push_back(buf); } G = move(Gnext); // 释放原数组 dfs(G, ans); } int main() { #ifdef __LOCAL_WONZY__ freopen("input-2.txt", "r", stdin); #endif // __LOCAL_WONZY__ int n; cin >> n; vector<vector<int>> G(n, vector<int>(n)); for(int i = 0; i < n; ++i) { for(int j = 0; j < n; ++j) { cin >> G[i][j]; } } vector<pair<int, int>> ans; dfs(G, ans); for(auto& pr : ans) { cout << pr.first+1 << " " << pr.second+1 << endl; } return 0; }
第三题(100%)
维护一个栈。
#include <bits/stdc++.h> using namespace std; const int inf = 0x3f3f3f3f; const long long infl = 0x3f3f3f3f3f3f3f3fll; struct Node { long long t, e, s; }; int main() { #ifdef __LOCAL_WONZY__ freopen("input-3.txt", "r", stdin); #endif // __LOCAL_WONZY__ int T; cin >> T; while (T--) { int n; cin >> n; vector<Node> tasks(n); for (int i = 0; i < n; ++i) { cin >> tasks[i].t >> tasks[i].e >> tasks[i].s; } unordered_map<long long, long long> mp; stack<int> stk; for (int i = 0; i < n; ++i) { if (!stk.empty()) mp[stk.top()] += (tasks[i].t - tasks[i-1].t); if (tasks[i].s == 0) stk.push(tasks[i].e); if (tasks[i].s == 1) stk.pop(); } auto ans = mp.begin()->first; for (auto& pr : mp) { if (pr.second > mp[ans]) ans = pr.first; else if(pr.second == mp[ans]) ans = min(ans, pr.first); } cout << ans << endl; } return 0; }
全部评论
(3) 回帖