Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n, m, d = map(int, input().split())
k = 1 + d // m
print((n + k - 1) // k)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
int sum = 0;
vector<int> a;
for (int i = 0; i < int(s.size()); ++i) {
int x = s[i] - '0';
sum += x;
a.push_back(x - (i == 0));
}
sort(a.begin(), a.end());
int ans = 0;
while (sum > 9) {
sum -= a.back();
a.pop_back();
++ans;
}
cout << ans << '\n';
}
}
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (FelixArg)
#include<bits/stdc++.h>
#define int long long
using namespace std;
bool get(int x, int a, int lim){
int f = 0;
for (int i = 59; i >= 0; i--){
f <<= 1;
if (((x >> i) & 1) == 1){
f++;
}
if (((a >> i) & 1) == 1){
f -= min(lim, f);
}
}
return (f == 0);
}
void solve(){
int x, a;
cin >> x >> a;
if (!get(x, a, 1ll << 60)){
cout << -1 << '\n';
return;
}
int l = 0, r = (1ll << 60);
while(l <= r){
int m = l + (r - l) / 2;
if (get(x, a, m)){
r = m - 1;
}
else{
l = m + 1;
}
}
cout << l << '\n';
}
signed main()
{
#ifdef FELIX
auto _clock_start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int tests = 1;
cin >> tests;
for (int test = 0; test < tests; test++){
solve();
}
#ifdef FELIX
cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
chrono::high_resolution_clock::now()
- _clock_start).count() << "ms." << endl;
#endif
return 0;
}
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (FelixArg)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
for (int test = 0; test < t; test++){
int n, m;
cin >> n >> m;
vector<int> a(n), b(m);
for (int i = 0; i < n; i++){
cin >> a[i];
}
for (int i = 0; i < m; i++){
cin >> b[i];
}
int max_a = n + m + 1;
vector<int> cnt_b(max_a, 0), cnt_a(max_a, 0);
for (int i = 0; i < n; i++){
cnt_a[a[i]]++;
}
for (int i = 0; i < m; i++){
cnt_b[b[i]]++;
}
vector<int> cnt_del(max_a, 0);
int f = 0, s = 0, com = 0;
for (int i = 1; i < max_a; i++){
for (int j = i; j < max_a; j += i){
cnt_del[j] += cnt_a[i];
}
}
for (int i = 1; i < max_a; i++){
if (cnt_b[i] == 0){
continue;
}
if (cnt_del[i] == 0){
s += cnt_b[i];
}
else if (cnt_del[i] == n){
f += cnt_b[i];
}
}
com = m - f - s;
bool turn = 0;
string win = "";
while(1){
if (turn == 0){
if (com > 0){
com--;
}
else if (f > 0){
f--;
}
else{
win = "Bob";
break;
}
}
else{
if (com > 0){
com--;
}
else if (s > 0){
s--;
}
else{
win = "Alice";
break;
}
}
turn = !turn;
}
cout << win << '\n';
}
return 0;
}
2203E - Probabilistic Card Game
Idea: BledDest
Tutorial
Tutorial is loading...
Solution 1 (awoo)
#include <bits/stdc++.h>
#include "ext/pb_ds/assoc_container.hpp"
using namespace __gnu_pbds;
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
const int MOD = 998244353;
int mul(int a, int b){
return a * 1ll * b % MOD;
}
int binpow(int a, int b){
int res = 1;
while (b){
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int m;
cin >> m;
vector<long long> a(m);
forn(i, m) cin >> a[i];
auto xs = a;
sort(xs.begin(), xs.end());
for (auto& x : a) x = lower_bound(xs.begin(), xs.end(), x) - xs.begin();
vector<long long> f(m);
auto upd = [&](int x, long long val){
for (int i = x; i < int(f.size()); i |= i + 1)
f[i] += val;
};
auto get = [&](int x){
long long sum = 0;
for (int i = x; i >= 0; i = (i & (i + 1)) - 1)
sum += f[i];
return sum;
};
long long sum = 0;
ordered_set<int> cur;
for (int x : a){
cur.insert(x);
int n = cur.size();
sum += xs[x];
upd(x, xs[x]);
if (n < 3) continue;
auto getL = [&](int pos, auto it){
if (it == cur.begin()) return 0ll;
return xs[*prev(it)] * 1ll * pos - get(*it - 1);
};
auto getR = [&](int pos, auto it){
if (next(it) == cur.end()) return 0ll;
return (sum - get(*it)) - xs[*next(it)] * 1ll * (n - pos - 1);
};
int l = 0, r = n - 1;
int pos = n;
while (l <= r){
int m = (l + r) / 2;
auto it = cur.find_by_order(m);
if (getL(m, it) >= getR(m, it)){
pos = m;
r = m - 1;
}
else{
l = m + 1;
}
}
long long ans = 1e18;
forn(_, 2){
if (0 <= pos && pos < n){
auto it = cur.find_by_order(pos);
ans = min(ans, max(getL(pos, it), getR(pos, it)));
}
--pos;
}
cout << mul(ans % MOD, binpow(n - 2, MOD - 2)) << '\n';
}
return 0;
}
Solution 2 (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
const long long INF64 = 1e18;
const int MOD = 998244353;
int mul(int a, int b){
return a * 1ll * b % MOD;
}
int binpow(int a, int b){
int res = 1;
while (b){
if (b & 1)
res = mul(res, a);
a = mul(a, a);
b >>= 1;
}
return res;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
int m;
cin >> m;
vector<long long> a(m);
int n = m + 2;
forn(i, m) cin >> a[i];
int p = sqrt(n) + 3;
auto xs = a;
xs.push_back(0);
sort(xs.begin(), xs.end());
xs.push_back(xs.back() + 1);
vector<int> cntbl(n), rgh(n, -1);
rgh[0] = 0;
rgh[(n - 1) / p] = n - 1;
vector<long long> sumbl(n);
vector<int> cll(n, 0), clr(n, n - 1);
set<int> on({0, n - 1});
int cnt = 0;
long long sum = 0;
forn(i, m){
int x = lower_bound(xs.begin(), xs.end(), a[i]) - xs.begin();
int bl = x / p;
++cntbl[bl];
sumbl[bl] += xs[x];
rgh[bl] = max(rgh[bl], x);
sum += xs[x];
++cnt;
auto it = on.lower_bound(x);
clr[x] = *it;
cll[x] = *prev(it);
clr[*prev(it)] = cll[*it] = x;
on.insert(x);
if (cnt < 3) continue;
int lst = clr[0];
int cntl = 0;
long long suml = 0;
auto getL = [&](int x){ return xs[cll[x]] * 1ll * cntl - suml; };
auto getR = [&](int x){ return (sum - suml - xs[x]) - xs[clr[x]] * 1ll * (cnt - cntl - 1); };
auto check = [&](int x){
if (x == 0) return true;
if (x == n - 1) return false;
return getL(x) < getR(x);
};
while (lst < n - 1){
int bl = lst / p;
assert(rgh[bl] != -1);
if (!check(rgh[bl])) break;
cntl += cntbl[bl];
suml += sumbl[bl];
lst = clr[rgh[bl]];
}
while (lst < n - 1){
if (!check(lst)) break;
++cntl;
suml += xs[lst];
lst = clr[lst];
}
x = lst;
long long ans = 1e18;
if (x != n - 1)
ans = min(ans, max(getL(x), getR(x)));
assert(x != 0);
x = cll[x];
if (x != 0){
--cntl;
suml -= xs[x];
ans = min(ans, max(getL(x), getR(x)));
}
cout << mul(ans % MOD, binpow(cnt - 2, MOD - 2)) << '\n';
}
return 0;
}
2203F - Binary Search with One Swap
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
vector<long long> ans;
int n;
vector<int> cnt;
void prepare_aux(int l, int r, vector<int>& a)
{
if(l > r) return;
int m = (l + r) / 2;
a[m] = r - l + 1;
if(l < m) prepare_aux(l, m - 1, a);
if(m < r) prepare_aux(m + 1, r, a);
}
vector<int> prepare(int len)
{
vector<int> a(len);
prepare_aux(0, len - 1, a);
return a;
}
int get_left(int len)
{
int m = (len - 1) / 2;
return m;
}
int get_right(int len)
{
int m = (len - 1) / 2;
return len - m - 1;
}
vector<long long> get(int x)
{
vector<long long> res(x + 1);
int m = (x - 1) / 2;
int left = m;
int right = x - m - 1;
vector<int> prepared = prepare(x);
//cout << x << endl;
// swap m with something to the left
{
for(int i = 0; i < m; i++)
{
int lost = abs(m - i);
//cout << i << " " << m << " " << lost << endl;
res[lost]++;
}
}
// swap m with something to the right
{
for(int i = 0; i < right; i++)
{
int lost = i + 1;
//cout << m << " " << i + m + 1 << " " << lost << endl;
res[lost]++;
}
}
// swap something to the left with something to the right
{
map<int, int> lost_left;
map<int, int> lost_right;
for(int i = 0; i < x; i++)
{
if(i == m) continue;
if(i < m)
{
int lost = get_right(prepared[i]) + 1;
lost_left[lost]++;
}
else
{
int lost = get_left(prepared[i]) + 1;
lost_right[lost]++;
}
}
for(auto p : lost_left)
for(auto p2 : lost_right)
{
//cerr << p.first << " " << p.second << endl;
//cerr << p2.first << " " << p2.second << endl;
res[p.first + p2.first] += p.second * 1ll * p2.second;
}
}
//cout << x << ":";
//for(auto y : dp[x]) cout << " " << y;
//cout << endl;
return res;
}
void calc(int l, int r)
{
if(l > r) return;
int m = (l + r) / 2;
if(l < m) calc(l, m - 1);
if(m < r) calc(m + 1, r);
int len = r - l + 1;
cnt[len]++;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n;
ans.resize(n + 1);
cnt.resize(n + 1);
calc(0, n - 1);
for(int i = 1; i <= n; i++)
if(cnt[i] != 0)
{
auto g = get(i);
for(int j = 0; j <= i; j++)
ans[n - j] += cnt[i] * g[j];
}
for(auto x : ans) cout << x << " ";
cout << "\n";
return 0;
}



