\(\rm Codeforces ~ Round ~ 757 ~ (Div. 2)\)
A Divan and a Store
There are \ (n \) items, and the price of the \ (I \) item is \ (a_i \).
Here are \ (L \) and \ (r \), which means that the merchant will only choose items with a price within \ ([l,r] \).
The merchant has \ (k \) yuan. Ask you how many goods the merchant can buy at most.
There are \ (T \) groups of data.
For the data of \ (100 \% \), ensure \ (1 \ Leq t \ Leq 100,1 \ Leq n \ Leq 100,1 \ Leq L \ Leq R \ Leq 10 ^ 9,1 \ Leq K \ Leq 10 ^ 9,1 \ Leq a_i \ Leq 10 ^ 9 \).
sol
Greedy, buy the remaining goods with the lowest price every time.
First, select the items that meet the conditions and put them in the priority queue (minimum priority).
Then pop up one by one and calculate the amount of money. If the amount of money \ (> k \) or the queue is empty, end the program.
Time complexity \ (\ mathcal O(Tn \log n) \).
#include <bits/stdc++.h> using namespace std; #define int long long int t; int n, l, r, k; signed main() { scanf("%lld", &t); while (t--) { scanf("%lld%lld%lld%lld", &n, &l, &r, &k); priority_queue<int, vector<int>, greater<int>> q; for (int i = 1, a; i <= n; ++i) { scanf("%lld", &a); if (a < l || a > r) continue; q.push(a); } int ans = 0, sum = 0; while (!q.empty()) { int now = q.top(); q.pop(); sum += now; // cout << now << "\n"; if (sum <= k) ans++; else break; } printf("%lld\n", ans); } return 0; }
B Divan and a New Project
On a street, there are \ (n+1 \) buildings. The coordinate of each building is \ (x_i \), which satisfies \ (\ forall j \ne i,x_i \ne x_j \) and \ (x_i \) is an integer.
Label all buildings from \ (0 \) to \ (n \).
There is a person in the building numbered \ (0 \) who has to go to the building numbered \ (I \) for \ (a_i \) times. The time spent by this person to and from the building numbered \ (I \) is \ (2 \times(|x_i - x_0|) \).
Please arrange the coordinates of each building to minimize the time spent by this person.
There are \ (T \) groups of data.
For data of \ (100 \% \), ensure \ (1\leq T \leq 10^3,1 \leq n \leq 2*10^5,0 \leq a_i \leq 10^6 \).
sol
First, we set the coordinates of the building numbered \ (0 \) as \ (0 \) for convenience.
Remember that the answer is \ (ans \), then \ (ans = \ sum \ limits {I = 1} ^ {n}{2 * a_i * | x_0-x_i|} \).
In order to minimize \ (ans \), the building with \ (a_i \) should be placed close to \ (x_0 \).
Then, sort from large to small according to \ (a_i \), and put the building in the order of left and right... In \ (x_0 \).
See the code for the specific implementation.
Time complexity \ (\ mathcal O(Tn) \).
#include <bits/stdc++.h> using namespace std; #define int long long const int _ = 2e5 + 7; int t; int n; struct abc { int v, id; } a[_]; int b[_]; int ans; bool cmp(abc a, abc b) { return a.v > b.v; } signed main() { scanf("%lld", &t); while (t--) { ans = 0; scanf("%lld", &n); for (int i = 1; i <= n; ++i) scanf("%lld", &a[i].v), a[i].id = i; sort(a + 1, a + n + 1, cmp); int k = 0; for (int i = 1; i <= n; i += 2) { b[a[i].id] = ++k; } k = 0; for (int i = 2; i <= n; i += 2) { b[a[i].id] = --k; } for (int i = 1; i <= n; ++i) { ans += 2 * a[i].v * abs(b[a[i].id]); } cout << ans << "\n"; cout << "0 "; for (int i = 1; i <= n; ++i) cout << b[i] << " "; cout << "\n"; } return 0; }
C Divan and bitwise operations
There is an unknown sequence \ (a \), and now \ (m \) pieces of information are known. Each star information is given in the form of l,r, x, indicating the bitwise or \ (x \) of the interval \ ([l,r] \), ensuring that each number in \ (a \) is included in the interval \ ([l,r] \) at least once.
Please output the XOR and \ (\ bmod (10^9+7) \) of all subsequences of sequence \ (a \).
There are \ (T \) groups of data.
For the data of \ (100 \% \), ensure \ (1\leq T \leq 10^3,1 \leq n,m \leq 2*10^5,1 \leq l \leq r \leq n,0 \leq x \leq 2^{30}-1 \).
sol
Obviously, we can get the bitwise or of the whole sequence, that is, the bitwise or of all \ (x \), set to \ (S \).
If the \ (i \) bit of \ (S \) is \ (0 \), the contribution is \ (0 \).
Otherwise, there is always a \ (1 \), in which exactly one corresponding contribution is \ (2^{i} \) and the total contribution is \ (2^{i}\times2^{n-1} \).
Then \ (Ans=S \times 2^{n-1} \).
Time complexity \ (\ mathcal O(Tn) \).
See the code for the specific implementation.
#include <bits/stdc++.h> #define int long long using namespace std; inline int read() { int s = 0, w = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') w = -1; for (; isdigit(c); c = getchar()) s = (s << 1) + (s << 3) + (c ^ 48); return s * w; } const int mod = 1e9 + 7; inline int qpow(int x, int y) { int res = 1; while (y) { if (y & 1) res = res * x % mod; x = x * x % mod; y >>= 1; } return res; } int T, n, m; signed main() { T = read(); while (T--) { n = read(), m = read(); int ans = 0; while (m--) { read(), read(); ans = ans | read(); } ans = ans * qpow(2, n - 1) % mod; printf("%lld\n", ans); } return 0; }
D Divan and Kostomuksha
Given the sequence \ (a_1,a_2,...,a_n \), it is required to rearrange \ (a \) so that
maximum.
Output this maximum value.
sol
Obviously, a dp.
Let \ (cnt_i \) represent the number of elements in the array \ (a \) that are multiples of \ (I \), \ (dp_i \) represent the maximum value that can be obtained by the arrangement with the end of the factor \ (I \).
There is a transfer equation
The final answer is the maximum value of \ (dp_i \) satisfying \ (cnt_i=n \).
Find \ (CNT \) to screen out all prime numbers, then enumerate \ (I \) and prime number set \ (pri \), and calculate \ (cnt_i \).
Time complexity \ (\ mathcal O(w \log \log w) \), where \ (w \) is the value range.
See the code for the specific implementation.
#include <bits/stdc++.h> using namespace std; #define int long long inline int read() { int x = 0, f = 1; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { x = x * 10 + c - '0'; c = getchar(); } return x * f; } const int _ = 2e7 + 1; int n; int ans; int cnt[_ + 7]; int dp[_ + 7]; bool vis[_]; vector<int> primes; void init() { for (int i = 2; i < _; ++i) { if (!vis[i]) primes.push_back(i); for (auto p : primes) { if (p * i > _) break; vis[p * i] = 1; if (i % p == 0) break; } } } signed main() { init(); n = read(); for (int i = 1, x; i <= n; ++i) cnt[read()]++; for (auto p : primes) for (int j = _ / p; j >= 1; --j) cnt[j] += cnt[j * p]; for (int i = _ - 1; i >= 1; --i) { dp[i] = cnt[i] * i; for (auto p : primes) { int j = p * i; if (j > _) break; dp[i] = max(dp[i], dp[j] + i * (cnt[i] - cnt[j])); } if (cnt[i] == n) ans = max(ans, dp[i]); } printf("%lld\n", ans); return 0; }