美团笔试统计&题解 2022-08-06 后端&数开&软开
1.包装礼盒
2.划分数组
3.反转朝上数字个数大于一半
4.训练集测试集划分
5.拼接字符串中的第k个字符
#include <bits/stdc++.h> using namespace std; int fun(int x, int y) { if(x == 0 || y == 0) return 0; if(x == 1 && y == 1) return 0; if(x > y) swap(x, y); int z = y / 2; if(z >= x) { return x; } else { return (x + y) / 3; } } int main() { int T; cin >> T; while(T--) { int x, y; cin >> x >> y; cout << fun(x, y) << endl; } return 0; }2.前缀和、后缀和
#include <bits/stdc++.h> using namespace std; int a[100005]; int b[100005]; int c[100005]; int main() { int n; cin >> n; b[0] = c[n] = 0; for(int i = 0; i < n; i++) { cin >> a[i]; if(a[i] >= 0) { b[i + 1] = b[i] + 1; } else { b[i + 1] = b[i]; } } for(int i = n - 1; i >= 0; i--) { if(a[i] <= 0) { c[i] = c[i + 1] + 1; } else { c[i] = c[i + 1]; } } int ans = c[0]; for(int i = 0; i < n; i++) { ans = min(ans, b[i + 1] + c[i + 1]); } cout << ans << endl; return 0; }3.记录反面每个数出现的个数,即反转后是该数的个数,然后遍历正面每个数的情况并统计,特殊情况是,正面的数都不满足,就得再看看反面的
#include <bits/stdc++.h> using namespace std; int a[100005]; int b[100005]; int main() { int n; cin >> n; unordered_map<int, int>mp;//记录反转后是x的数个数 for(int i = 0; i < n; i++) { cin >> a[i]; } for(int i = 0; i < n; i++) { cin >> b[i]; if(b[i] != a[i]) { mp[b[i]]++; } } int target; if(n & 1) target = n / 2 + 1; else target = n / 2; sort(a, a + n); int ans = INT_MAX; for(int i = 0; i < n; i++) { int count = 0; int j = i; while(j < n && a[i] == a[j]) { count++; j++; } if(count >= target) { ans = 0; break; } if(mp[a[i]] >= target - count) { ans = min(ans, target - count); } i = j - 1; } if(ans != INT_MAX) cout << ans << endl; else { for(int i = 0; i < n; i++) {//正面的数字都不能满足要求,就只能看看能不能反转target个反面的数字 if(mp[b[i]] >= target) { ans = target; break; } } if(ans != INT_MAX) cout << ans << endl; else cout << -1 << endl; } return 0; }4.排序
#include <bits/stdc++.h> using namespace std; int a[10005]; int main() { int n, k; cin >> n >> k; vector<vector<int>>v(k); for(int i = 0; i < n; i++) { cin >> a[i]; v[a[i] - 1].push_back(i + 1); } vector<int>train; vector<int>test; for(int i = 0; i < k; i++) { int m = ceil(1.0 * v[i].size() / 2); for(int j = 0; j < m; j++) { train.push_back(v[i][j]); } for(int j = m; j < v[i].size(); j++) { test.push_back(v[i][j]); } } sort(train.begin(), train.end()); sort(test.begin(), test.end()); for(int i = 0; i < train.size(); i++) { if(i == train.size() - 1) cout << train[i] << endl; else cout << train[i] << " "; } for(int i = 0; i < test.size(); i++) { if(i == test.size() - 1) cout << test[i] << endl; else cout << test[i] << " "; } return 0; }5.二分+dfs,预处理所有的字符串长度,分前半部分、后半部分和wow分别处理
#include <bits/stdc++.h> using namespace std; vector<long long>count_; string s = "MeiTuan"; char dfs(long long k, long long cur_len) { // cout << k << endl; if(k <= s.size()) return s[k - 1]; // cout << count_[i] << endl; if(k == cur_len || k == cur_len - 2) return 'w'; if(k == cur_len - 1) return 'o'; long long next_len = (cur_len - 3) / 2; if(k > next_len) {//k在后半部分 k -= next_len; return dfs(next_len - k + 1, next_len); } return dfs(k, next_len); } int main() { long long x = 7; long long y = LLONG_MAX; while(true) { count_.push_back(x); if(x >= (y - 3) / 2) break; x = x * 2 + 3; } int T; cin >> T; while(T--) { long long k; cin >> k; int i = upper_bound(count_.begin(), count_.end(), k) - count_.begin(); long long cur_len = count_[i]; cout << dfs(k, cur_len) << endl; } return 0; }