9.6腾讯笔试(后台&综合)

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;
}


#腾讯#
全部评论
第五题是不是可以使用选择排序的思路。
1 回复 分享
发布于 2020-09-06 22:40
https://blog.csdn.net/qq_38649940/article/details/108438347 腾讯笔试题解!(虽然好像和大家题目不同emm....
1 回复 分享
发布于 2020-09-06 23:02
天翼云科技有限公司
校招火热招聘中
官网直投
第四题那个题目我看了15分钟,还好A了,求个第五题思路
点赞 回复 分享
发布于 2020-09-06 22:04
不同岗位题不一样
点赞 回复 分享
发布于 2020-09-06 22:05
也不加个路过,别人直接点5岂不是打击我们的信心?😂
点赞 回复 分享
发布于 2020-09-06 22:10
有做算法研究员的哥们吗?感觉有点难啊
点赞 回复 分享
发布于 2020-09-06 22:12
大佬们都A了4道,羡慕
点赞 回复 分享
发布于 2020-09-06 22:14
做完华为的笔试赶着来腾讯 一个小时 第一题 双指针 100% 第二题 队列  思路写完,还没来得及调试代码。。。。。 感觉应该可以100%的 第三题 没认真看 0 第四题 中位数 50% 第五题 红黑旗  0
点赞 回复 分享
发布于 2020-09-06 22:21
就求一个红黑棋思路 感觉选4-4.99的那么多 都是死在红黑棋了
点赞 回复 分享
发布于 2020-09-06 22:24
第四题我人傻了
点赞 回复 分享
发布于 2020-09-06 22:26
第五题头皮挠破,最后输出0,+0.05分🤣
点赞 回复 分享
发布于 2020-09-06 22:34
第二题我构建了图结构,然后从0开始BFS遍历,结果只过了20,说是数组越界,看了好久感觉不应该啊,有人和我思路相似吗
点赞 回复 分享
发布于 2020-09-06 22:48
排序输出那道...我NM自己写测试用例怎么测试都对,传上去就是0我个five
点赞 回复 分享
发布于 2020-09-07 09:49
第五题盯了半小时草稿纸写了一整张还是没思路。。。QAQ
点赞 回复 分享
发布于 2020-09-07 19:30

相关推荐

Natrium_:这下真直聘了
点赞 评论 收藏
分享
5 18 评论
分享
牛客网
牛客企业服务