字节笔试9.18后端
自己简单记录一下
1.堆金字塔,用二分优化了一下,AC
#include <iostream> #include <vector> #include <set> using namespace std; vector<double> v; int main() { int n, m, ret = 0; double x; scanf("%d%d", &n, &m); for(int i = 0; i < m; ++i) { scanf("%lf", &x); v.push_back(x); } ret += m; for(int i = 1; i < n; ++i) { scanf("%d", &m); vector<double> t; for(int j = 0; j < m; ++j) { scanf("%lf", &x); int f = 0, cnt = 0, l = 0, r = v.size() - 1; set<int> s; while(l <= r) { int mid = l + r >> 1; if(v[mid] > x - 50 && v[mid] < x + 50) { f = 1; break; } if(v[mid] > x - 100 && v[mid] < x + 100) { s.insert(mid); l++; } else if(v[mid] <= x - 100) l = mid + 1; else r = mid - 1; } l = 0, r = v.size() - 1; while(l <= r) { int mid = l + r >> 1; if(v[mid] > x - 50 && v[mid] < x + 50) { f = 1; break; } if(v[mid] > x - 100 && v[mid] < x + 100) { s.insert(mid); r--; } else if(v[mid] <= x - 100) l = mid + 1; else r = mid - 1; } if(f || s.size() > 1) { ret++; t.push_back(x); } } v = t; } printf("%d", ret); }
2.神奇序列,AC
#include <iostream> using namespace std; int main() { string s; cin >> s; int len = 1, ans = 0; for(int i = 1; i < s.length(); ++i) { if(s[i] == s[i - 1]) { len = 1; } else { len++; if(len > 2) ans = max(ans, len); } } cout << ans; }
3.ASDF,滑动窗口划过去,有个样例没过,但一交AC了
#include <iostream> #include <vector> #include <map> using namespace std; map<char, int> m, m1; vector<char> v; int main() { string s; cin >> s; for(int i = 0; i < s.length(); ++i) m[s[i]]++; int cnt = s.length() / 4; for(auto it = m.begin(); it != m.end(); ++it) if(it->second > cnt) v.push_back(it->first); int l = 0, r = 0, len = 1e7; for(int i = 0; i < s.length(); ++i) { int f = 1; m1[s[i]]++; for(int j = 0; j < v.size(); ++j) if(m1[v[j]] < m[v[j]] - cnt) f = 0; if(f == 0) r++; else { len = min(len, r - l + 1); l++; while(l <= r) { f = 1; m1[s[l - 1]]--; for(int j = 0; j < v.size(); ++j) if(m1[v[j]] < m[v[j]] - cnt) f = 0; if(f == 0) { r++; break; } else len = min(len, r - l + 1); l++; } } } cout << len; }
4.做书架,不知道用啥,暴力了...
#字节笔试##字节跳动23秋招笔试心得体会#