9.6腾讯笔试
第四题(题目有毒)
#include <vector> #include <string> #include <iostream> #include <algorithm> using namespace std; int main() { int N; long long s = 0; cin >> N; vector<int> vec1(N),vec2(N); for (int i = 0; i < N; i++) { cin >> vec1[i]; vec2[i] = vec1[i]; } sort(vec2.begin(), vec2.end()); int Lmid = vec2[N / 2 - 1]; int Rmid = vec2[N / 2]; for (int i = 0; i < N; i++) { if (vec1[i] >= Rmid) { cout << Lmid << "\n"; } else { cout << Rmid << "\n"; } } return 0; }第三题
#include <vector> #include <string> #include <iostream> #include <unordered_map> #include <queue> #include <algorithm> using namespace std; long long res = 0; unordered_map<int, vector<int>> teams; unordered_map<int, bool> teamVis; unordered_map<int, bool> manVis; vector<vector<int,int>> party; int main() { int n, m; cin >> n >> m; for (int i = 0; i < m; i++) { int x; cin >> x; vector<int> tVec; for (int j = 0; j < x; j++) { int t; cin >> t; tVec.emplace_back(t); teams[t].emplace_back(i);//x参加了什么 } party.emplace_back(tVec); } cout << res; return 0; } void dfs(int curMan) { for (auto team : teams[curMan]) { if (teamVis[team] == false) { teamVis[team] = true; for (auto man : party[team]) { if (!manVis[man]) { res++; manVis[man] = true; dfs(man); } } } } }
第二题:
#include <vector> #include <string> #include <iostream> #include <unordered_map> #include <queue> #include <algorithm> using namespace std; struct MyCmp1 { bool operator()(const pair<string, int>& x, const pair<string, int>& y) { if (x.second == y.second) return x.first < y.first; return x.second > y.second; } }; struct MyCmp1_1 { bool operator()(const pair<string, int>& x, const pair<string, int>& y) { if (x.second == y.second) return x.first < y.first; return x.second > y.second; } }; struct MyCmp2 { bool operator()(const pair<string, int>& x, const pair<string, int>& y) { if (x.second == y.second) return x.first < y.first; return x.second < y.second; } }; struct MyCmp2_2 { bool operator()(const pair<string, int>& x, const pair<string, int>& y) { if (x.second == y.second) return x.first < y.first; return x.second < y.second; } }; int main() { int N, K; cin >> N >> K; unordered_map<string,int> vis; //小顶 priority_queue<pair<string, int>, vector<pair<string, int>>, MyCmp1> q1; //大顶 priority_queue<pair<string, int>, vector<pair<string, int>>, MyCmp2> q2; for (int i = 0; i < N; i++) { string s; cin >> s; //cout << s << '\n'; vis[s]++; } for (auto v : vis) { string str = v.first; int cnt = v.second; //cout << v.first << " " << v.second << "\n"; if (q1.size() < K) { q1.emplace(str, cnt); } else { if (q1.top().second < cnt) { q1.pop(); q1.emplace(str, cnt); } else if (q1.top().second == cnt && str < q1.top().first) { q1.pop(); q1.emplace(str, cnt); } } if (q2.size() < K) { q2.emplace(str, cnt); } else { if (q2.top().second > cnt) { q2.pop(); q2.emplace(str, cnt); } else if (q2.top().second == cnt && str < q2.top().first) { q2.pop(); q2.emplace(str, cnt); } } } vector<pair<string, int>> v1; vector<pair<string, int>> v2; while (!q1.empty()) { v1.emplace_back(q1.top().first, q1.top().second); q1.pop(); } while (!q2.empty()) { v2.emplace_back(q2.top().first, q2.top().second); q2.pop(); } sort(v1.begin(), v1.end(), MyCmp1_1()); sort(v2.begin(), v2.end(), MyCmp2_2()); for(auto i:v1) cout << i.first << " " << i.second << "\n"; for(auto i:v2) cout << i.first << " " << i.second << "\n"; return 0; }
全部评论
(14) 回帖