classSolution { public: boolcheckAlmostEquivalent(string w1, string w2){ unordered_map<char, int> m1, m2; for (auto& c : w1) m1[c] ++ ; for (auto& c : w2) m2[c] ++ ; for (char c = 'a'; c <= 'z'; c ++ ) { int d = abs(m1[c] - m2[c]); if (d > 3) returnfalse; } returntrue; } };
constint dx[4] = {0, 1, 0, -1}, dy[4] = {1, 0, -1, 0}; classRobot { public: map<int, string> mp; int idx; int x, y; int W, H; // 周长 int all; Robot(int width, int height) { W = width, H = height; x = 0, y = 0; idx = 1; mp[0] = "North"; mp[1] = "East"; mp[2] = "South"; mp[3] = "West"; all = 2 * (W + H) - 4; } boolcheck(int x, int y){ return x >= 0and x < W and y >= 0and y < H; } voidmove(int num){ num %= all; if (num == 0) { if (x == 0and y == 0) { if (getDir() == "North") idx = 3; else idx = 2; } if (x == 0and y == H - 1) { if (getDir() == "East") idx = 0; else idx = 3; } if (x == W - 1and y == 0) { if (getDir() == "West") idx = 2; else idx = 1; } if (x == W - 1and y == H - 1) { if (getDir() == "South") idx = 1; else idx = 0; } } string c = getDir(); if (c == "North") { int Mx = H - 1 - y; if (Mx >= num) { y += num; return ; } else { y = H - 1; idx = 3; returnmove(num - Mx); } } if (c == "South") { int Mx = y; if (Mx >= num) { y -= num; return ; } else { y = 0; idx = 1; returnmove(num - Mx); } } if (c == "West") { int Mx = x; if (Mx >= num) { x -= num; return ; } else { x = 0; idx = 2; returnmove(num - Mx); } } if (c == "East") { int Mx = W - x - 1; if (Mx >= num) { x += num; return ; } else { x = W - 1; idx = 0; returnmove(num - Mx); } } for (int i = 1; i <= num; ) { int nx = x + dx[idx], ny = y + dy[idx]; if (check(nx, ny)) { i += 1; x = nx, y = ny; continue; } idx -= 1; if (idx == -1) idx = 3; } } vector<int> getPos(){ return {x, y}; } string getDir(){ return mp[idx]; } };
/** * Your Robot object will be instantiated and called as such: * Robot* obj = new Robot(width, height); * obj->move(num); * vector<int> param_2 = obj->getPos(); * string param_3 = obj->getDir(); */
// 在线算法. 注意树状数组的范围要开到题目范围的两倍(查询点 + 价格点). constint N = 2e5 + 5; classSolution { public: int tr[N], M; intlowbit(int x){ return x & -x; } voidadd(int x, int c){ for (int i = x; i <= M; i += lowbit(i)) tr[i] = max(tr[i], c); } intquery(int x){ int res = 0; for (int i = x; i; i -= lowbit(i)) res = max(res, tr[i]); return res; } vector<int> maximumBeauty(vector<vector<int>>& nums, vector<int>& qu){ int n = nums.size(); vector<int> all = qu; for (auto& c : nums) all.push_back(c[0]); sort(all.begin(), all.end()); all.erase(unique(all.begin(), all.end()), all.end()); M = all.size(); auto get = [&] (int x) { returnlower_bound(all.begin(), all.end(), x) - all.begin() + 1; }; for (auto& c : nums) { int idx = get(c[0]), val = c[1]; add(idx, val); } vector<int> ans; for (auto& q : qu) { int idx = get(q); int cur = query(idx); ans.push_back(cur); } return ans; } };
// 上述贪心方式 classSolution { public: intmaxTaskAssign(vector<int>& task, vector<int>& work, int cnt, int sth){ sort(task.begin(), task.end()); sort(work.begin(), work.end()); int n = task.size(), m = work.size(); int l = 0, r = min(n, m);
auto check = [&] (int x) { int need = 0; multiset<int> st; for (int i = 0; i < x; i ++ ) st.insert(task[i]);
for (int i = m - x; i < m; i ++ ) { int cur = work[i]; if (cur >= *st.begin()) { st.erase(st.begin()); continue; } auto idx = st.lower_bound(cur + sth + 1); if (idx == st.begin()) returnfalse; -- idx; st.erase(idx); need += 1; } return need <= cnt; }; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } return r; } };
// 题解区大佬的解法, 从大到小枚举任务, 贪心的选工人去完成它. classSolution { public: intmaxTaskAssign(vector<int>& task, vector<int>& work, int cnt, int sth){ sort(task.begin(), task.end()); sort(work.begin(), work.end()); int n = task.size(), m = work.size(); int l = 0, r = min(n, m);
auto check = [&] (int x) { int need = 0; multiset<int> st; for (int i = m - x; i < m; i ++ ) st.insert(work[i]); for (int i = x - 1; i >= 0; i -- ) { auto it = st.lower_bound(task[i]); if (it != st.end()) { st.erase(it); continue; } need ++ ; it = st.lower_bound(task[i] - sth); if (it == st.end()) returnfalse; st.erase(it); } return need <= cnt; }; while (l < r) { int mid = (l + r + 1) >> 1; if (check(mid)) l = mid; else r = mid - 1; } return r; } };