美团笔试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 四川

相关推荐

不愿透露姓名的神秘牛友
07-25 17:46
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
评论
2
5
分享

创作者周榜

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