字节5.6笔试 客户端题解
第一题
第三题
第四题, #实习##笔试题目##C/C++##字节跳动#
buff总共持续时间,新buff会重置旧buff
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n; cin >> n; vector<pair<int, int>> pa(n); for (int i = 0; i < n; ++i) { cin >> pa[i].first; } for (int i = 0, tmp; i < n; ++i) { cin >> tmp; pa[i].second = pa[i].first + tmp - 1; } sort(pa.begin(), pa.end()); int res = 0; int start = pa[0].first, last = pa[0].second; for (int i = 1; i < n; ++i) { if (last < pa[i].first) { res += last - start + 1; } else { res += pa[i].first - start; } start = pa[i].first; last = pa[i].second; } res += last - start + 1; cout << res; }
第二题
有效数字集,匹配数字前缀成功即有效
主要会有溢出问题,用long long #include <iostream> #include <vector> #include <unordered_set> using namespace std; using ll = long long ; int main () { int N; cin >> N; for (int i = 0; i < N; ++i) { unordered_set<ll> mset; int T; cin >> T; vector<ll> data(T); for (int j = 0; j < T; ++j) { cin >> data[j]; mset.insert(data[j]); } bool flag = false; for (int j = 0; j < T; ++j) { ll val = data[j] / 10; while (val) { if (mset.count(val)) { flag = true; break; } else { val /= 10; } } if (flag) break; } if (flag) cout << "YES" << endl; else cout << "NO" << endl; } }
完成两个任务,总共可支配时间m,给出n个任务,求能完成的最大价值
#include <iostream> #include <vector> #include <algorithm> using namespace std; int main() { int n, m; cin >> n >> m; vector<pair<int, int>> pa(n); for (int i = 0; i < n; ++i) { cin >> pa[i].second >> pa[i].first; //时间 价值 } sort(pa.rbegin(), pa.rend());//按照价值降序 int res = 0; for (int i = 0; i < n; ++i) { if (pa[i].second >= m) continue; int tmpSum = 0; for (int j = i + 1; j < n; ++j) { tmpSum = pa[i].first + pa[j].first; if (tmpSum <= res) break; if (pa[i].second + pa[j].second <= m) res = max(res, tmpSum); } } cout << res; }
第四题,
删除一个连续子数组,求新数组的最大严格上升子数组
只过了35,求大佬补充