Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, a, b, c;
cin >> t;
while (t--) {
cin >> a >> b >> c;
cout << (a + c) % 2 << '\n';
}
return 0;
}
1582B - Luntik and Subsequences
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n, x;
cin >> t;
while (t--) {
cin >> n;
int cnt0 = 0, cnt1 = 0;
for (int i = 1; i <= n; ++i) {
cin >> x;
if (x == 0) cnt0++;
if (x == 1) cnt1++;
}
cout << (1ll << cnt0) * (ll)cnt1 << '\n';
}
return 0;
}
1582C - Grandma Capa Knits a Scarf
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n;
cin >> t;
while (t--) {
string s;
cin >> n >> s;
int ans = n + 1;
for (int c = 0; c < 26; ++c) {
int l = 0, r = n - 1, cnt = 0;
while (l <= r) {
if (s[l] == s[r]) {
l++, r--;
}
else if (s[l] == char('a' + c)) {
cnt++, l++;
}
else if (s[r] == char('a' + c)) {
cnt++, r--;
}
else {
cnt = n + 1;
break;
}
}
ans = min(ans, cnt);
}
if (ans == n + 1) ans = -1;
cout << ans << '\n';
}
return 0;
}
Idea: AlFlen
Tutorial
Tutorial is loading...
Solution (AlFlen)
ttt = int(input())
for t in range(ttt):
n = int(input())
a = [int(x) for x in input().split()]
start = 0
if n % 2 == 1:
if (a[0] + a[1] != 0):
print(-a[2], -a[2], a[0] + a[1], end = " ")
elif (a[1] + a[2] != 0):
print(a[2] + a[1], -a[0], -a[0], end = " ")
else:
print(-a[1], a[0] + a[2], -a[1], end = " ")
start = 3
while start < n:
print(-a[s + 1], a[s], end = " ")
start += 2
print()
1582E - Pchelyonok and Segments
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 1e5 + 5, maxk = 450;
int a[maxn], dp[maxn][maxk];
int inf = 1e9 + 7;
ll pref[maxn];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t, n;
cin >> t;
while (t--) {
cin >> n;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
int k = 0;
while (k * (k + 1) / 2 <= n) k++;
for (int j = 0; j < k; ++j) {
dp[n + 1][j] = -inf;
}
dp[n + 1][0] = inf;
for (int i = n; i >= 1; --i) {
for (int j = 0; j < k; ++j) {
dp[i][j] = dp[i + 1][j];
if (j && i + j - 1 <= n && pref[i + j - 1] - pref[i - 1] < dp[i + j][j - 1]) {
dp[i][j] = max(dp[i][j], (int)(pref[i + j - 1] - pref[i - 1]));
}
}
}
int ans = 0;
for (int j = 0; j < k; ++j) {
if (dp[1][j] > 0) ans = j;
}
cout << ans << '\n';
}
return 0;
}
1582F2 - Korney Korneevich and XOR (hard version)
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
int const max_value = (1 << 13);
vector < int > g[max_value];
int ans[max_value], r[max_value];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n, x;
cin >> n;
ans[0] = 1;
for (int i = 0; i < max_value; ++i) {
g[i].push_back(0);
}
for (int i = 0; i < max_value; ++i) {
r[i] = max_value;
}
for (int i = 1; i <= n; ++i) {
cin >> x;
for (auto key : g[x]) {
int to = (key^x);
ans[to] = 1;
while (r[to] > x) {
r[to]--;
if (r[to] != x) g[r[to]].push_back(to);
}
}
g[x] = {};
}
int k = 0;
for (int i = 0; i < max_value; ++i) {
k += ans[i];
}
cout << k << '\n';
for (int i = 0; i < max_value; ++i) {
if (ans[i]) cout << i << " ";
}
cout << '\n';
return 0;
}
Idea: 74TrAkToR
Tutorial
Tutorial is loading...
Solution (74TrAkToR)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 1e6 + 5;
int a[maxn];
int prime[maxn];
int L[maxn];
vector < int > pos[maxn];
inline void add(int x, int l) {
L[l] = l;
while (x > 1) {
pos[prime[x]].push_back(l);
x /= prime[x];
}
}
inline void del(int x, int l) {
if (x == 1) {
L[l] = l;
return;
}
L[l] = l;
while (x > 1) {
if ((int)pos[prime[x]].size() == 0) {
L[l] = 0;
return;
}
L[l] = min(L[l], pos[prime[x]].back());
pos[prime[x]].pop_back();
x /= prime[x];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin >> n;
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 2; i < maxn; ++i) {
if (prime[i] == 0) {
prime[i] = i;
if (i > 1000) continue;
for (int j = i * i; j < maxn; j += i) {
prime[j] = i;
}
}
}
char type;
for (int i = 1; i <= n; ++i) {
cin >> type;
if (type == '*') add(a[i], i);
else del(a[i], i);
}
ll answer = 0;
vector < pair < int, int > > f_min;
for (int i = n; i >= 1; --i) {
int cnt = 1;
while ((int)f_min.size() > 0 && f_min.back().first >= L[i]) {
cnt += f_min.back().second;
f_min.pop_back();
}
f_min.push_back({L[i], cnt});
if (L[i] == i) answer += (ll)cnt;
}
cout << answer << '\n';
return 0;
}