Given a permutation, find the number of pairs of continuous subarrays of a given lengthm such that the left one is element-wise smaller than the right one, i.e. every element in the left subarray is strictly less than the corresponding element in the right subarray.n⩽5×104。
构造矩阵 Aij=ai<ai+j,统计所有连续m 行之 AND 的 POPCOUNT 之和,复杂度O(wn2)。具体实现颇具技巧:
constexprint N = 5e4; intmain(){ int n, m; cin >> n >> m; vector<int> pos(n); for (int i = 0; i < n; ++i) { int x; cin >> x; pos[--x] = i; } bitset<N> tmp; vector<bitset<N>> A(n); for (int x = n - 1; x >= 0; x--) { A[pos[x]] = tmp >> pos[x]; tmp[pos[x]] = 1; } auto pre = A, suf = A; for (int i = 0; i < n; i++) { if (i % m) pre[i] &= pre[i - 1]; } for (int i = n - 1; i >= 0; i--) { if (i + 1 < n && (i + 1) % m) suf[i] &= suf[i + 1]; } ll res = 0; for (int i = 0; i + m <= n; i++) { if (i % m == 0) { res += suf[i].count(); } else { res += (suf[i] & pre[i + m - 1]).count(); } } cout << res << endl; }
intmain(){ int n, m; cin >> n >> m; map<int, vector<int>> adj; for (int i = 0; i < n; i++) { int x; cin >> x; adj[x].push_back(i); } string s; cin >> s; map<int, array<int, 2>> st; // l, [op, cnt] int l = -1; multiset<int> lens; for (int i = 0; i < s.size(); i++) { if (s[i] == '0') { if (l != -1) { st[l] = { 1, i - l }; lens.insert(i - l); } l = -1; st[i] = { 0, 1 }; } elseif (s[i] == '1') { if (l == -1) l = i; } } if (l != -1) { st[l] = { 1, n - l }; lens.insert(n - l); } int res = 0; if (!lens.empty() && *lens.rbegin() >= m) { res = max(res, adj.upper_bound(0)->first); } for (auto [k, vec] : adj) { for (auto id : vec) { if (s[id] == '0') { auto it = st.lower_bound(id); auto L = prev(it); auto R = next(it); if (it != st.begin() && R != st.end() && L->second[0] == 1 && R->second[0] == 1) { auto l = it->first; auto [op, cnt] = it->second; auto l1 = L->first; auto [op1, cnt1] = L->second; auto l2 = R->first; auto [op2, cnt2] = R->second; st.erase(it); st.erase(L); st.erase(R); st[l1] = { 1, cnt1 + cnt2 }; lens.extract(cnt1); lens.extract(cnt2); lens.insert(cnt1 + cnt2); } else { st.erase(it); } } else { auto it = st.upper_bound(id); it--; auto l = it->first; auto [op, cnt] = it->second; lens.extract(cnt); if (cnt - 1) { st[l] = { op, cnt - 1 }; lens.insert(cnt - 1); } } } if (!lens.empty() && *lens.rbegin() >= m) { res = max(res, adj.upper_bound(k)->first); } } cout << res << endl;
Two random sets of exactlyn positive integers with the same sum109 are generated (a uniform distribution is taken over all sets with sum109). You are givenN=2n generated elements in random order. Your task is to select some of the given numbers so that the sum of the selected numbers is exactly109.N⩽100.
Thus, this is not the traditional concern of “two random numbers coincidentally hashing to the same value,” but rather the situation where “given a huge amount of numbers, it is almost certain to find one that hashes to the target value.” 这不是传统意义上地担心“两个随机数哈希值相同”,而是“从巨量的数中,几乎必然能找到两个哈希值相同的数”。
intmain(){ string s; map<int, vector<int>> mp; while (getline(cin, s)) { stringstream ss(s); string t; ss >> t; t = t.substr(3); if (t.back() == ':') t.pop_back(); // cout << t << endl; int x = stoi(t); while (ss >> t) { t = t.substr(3); if (t.back() == ',') t.pop_back(); // cout << t << endl; int y = stoi(t); mp[y].push_back(x); } // cout << endl; }
for (auto& [k, vec] : mp) { cout << "CS-" << k << ": "; ranges::sort(vec); for (int i = 0; i < vec.size(); i++) { cout << "CS-" << vec[i]; if (i == vec.size() - 1) { cout << endl; } else { cout << ", "; } } } return0; }