Idea: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
k = int(input())
a = list(map(int, input().split()))
if max(a) > 2 or a.count(2) > 1:
print('YES')
else:
print('NO')
2242B - Predominant Frequency Division
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (FelixArg)
#include <bits/stdc++.h>
using namespace std;
const int INF = 1'000'000'007;
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++){
cin >> a[i];
}
vector<int> pref1(n + 1);
vector<int> pref2(n + 1);
for (int i = 0; i < n; i++){
pref1[i + 1] = pref1[i] + (a[i] == 1 ? 1 : -1);
pref2[i + 1] = pref2[i] + (a[i] == 3 ? -1 : 1);
}
int mn = INF;
for (int i = 1; i < n; i++){
if (pref2[i] - mn >= 0){
cout << "YES\n";
return;
}
if (pref1[i] >= 0){
mn = min(mn, pref2[i]);
}
}
cout << "NO\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;
while(tests--){
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: BledDest
Tutorial
Tutorial is loading...
Solution (BledDest)
#include <bits/stdc++.h>
using namespace std;
void solve()
{
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int i = 0; i < n; i++)
{
cin >> a[i];
}
vector<int> lens;
int cur = 1;
for (int i = 1; i < n; i++)
if (a[i] != a[i - 1])
{
lens.push_back(cur);
cur = 1;
}
else cur++;
lens.push_back(cur);
sort(lens.begin(), lens.end());
int m = (int)lens.size();
int d = 0;
int ans = 0;
int i = 0;
while(i < m)
{
int len = lens[i];
int x = len - 1;
int q = m - i;
int curLen = n - d - x * q;
if (curLen <= k && (k - curLen) % q == 0)
ans++;
int j = i;
while (j < m && lens[j] == len)
j++;
d += (j - i) * lens[i];
i = j;
}
cout << ans << '\n';
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
return 0;
}
Idea: Roms
Tutorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
void upd(int& x, int y)
{
if(x < y) x = y;
}
void solve()
{
string s, t;
cin >> s >> t;
int n = s.size();
int m = t.size();
vector<int> ps(n + 1), pt(m + 1);
for(int i = 0; i < n; i++)
ps[i + 1] = (ps[i] + (s[i] - '0')) % 10;
for(int i = 0; i < m; i++)
pt[i + 1] = (pt[i] + (t[i] - '0')) % 10;
n++;
m++;
if(ps.back() != pt.back())
{
cout << -1 << "\n";
return;
}
vector<vector<int>> dp(n + 1, vector<int>(m + 1));
for(int i = 0; i <= n; i++)
for(int j = 0; j <= m; j++)
{
if(i < n) upd(dp[i + 1][j], dp[i][j]);
if(j < m) upd(dp[i][j + 1], dp[i][j]);
if(i < n && j < m && ps[i] == pt[j]) upd(dp[i + 1][j + 1], dp[i][j] + 1);
}
cout << dp[n][m] - 1 << "\n";
}
int main()
{
int t;
cin >> t;
for(int i = 0; i < t; i++)
solve();
return 0;
}
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
return out << "(" << p.x << ", " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
fore(i, 0, sz(v)) {
if(i) out << " ";
out << v[i];
}
return out;
}
const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
int l, r, n;
void solve() {
cin >> l >> r >> n;
int k = 63 - __builtin_clzll(r);
int x = -1, y = -1;
if (l >= (1ll << k)) {
int p = 30;
while (((l >> p) & 1) == ((r >> p) & 1))
p--;
assert(p >= 0);
x = l;
y = l - (l & ((1 << p) - 1)) + (1 << p);
}
else {
x = max(l, (1 << (k - 1)));
y = 1ll << k;
}
cerr << l << " " << r << ": " << x << " " << y << endl;
assert(l <= x && x < y && y <= r);
auto getBinary = [](int v) {
vector<int> rep;
while (v > 0) {
rep.push_back(v & 1);
v >>= 1;
}
reverse(rep.begin(), rep.end());
return rep;
};
auto x_bits = getBinary(x);
auto y_bits = getBinary(y);
string ans(n, '0');
fore (i, 0, n)
ans[i] = '0' + (x_bits[i % x_bits.size()] & y_bits[i % y_bits.size()]);
cout << ans << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int t; cin >> t;
while (t--) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (FelixArg)
#include <bits/stdc++.h>
using namespace std;
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int MX = 200000;
const int REBUILD_EVERY = 5000;
struct item {
int value, cnt;
int l, r;
item(): value(0), cnt(0), l(0), r(0) {}
item(int x): value(x), cnt(1), l(0), r(0) {}
};
vector<item> treap(1);
int copy(int v) {
treap.push_back(treap[v]);
return int(treap.size()) - 1;
}
int cnt(int v) {
return v ? treap[v].cnt : 0;
}
void upd_cnt(int v) {
if (v) {
treap[v].cnt = cnt(treap[v].l) + cnt(treap[v].r) + 1;
}
}
int merge(int l, int r) {
if (!l || !r) {
return l ? l : r;
}
int rang = rng() % (cnt(l) + cnt(r));
if (rang < cnt(l)) {
int res = copy(l);
treap[res].r = merge(treap[res].r, r);
upd_cnt(res);
return res;
} else {
int res = copy(r);
treap[res].l = merge(l, treap[res].l);
upd_cnt(res);
return res;
}
}
int find_first(int v) {
if (!treap[v].l) {
return treap[v].value;
} else {
return find_first(treap[v].l);
}
}
void dfs(int v) {
if (!v) {
return;
}
if (treap[v].l) {
dfs(treap[v].l);
}
cout << treap[v].value << ' ';
if (treap[v].r) {
dfs(treap[v].r);
}
}
pair<int, int> split(int t, int key, int add = 0) {
if (!t) {
return make_pair(0, 0);
}
t = copy(t);
int cur_key = add + cnt(treap[t].l);
if (key <= cur_key) {
auto [l, r] = split(treap[t].l, key, add);
treap[t].l = r;
r = t;
upd_cnt(t);
return {l, r};
} else {
auto [l, r] = split(treap[t].r, key, add + 1 + cnt(treap[t].l));
treap[t].r = l;
l = t;
upd_cnt(t);
return {l, r};
}
}
int change(int x, int it) {
auto [l, r] = split(it, x, 0);
auto [rl, rr] = split(r, x, 0);
auto [l1, r1] = split(it, MX - x, 0);
return merge(rl, l1);
}
void collect_values(int v, vector<int>& values) {
if (!v) {
return;
}
collect_values(treap[v].l, values);
values.push_back(treap[v].value);
collect_values(treap[v].r, values);
}
int build_balanced(const vector<int>& values, int l, int r) {
if (l >= r) {
return 0;
}
int m = (l + r) / 2;
treap.emplace_back(values[m]);
int v = int(treap.size()) - 1;
treap[v].l = build_balanced(values, l, m);
treap[v].r = build_balanced(values, m + 1, r);
upd_cnt(v);
return v;
}
int rebuild(int tr) {
vector<int> values;
values.reserve(cnt(tr));
collect_values(tr, values);
treap.clear();
treap.emplace_back();
return build_balanced(values, 0, int(values.size()));
}
void solve() {
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; i++) {
cin >> a[i];
}
vector<int> ans(n);
treap.reserve((size_t)((1 << 25) * 1.5));
int tr = 0;
for (int i = 0; i < MX; i++) {
treap.emplace_back(i);
tr = merge(tr, int(treap.size()) - 1);
}
int changed_iterations = 0;
for (int i = n - 1; i >= 0; i--) {
tr = change(a[i], tr);
ans[i] = find_first(tr);
changed_iterations++;
if (changed_iterations % REBUILD_EVERY == 0) {
tr = rebuild(tr);
}
}
for (int i = n - 1; i >= 0; i--) {
cout << ans[i] << ' ';
}
}
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;
while (tests--) {
solve();
}
#ifdef FELIX
cerr << "Executed in "
<< chrono::duration_cast<chrono::milliseconds>(
chrono::high_resolution_clock::now() - _clock_start
).count()
<< "ms."
<< endl;
#endif
return 0;
}










