diff --git a/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md b/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md index 471cf92a720b7..92936bcc0e371 100644 --- a/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md +++ b/solution/3400-3499/3464.Maximize the Distance Between Points on a Square/README.md @@ -119,6 +119,51 @@ tags: #### C++ ```cpp +uint32_t dists[15000]; +uint16_t nxt[15000]; + +class Solution { +public: + int maxDistance(int s, vector>& points, int k) { + const uint64_t n = points.size(), perim = 4UL * s; + + for (uint i = 0; i < n; i++) { + uint64_t x = points[i][0], y = points[i][1]; + dists[i] = (x == 0 || y == s) ? perim - x - y : x + y; + } + + sort(dists, dists + n); + + const auto check = [&](uint64_t x) -> bool { + for (uint i = 0, r = 0; i < n; i++) { + r = max(r, i + 1); + while (r < n && dists[r] < dists[i] + x) r++; + nxt[i] = r; + } + uint64_t maxreach = dists[n - 1] + perim - (k - 1) * x; + auto limit = perim - x; + + for (uint i = 0; i < n && dists[i] <= maxreach; i++) { + uint j = i; + for (uint r = k - 1; r > 0 && j != n; r--) { + j = nxt[j]; + } + if (j != n && dists[j] - dists[i] <= limit) return true; + } + return false; + }; + + uint64_t lo = 1, hi = perim / k; + while (lo < hi) { + auto x = (lo + hi + 1) / 2; + if (check(x)) + lo = x; + else + hi = x - 1; + } + return hi; + } +}; ``` diff --git a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md index 8ed17fe41e0e8..b2f5a189b3c45 100644 --- a/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md +++ b/solution/3500-3599/3514.Number of Unique XOR Triplets II/README.md @@ -97,7 +97,44 @@ tags: #### C++ ```cpp - +class Solution { +public: + int uniqueXorTriplets(vector& nums) { + const int SIZE = 2048; // since nums[i] <= 1500 < 2048, all XORs fit in 11 bits + + vector present(SIZE, false); + for (int v : nums) present[v] = true; + + vector distinct; + for (int v = 0; v < SIZE; v++) { + if (present[v]) distinct.push_back(v); + } + + // Step 1: compute all possible pairwise XORs (a ^ b) + vector pairXor(SIZE, false); + for (int a : distinct) { + for (int b : distinct) { + pairXor[a ^ b] = true; + } + } + + // Step 2: compute all possible triple XORs (s ^ c) where s is from pairXor set + vector tripleXor(SIZE, false); + for (int s = 0; s < SIZE; s++) { + if (!pairXor[s]) continue; + for (int c : distinct) { + tripleXor[s ^ c] = true; + } + } + + int count = 0; + for (int i = 0; i < SIZE; i++) { + if (tripleXor[i]) count++; + } + + return count; + } +}; ``` #### Go diff --git a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md index e201e656cae0e..df3c373f8b45d 100644 --- a/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md +++ b/solution/3500-3599/3518.Smallest Palindromic Rearrangement II/README.md @@ -121,6 +121,96 @@ tags: #### C++ ```cpp +long long comb(long long n, long long m, long long k) { + long long res = 1; + if (n - m < m) { + m = n - m; + } + + for (long long i = 1; i <= m; i++) { + res = res * (n - i + 1) / i; + if (res > k) { + return k + 1; + } + } + return res; +} + +long long permutations(int rem, int* bucket, long long k) { + long long ways = 1; + for (int i = 0; i < 26; i++) { + if (bucket[i] == 0) { + continue; + } + + ways *= comb(rem, bucket[i], k); + if (ways > k) { + break; + } + rem -= bucket[i]; + } + return ways; +} + +char* smallestPalindrome(char* s, long long k) { + int len = strlen(s); + int partition = len / 2; + int bucket[26] = {0}; + + for (int i = 0; i < partition; i++) { + bucket[s[i] - 'a'] += 1; + } + + char* left = (char*)malloc(partition + 1); + int left_idx = 0; + long long start_index = 1; + + for (int pos = 0; pos < partition; pos++) { + for (int i = 0; i < 26; i++) { + if (bucket[i] == 0) { + continue; + } + + bucket[i] -= 1; + + long long ways = permutations(partition - pos - 1, bucket, k); + if (start_index + ways > k) { + left[left_idx++] = i + 'a'; + break; + } + + bucket[i] += 1; + start_index += ways; + } + } + left[left_idx] = '\0'; + + if (left_idx < partition) { + char* empty_res = (char*)malloc(1); + empty_res[0] = '\0'; + free(left); + return empty_res; + } + + char* result = (char*)malloc(len + 1); + int res_idx = 0; + + for (int i = 0; i < partition; i++) { + result[res_idx++] = left[i]; + } + + if (len % 2 != 0) { + result[res_idx++] = s[partition]; + } + + for (int i = partition - 1; i >= 0; i--) { + result[res_idx++] = left[i]; + } + result[res_idx] = '\0'; + + free(left); + return result; +} ``` diff --git a/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md b/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md index b4814d548e947..e712e3b1b7371 100644 --- a/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md +++ b/solution/3600-3699/3614.Process String with Special Operations II/README_EN.md @@ -229,6 +229,45 @@ tags: #### C++ ```cpp +class Solution { +public: + char processStr(string s, long long k) { + long long len = 0; + for (char c : s) { + if (islower(c)) { + len++; + } + else if (c == '*') { + if (len > 0) len--; + } + else if (c == '#') { + len *= 2; + } + else if (c == '%') {} + } + if (k >= len) return '.'; + for (int i = s.size() - 1; i >= 0; i--) { + char c = s[i]; + if (islower(c)) { + if (k == len - 1) { + return c; + } + len--; + } + else if (c == '*') { + len++; + } + else if (c == '#') { + len /= 2; + k %= len; + } + else if (c == '%') { + k = len - 1 - k; + } + } + return '.'; + } +}; ``` diff --git a/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp b/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp new file mode 100644 index 0000000000000..6f1adf5cfd13b --- /dev/null +++ b/solution/3600-3699/3655.XOR After Range Multiplication Queries II/solution.cpp @@ -0,0 +1,79 @@ +#include +using namespace std; + +class Solution { + static constexpr int MOD = 1000000007; + + long long modpow(long long a, long long e) { + long long r = 1 % MOD; + a %= MOD; + while (e > 0) { + if (e & 1) r = (r * a) % MOD; + a = (a * a) % MOD; + e >>= 1; + } + return r; + } + +public: + int xorAfterQueries(vector& nums, vector>& queries) { + int n = nums.size(); + int B = std::sqrt(n) + 1; + + vector>>> events(B + 1); + for (int k = 1; k <= B; ++k) + events[k].resize(k); + + for (auto &qq : queries) { + int l = qq[0], r = qq[1], k = qq[2], v = qq[3]; + if (k > B) { + for (int idx = l; idx <= r; idx += k) + nums[idx] = (long long)nums[idx] * v % MOD; + } else { + int res = l % k; + int t1 = (l - res) / k; + int t2 = (r - res) / k; + events[k][res].push_back({t1, v}); + + if (t2 + 1 <= (n - 1 - res) / k) { + int invv = modpow(v, MOD - 2); + events[k][res].push_back({t2 + 1, invv}); + } + } + } + + for (int k = 1; k <= B; ++k) + for (int res = 0; res < k; ++res) { + auto &ev = events[k][res]; + if (ev.empty()) + continue; + + sort(ev.begin(), ev.end()); + vector> comp; + + for (auto &p : ev) { + if (!comp.empty() && comp.back().first == p.first) + comp.back().second = (long long)comp.back().second * p.second % MOD; + else + comp.push_back(p); + } + + long long cur = 1; + int ptr = 0; + int t = 0; + for (int idx = res; idx < n; idx += k, ++t) { + while (ptr < comp.size() && comp[ptr].first == t) { + cur = (cur * comp[ptr].second) % MOD; + ++ptr; + } + nums[idx] = nums[idx] * cur % MOD; + } + } + + int xr = 0; + for (int x : nums) + xr ^= x; + + return xr; + } +}; \ No newline at end of file diff --git a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md index 4ab82d839a117..72b97d1aa5cfb 100644 --- a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md +++ b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/README.md @@ -156,6 +156,83 @@ tags: #### C++ ```cpp +class Solution { +public: + using ll = long long; + using t3 = tuple; + ll enc(ll a, ll b, ll c) + { + return (a << 26) + (b << 15) + (c); + } + t3 dec(ll l) + { + ll c = l & ((1 << 15) - 1); + ll a = l >> 26; + ll b = (l >> 15) & ((1 << 11) - 1); + return {a, b, c}; + } + long long minMergeCost(vector>& lists) { + int n = lists.size(); + int m = 1 << n; + + vector vals; + for(auto& v: lists) + { + for(auto x: v) vals.push_back(x); + } + sort(vals.begin(), vals.end()); + vals.erase(unique(vals.begin(), vals.end()), vals.end()); + + vector cnt(m), med(m); + for(int i = 1; i < m; ++i) + { + for(int j = 0; j < n; ++j) + { + if((i >> j) & 1) cnt[i] += lists[j].size(); + } + } + for(int mask = 1; mask < m; ++mask) + { + ll need = (cnt[mask] + 1) / 2; + int l = 0, r = vals.size() - 1; + + while(l < r) + { + int mid = (l + r) / 2; + ll le = 0; + + for(int b = mask; b; b &= b - 1) + { + int id = __builtin_ctz(b); + le += upper_bound(lists[id].begin(), lists[id].end(), vals[mid]) - lists[id].begin(); + if(le >= need) break; + } + + if(le >= need) r = mid; + else l = mid + 1; + } + + med[mask] = vals[l]; + } + vector dp(m, LLONG_MAX); + for(int i = 1; i < m; ++i) + { + if(__builtin_popcount(i) == 1) + { + dp[i] = 0; + continue; + } + for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) + { + int k = (i ^ j) & i; + dp[i] = min(dp[i], dp[j] + dp[k] + abs(med[j] - med[k])); + } + dp[i] += cnt[i]; + //cout << i << " " << cnt[i] << " " << dp[i] << endl; + } + return dp[m - 1]; + } +}; ``` diff --git a/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp new file mode 100644 index 0000000000000..61f22b375a08e --- /dev/null +++ b/solution/3800-3899/3801.Minimum Cost to Merge Sorted Lists/solution.cpp @@ -0,0 +1,77 @@ +class Solution { +public: + using ll = long long; + using t3 = tuple; + ll enc(ll a, ll b, ll c) + { + return (a << 26) + (b << 15) + (c); + } + t3 dec(ll l) + { + ll c = l & ((1 << 15) - 1); + ll a = l >> 26; + ll b = (l >> 15) & ((1 << 11) - 1); + return {a, b, c}; + } + long long minMergeCost(vector>& lists) { + int n = lists.size(); + int m = 1 << n; + + vector vals; + for(auto& v: lists) + { + for(auto x: v) vals.push_back(x); + } + sort(vals.begin(), vals.end()); + vals.erase(unique(vals.begin(), vals.end()), vals.end()); + + vector cnt(m), med(m); + for(int i = 1; i < m; ++i) + { + for(int j = 0; j < n; ++j) + { + if((i >> j) & 1) cnt[i] += lists[j].size(); + } + } + for(int mask = 1; mask < m; ++mask) + { + ll need = (cnt[mask] + 1) / 2; + int l = 0, r = vals.size() - 1; + + while(l < r) + { + int mid = (l + r) / 2; + ll le = 0; + + for(int b = mask; b; b &= b - 1) + { + int id = __builtin_ctz(b); + le += upper_bound(lists[id].begin(), lists[id].end(), vals[mid]) - lists[id].begin(); + if(le >= need) break; + } + + if(le >= need) r = mid; + else l = mid + 1; + } + + med[mask] = vals[l]; + } + vector dp(m, LLONG_MAX); + for(int i = 1; i < m; ++i) + { + if(__builtin_popcount(i) == 1) + { + dp[i] = 0; + continue; + } + for(int j = (i - 1) & i; j > 0; j = (j - 1) & i) + { + int k = (i ^ j) & i; + dp[i] = min(dp[i], dp[j] + dp[k] + abs(med[j] - med[k])); + } + dp[i] += cnt[i]; + //cout << i << " " << cnt[i] << " " << dp[i] << endl; + } + return dp[m - 1]; + } +};