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;
}
相关推荐
hh随便起个名6 小时前
力扣二叉树的三种遍历
javascript·数据结构·算法·leetcode
Dingdangcat867 小时前
城市交通多目标检测系统:YOLO11-MAN-FasterCGLU算法优化与实战应用_3
算法·目标检测·目标跟踪
xie_pin_an8 小时前
深入浅出 C 语言数据结构:从线性表到二叉树的实战指南
c语言·数据结构·图论
tang&8 小时前
滑动窗口:双指针的优雅舞步,征服连续区间问题的利器
数据结构·算法·哈希算法·滑动窗口
拼命鼠鼠8 小时前
【算法】矩阵链乘法的动态规划算法
算法·矩阵·动态规划
LYFlied8 小时前
【每日算法】LeetCode 17. 电话号码的字母组合
前端·算法·leetcode·面试·职场和发展
式5169 小时前
线性代数(八)非齐次方程组的解的结构
线性代数·算法·机器学习
橘颂TA9 小时前
【剑斩OFFER】算法的暴力美学——翻转对
算法·排序算法·结构与算法
叠叠乐9 小时前
robot_state_publisher 参数
java·前端·算法
hweiyu0010 小时前
排序算法:冒泡排序
算法·排序算法