美团笔试820

#include<iostream>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<unordered_map>
using namespace std;

// 交替输出字符串
void first() {
	int n = 0;
	cin >> n;
	string line1, line2;
	cin >> line1;
	cin >> line2;

	for (int i = 0; i < n; i++) {
		cout << line1[i] << line2[i];
	}
}
// 通信坐标最小
void second() {
	int n = 0;
	cin >> n;
	int x1 = 0, x2 = 0, x3 = 0, y1 = 0, y2 = 0, y3 = 0;
	cin >> x1 >> y1;
	cin >> x2 >> y2;
	cin >> x3 >> y3;
	int d1 = 0, d2 = 0, d3 = 0;
	cin >> d1 >> d2 >> d3;
	set<string> s1, s2;

	int ansx = INT32_MAX, ansy = INT32_MAX;
	for (int x = x1 - d1; x < x1 + d1; x++) {
		if (x <= 0 || x > n) continue;

		int xd = abs(x - x1);
		int left = d1 - xd;
		if (left == 0) {
			string k = to_string(x) + " " + to_string(y1);
			s1.insert(k);
		}
		else {
			int yy1 = y1 - left;
			int yy2 = y1 + left;
			if (yy1 > 0 && yy1 <= n) {
				string k = to_string(x) + " " + to_string(yy1);
				s1.insert(k);
			}
			if (yy2 > 0 && yy2 <= n) {
				string k = to_string(x) + " " + to_string(yy2);
				s1.insert(k);
			}
		}
	}
	
	for (int x = x2 - d2; x < x2 + d2; x++) {
		if (x <= 0 || x > n) continue;

		int xd = abs(x - x2);
		int left = d2 - xd;
		if (left == 0) {
			string k = to_string(x) + " " + to_string(y2);
			if (s1.count(k))
				s2.insert(k);
		}
		else {
			int yy1 = y2 - left;
			int yy2 = y2 + left;
			if (yy1 > 0 && yy1 <= n) {
				string k = to_string(x) + " " + to_string(yy1);
				if(s1.count(k))
					s2.insert(k);
			}
			if (yy2 > 0 && yy2 <= n) {
				string k = to_string(x) + " " + to_string(yy2);
				if (s1.count(k))
					s2.insert(k);
			}
		}
	}


	for (int x = x3 - d3; x < x3 + d3; x++) {
		if (x <= 0 || x > n) continue;

		int xd = abs(x - x3);
		int left = d3 - xd;
		if (left == 0) {
			string k = to_string(x) + " " + to_string(y3);
			if (s2.count(k)) {
				if (ansx > x || ansx == x && ansy > y3) {
					ansx = x;
					ansy = y3;
				}
			}
				
		}
		else {
			int yy1 = y3 - left;
			int yy2 = y3 + left;
			if (yy1 > 0 && yy1 <= n) {
				string k = to_string(x) + " " + to_string(yy1);
				if (s2.count(k)) {
					if (ansx > x || ansx == x && ansy > y3) {
						ansx = x;
						ansy = yy1;
					}
				}
			}
			if (yy2 > 0 && yy2 <= n) {
				string k = to_string(x) + " " + to_string(yy2);
				if (s2.count(k)) {
					if (ansx > x || ansx == x && ansy > y3) {
						ansx = x;
						ansy = yy2;
					}
				}
			}
		}
	}


	cout << ansx<<" "<<ansy;

}

// 复习获取最大期望
void third() {
	int n = 0, m = 0;
	cin >> n >> m;
	int i = 0;
	int num = 0;
	vector<int> p(n, 0), scores(n, 0);
	while (i < n) {
		cin >> num;
		p[i++] = num;
	}
	i = 0;
	while (i < n) {
		cin >> num;
		scores[i++] = num;
	}

	vector<pair<int,double>> tmp(n,{0,0});
	for (i = 0; i < n; i++) {
		tmp[i] = { i,(double)(100 - p[i]) / (double)100 * (double)scores[i] };
	}

	sort(tmp.begin(), tmp.end(), [&](const pair<int, double>& a, const pair<int, double>& b) {
		return a.second > b.second;
		});
	set<int> indexs;
	for (i = 0; i < m; i++) {
		indexs.insert(tmp[i].first);
	}
	double sum = 0;
	for (i = 0; i < n; i++) {
		if (indexs.count(i)) {
			sum += scores[i];
		}
		else {
			sum += (double)scores[i] * (double)p[i] / 100.0;
		}
	}

	printf("%.2f", sum);
}

 
long dp(vector<int>& s1, vector<int>& s2, int i, int j, unordered_map<long, long>& memo) {
	if (i < 0 && j < 0)
		return 0;
	int key = i * 20000 + j;
	if (memo.count(key)) {
		return memo[key];
	}
	long i_c = LONG_MAX;
	long j_c = LONG_MAX;
	long cc = LONG_MAX;
	if (i >= 0 && j >= 0) {
		cc = abs(s1[i] - s2[j]) + dp(s1, s2, i - 1, j - 1, memo);
		i_c = abs(s1[i]) + dp(s1, s2, i - 1, j, memo);
		j_c = abs(s2[j]) + dp(s1, s2, i, j - 1, memo);
	}
	else if (i >= 0) {
		i_c = abs(s1[i]) + dp(s1, s2, i - 1, j, memo);
	}
	else if (j >= 0) {
		j_c = abs(s2[j]) + dp(s1, s2, i, j - 1, memo);
	}

	memo[key] = min(min(i_c, j_c), cc);

	return memo[key];

}

// 同步两个字符串的最小距离
void forth() {
	int n = 0, m = 0;
	cin >> n >> m;
	int i = 0;
	int num = 0;
	vector<int> s1(n, 0), s2(m, 0);
	while (i < n) {
		cin >> num;
		s1[i++] = num;
	}
	i = 0;
	while (i < m) {
		cin >> num;
		s2[i++] = num;
	}

	unordered_map<long, long> memo;

	long ans = dp(s1, s2, n - 1, m - 1, memo);
	cout << ans;
}
int main() {
	forth();

	return 0;
}
#美团笔试#
全部评论
麻烦问下投的什么岗位的笔试?
点赞 回复 分享
发布于 2022-08-20 18:11 山东
我还想问一下定位那一题思路,有没有大佬解答一下
点赞 回复 分享
发布于 2022-08-20 17:23 四川
楼主,问一下,第四题一定要用long吗,我用int只过了45%,是因为越界的关系吗
点赞 回复 分享
发布于 2022-08-20 17:18 四川
复习最大期望是必须要保留两位小数吗,我说怎么一直无法a掉,淦。另外你写的这几个题的代码,和我简直一模一样哈哈
点赞 回复 分享
发布于 2022-08-20 14:06 北京
我想问一下 java 第一题 public static void out(String A,String B,int n){     StringBuffer ans=new StringBuffer();     for(int i=0;i<n;i++){              ans.append(A.charAt(i));              ans.append(B.charAt(i));      }      System.out.println(ans.toString()); } 不知道为什么过不了 能解答一下吗
点赞 回复 分享
发布于 2022-08-20 13:01 上海
第二题代码好长哦
点赞 回复 分享
发布于 2022-08-20 12:30 北京
第二题一直不能AC全部,也不知道哪错了。。
点赞 回复 分享
发布于 2022-08-20 12:25 四川

相关推荐

06-27 12:54
已编辑
门头沟学院 Java
累了,讲讲我的大学经历吧,目前在家待业。我是一个二本院校软件工程专业。最开始选专业是觉得计算机感兴趣,所以选择了他。本人学习计算机是从大二暑假结束开始的,也就是大三开始。当时每天学习,我个人认为Java以及是我生活的一部分了,就这样持续学习了一年半,来到了大四上学期末,大概是在12月中旬,我终于找的到了一家上海中厂的实习,但我发现实习生的工作很枯燥,公司分配的活也不多,大多时间也是自己在自学。就这样我秋招末才找到实习。时间来到了3月中旬,公司说我可以转正,但是转正工资只有7000,不过很稳定,不加班,双休,因为要回学校参加答辩了,同时当时也是心高气傲,认为可以找到更好的,所以放弃了转正机会,回学校准备论文。准备论文期间就也没有投递简历。然后时间来到了5月中旬,这时春招基本也结束了,然后我开始投递简历,期间只是约到了几家下场面试。工资也只有6-7k,到现在我不知道该怎么办了。已经没有当初学习的心劲了,好累呀,但是又不知道该干什么去。在家就是打游戏,boss简历投一投。每天日重一次。26秋招都说是针对26届的人,25怎么办。我好绝望。要不要参加考公、考研、央国企这些的。有没有大佬可以帮帮我。为什么感觉别人找工作都是顺其自然的事情,我感觉自己每一步都在艰难追赶。八股文背了又忘背了又忘,我每次都花很长时间去理解他,可是现在感觉八股、项目都忘完了。真的已经没有力气再去学习了。图片是我的简历,有没有大哥可以指正一下,或者说我应该走哪条路,有点不想在找工作了。
码客明:太累了就休息一下兄弟,人生不会完蛋的
如果实习可以转正,你会不...
点赞 评论 收藏
分享
评论
2
5
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务