Skip to content

Commit 62f24d5

Browse files
Add files via upload
1 parent 149d4fe commit 62f24d5

File tree

3 files changed

+179
-0
lines changed

3 files changed

+179
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
using ll = long long;
7+
using vi = vector<int>;
8+
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
9+
10+
11+
void solve() {
12+
set<pair<ll, ll>> s; // (max_value, index)
13+
int n;
14+
cin >> n;
15+
16+
vector<ll> a(n);
17+
vector<ll> nxt(n);
18+
vector<ll> prv(n);
19+
for (int i = 0; i < n; i++) {
20+
cin >> a[i];
21+
nxt[i] = (i + 1) % n;
22+
prv[i] = (i - 1 + n) % n;
23+
}
24+
25+
for (int i = 0; i < n; i++) {
26+
s.insert({max(a[i], a[nxt[i]]), i});
27+
}
28+
29+
ll ans = 0;
30+
while (s.size() > 1) {
31+
auto [val, idx] = *s.begin();
32+
s.erase(s.begin());
33+
ans += val;
34+
35+
// remove the left one
36+
pair<ll, ll> left_pair = {max(a[prv[idx]], a[idx]), prv[idx]};
37+
s.erase(left_pair);
38+
39+
// now update with new pair
40+
nxt[prv[idx]] = nxt[idx];
41+
prv[nxt[idx]] = prv[idx];
42+
a[nxt[idx]] = val;
43+
pair<ll, ll> new_pair = {max(a[prv[idx]], a[nxt[idx]]), prv[idx]};
44+
s.insert(new_pair);
45+
}
46+
47+
cout << ans << '\n';
48+
}
49+
50+
int main(){
51+
int tc; cin >> tc; while (tc--) solve();
52+
53+
return 0;
54+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#pragma GCC optimize("Ofast")
2+
#pragma GCC optimize("unroll-loops")
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
using ll = long long;
7+
using vi = vector<int>;
8+
#define fast_cin() \
9+
ios_base::sync_with_stdio(false); \
10+
cin.tie(NULL); \
11+
cout.tie(NULL);
12+
13+
int n;
14+
ll maxi;
15+
16+
constexpr ll MOD = 998244353;
17+
vector<ll> arr;
18+
vector<vector<ll>> memo;
19+
ll dp(int idx, ll taken) {
20+
if (idx >= n) {
21+
if (taken < maxi)
22+
return 0;
23+
else
24+
return 1;
25+
}
26+
27+
if (memo[idx][taken] != -1) return memo[idx][taken];
28+
ll ans = 0;
29+
ans += dp(idx + 1, taken);
30+
ans %= MOD;
31+
ans += (arr[idx] * dp(idx + 1, taken + arr[idx])) % MOD;
32+
ans %= MOD;
33+
34+
return memo[idx][taken] = ans;
35+
}
36+
void solve() {
37+
cin >> n;
38+
arr.assign(n, 0);
39+
maxi = 0;
40+
for (int i = 0; i < n; i++) {
41+
int x;
42+
cin >> x;
43+
arr[x-1]++;
44+
maxi = max(maxi, arr[x-1]);
45+
}
46+
memo.assign(n + 1, vector<ll>(n + 1, -1));
47+
cout << dp(0, 0) % MOD << '\n';
48+
}
49+
50+
int main() {
51+
int tc;
52+
cin >> tc;
53+
while (tc--) solve();
54+
55+
return 0;
56+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include <queue>
2+
#pragma GCC optimize("Ofast")
3+
#pragma GCC optimize("unroll-loops")
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
using ll = long long;
8+
using vi = vector<int>;
9+
#define fast_cin() \
10+
ios_base::sync_with_stdio(false); \
11+
cin.tie(NULL); \
12+
cout.tie(NULL);
13+
14+
void solve() {
15+
int n, q;
16+
cin >> n >> q;
17+
vector<int> arr(n);
18+
19+
for (int i = 0; i < n; i++) {
20+
cin >> arr[i];
21+
}
22+
sort(arr.begin(), arr.end(), greater<int>());
23+
24+
while (q--) {
25+
int x;
26+
cin >> x;
27+
28+
priority_queue<int> pq;
29+
while (!pq.empty()) pq.pop();
30+
for (int i = 0; i < min(n, 32); i++) { // at most 32 needed
31+
pq.push(arr[i]);
32+
}
33+
34+
int cur = x;
35+
int count = 0;
36+
while (cur > 0) {
37+
int ref = (pq.empty()) ? 0 : pq.top();
38+
if (!pq.empty()) {
39+
pq.pop();
40+
}
41+
42+
if (cur <= ref) {
43+
break;
44+
} else if (ref == 0) {
45+
count += cur;
46+
break;
47+
} else if (__builtin_clz(ref) > __builtin_clz(cur)) {
48+
int target = (1 << (32 - __builtin_clz(cur) - 1));
49+
count += (target - ref);
50+
cur -= target;
51+
} else {
52+
assert(__builtin_clz(cur) == __builtin_clz(ref));
53+
int part = (1 << (31 - __builtin_clz(ref)));
54+
cur ^= part;
55+
pq.emplace(ref ^ part);
56+
}
57+
}
58+
59+
cout << count << endl;
60+
}
61+
}
62+
63+
int main() {
64+
int tc;
65+
cin >> tc;
66+
while (tc--) solve();
67+
68+
return 0;
69+
}

0 commit comments

Comments
 (0)