9.16 美团笔试 4.06 / 5.00

T1 100%

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, a[N];

void solve() {
	cin >> n;
	for(int i = 0; i < n; i ++) cin >> a[i];

	int ans = a[0];
	for(int i = 1; i < n; i ++) {
		if(a[i] == 1) {
			ans ++ ;
			if(a[i - 1] == 1) ans ++ ;
		}
	}

	cout << ans << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T2 100%

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

void solve() {
	int x, y;
	cin >> x >> y;
	int r = ceil(sqrt(x * x + y * y));
	if(r == 0) r = 1;
	cout << max(11 - r, 0) << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T3 100%

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;
typedef pair<int, int> PII;

const int N = 1e3 + 10;

int n, H, A;
int h[N], a[N], ids[N];

void solve() {
	cin >> n >> H >> A;

	for(int i = 0; i < n; i ++) cin >> h[i];
	for(int i = 0; i < n; i ++) cin >> a[i];
	for(int i = 0; i < n; i ++) ids[i] = i;
	sort(ids, ids + n, [&](int a, int b){
		return h[a] < h[b];
	});

	int ed = -1;
	for(int i = 0; i < n; i ++) {
		int id = ids[i];
		if(h[id] >= H) break;
		ed = i;
	}

	if(ed == -1) cout << 0 << endl;
	else {
		vector<int> nums, f;
		for(int i = 0; i <= ed; i ++) {
			if(a[ids[i]] >= A) continue;
			nums.push_back(a[ids[i]]);
		}

		int ans = 0;
		f = vector<int> (nums.size());
		for(int i = 0; i < f.size(); i ++) {
			f[i] = 1;
			for(int j = 0; j < i; j ++) {
				if(nums[j] < nums[i]) f[i] = max(f[j] + 1, f[i]);
			}
			ans = max(ans, f[i]);
		}

		cout << ans << endl;
	}
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T4 100%

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10;

int n, a[N];

int get(int x) {
	int ret = 0;
	while(x != 0 && x % 2 == 0) {
		x /= 2;
		ret ++ ;
	}
	return ret;
}

void solve() {
	cin >> n;
	for(int i = 0; i < n; i ++) cin >> a[i];

	sort(a, a + n);
	int ans = get(a[n - 1]), rec = a[n - 1];
	for(int i = n - 2; i >= 0; i --) {
		rec &= a[i];
		ans = max(ans, get(rec));
	}

	cout << ans << endl;
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}

T5 6.67%

只会骗了

cpp 复制代码
#include <bits/stdc++.h>

#define endl '\n'

using namespace std;

typedef long long LL;

const int N = 1e5 + 10, M = N * 2;

int n, m, C;
int h[N], e[M], ne[M], w[M], flag[M], num[M], idx;

LL total = 1e18;
vector<int> path, ans;

void add(int a, int b, int c, int f, int id) {
	e[idx] = b, w[idx] = c, flag[idx] = f;
	num[idx] = id,ne[idx] = h[a], h[a] = idx ++ ;
}

void dfs(int u, int p, LL sum, int rec, int vis) {
	if(sum > total) return ;
	if(vis == n) {
		if(rec == C && total > sum) {
			total = sum;
			ans = path;
		}
		return ;
	}

	for(int i = h[u]; i != -1; i = ne[i]) {
		int j = e[i];
		if(j == p) continue;
		if(flag[i] == 1) {
			path.push_back(num[i]);
			dfs(j, u, sum + 0ll + w[i], rec + 1, vis + 1);
		} else {
			path.push_back(num[i]);
			dfs(j, u, sum + 0ll + w[i], rec, vis + 1);
//			path.pop_back();
		}
	}
}

void solve() {
	cin >> n >> m;

	memset(h, -1, sizeof h);
	for(int i = 0; i < m; i ++) {
		int u, v, w, p;
		cin >> u >> v >> w >> p;
		add(u, v, w, p, i + 1);
		add(v, u, w, p, i + 1);
		if(p == 1) C ++ ;
	}

	cout << -1 << endl;

//	dfs(1, 0, 0, 0, 0);

//	if(ans.size() == 0) cout << -1 << endl;
//	else {
//		cout << ans.size() << endl;
//		for(int x : ans) cout << x << ' ';
//		cout << endl;
//	}
}

int main() {
	cin.tie(0); cout.tie(0);
	std::ios::sync_with_stdio(false);

	int T = 1;
//	cin >> T;
	while(T --) {
		solve();
	}

	return 0;
}
相关推荐
靠近彗星35 分钟前
3.1 栈
数据结构·算法
sulikey1 小时前
一文彻底理解:如何判断单链表是否成环(含原理推导与环入口推算)
c++·算法·leetcode·链表·floyd·快慢指针·floyd判圈算法
Swift社区1 小时前
LeetCode 402 - 移掉 K 位数字
算法·leetcode·职场和发展
_码力全开_1 小时前
P1005 [NOIP 2007 提高组] 矩阵取数游戏
java·c语言·c++·python·算法·矩阵·go
墨染点香2 小时前
LeetCode 刷题【124. 二叉树中的最大路径和、125. 验证回文串】
算法·leetcode·职场和发展
Camel卡蒙2 小时前
红黑树详细介绍(五大规则、保持平衡操作、Java实现)
java·开发语言·算法
AhriProGramming2 小时前
Flask-SQLAlchemy精读-双语精选文章
python·算法·flask
孤廖2 小时前
吃透 C++ 栈和队列:stack/queue/priority_queue 用法 + 模拟 + STL 标准实现对比
java·开发语言·数据结构·c++·人工智能·深度学习·算法