Идея: Roms
Разбор
Tutorial is loading...
Решение (awoo)
for _ in range(int(input())):
n = int(input())
a = input()
b = input()
c = input()
print("YES" if any([a[i] != c[i] and b[i] != c[i] for i in range(n)]) else "NO")
1922B - Формирование треугольников
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
int t;
int main() {
cin >> t;
for (int tc = 0; tc < t; ++tc) {
int n;
cin >> n;
map<int, int> numOfLens;
for (int i = 0; i < n; ++i){
int x;
cin >> x;
++numOfLens[x];
}
long long res = 0;
int sum = 0;
for (auto it : numOfLens) {
long long cnt = it.second;
if(cnt >= 3)
res += cnt * (cnt - 1) * (cnt - 2) / 6;
if(cnt >= 2)
res += cnt * (cnt - 1) / 2 * sum;
sum += cnt;
}
cout << res << endl;
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int N = 200'000;
const int INF = 1'000'000'009;
int t;
char type(const vector <int>& a, int id) {
int distL = (id == 0? INF : a[id] - a[id - 1]);
int distR = (id + 1 == a.size()? INF : a[id + 1] - a[id]);
if(distL < distR) return 'L';
if(distL > distR) return 'R';
assert(false);
}
int main() {
ios::sync_with_stdio(false);
cin >> t;
for (int tc = 0; tc < t; ++tc) {
int n;
cin >> n;
vector <int> a(n);
for (int i = 0; i < n; ++i)
cin >> a[i];
vector <int> l(n), r(n);
for (int i = 1; i < n; ++i)
r[i] = r[i - 1] + (type(a, i - 1) == 'R'? 1 : a[i] - a[i - 1]);
for (int i = n - 2; i >= 0; --i)
l[i] = l[i + 1] + (type(a, i + 1) == 'L'? 1 : a[i + 1] - a[i]);
int m;
cin >> m;
for (int i = 0; i < m; ++i) {
int x, y;
cin >> x >> y;
--x, --y;
if (x < y)
cout << r[y] - r[x] << endl;
else
cout << l[y] - l[x] << endl;
}
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n + 2), d(n + 2, INT_MAX);
for (int i = 1; i <= n; ++i) cin >> a[i];
for (int i = 1; i <= n; ++i) cin >> d[i];
set<int> lft, cur;
for (int i = 0; i < n + 2; ++i) {
lft.insert(i);
cur.insert(i);
}
for (int z = 0; z < n; ++z) {
set<int> del, ncur;
for (int i : cur) {
auto it = lft.find(i);
if (it == lft.end()) continue;
int prv = *prev(it);
int nxt = *next(it);
if (a[prv] + a[nxt] > d[i]) {
del.insert(i);
ncur.insert(prv);
ncur.insert(nxt);
}
}
cout << del.size() << ' ';
for (auto it : del) lft.erase(it);
cur = ncur;
}
cout << '\n';
}
}
1922E - Возрастающие подпоследовательности
Идея: Roms
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
vector<int> f(long long x) {
vector<int> res;
if (x == 2) {
res.push_back(0);
} else if (x & 1) {
res = f(x - 1);
res.push_back(*min_element(res.begin(), res.end()) - 1);
} else {
res = f(x / 2);
res.push_back(*max_element(res.begin(), res.end()) + 1);
}
return res;
}
int main() {
int t;
cin >> t;
while (t--) {
long long x;
cin >> x;
auto ans = f(x);
cout << ans.size() << '\n';
for (int i : ans) cout << i << ' ';
cout << '\n';
}
}
Идея: Roms
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
const int N = 111;
int n, k;
int a[N];
int nxtx[N][N], prvx[N][N];
int nxtnx[N][N], prvnx[N][N];
int dp1[N][N][N], dp2[N][N][N];
int calc2(int, int, int);
int calc1(int l, int r, int x) {
l = nxtnx[l][x], r = prvnx[r][x];
if (l > r) return 0;
if (dp1[l][r][x] != -1) return dp1[l][r][x];
dp1[l][r][x] = calc2(l, r, x) + 1;
for (int i = l; i < r; ++i) dp1[l][r][x] = min(dp1[l][r][x], calc1(l, i, x) + calc1(i + 1, r, x));
return dp1[l][r][x];
}
int calc2(int l, int r, int x) {
l = nxtx[l][x], r = prvx[r][x];
if (l > r) return 0;
if (dp2[l][r][x] != -1) return dp2[l][r][x];
dp2[l][r][x] = n;
for (int i = l; i < r; ++i) dp2[l][r][x] = min(dp2[l][r][x], calc2(l, i, x) + calc2(i + 1, r, x));
for (int y = 0; y < k; ++y) if (x != y) dp2[l][r][x] = min(dp2[l][r][x], calc1(l, r, y));
return dp2[l][r][x];
}
void solve() {
cin >> n >> k;
for (int i = 0; i < n; ++i) cin >> a[i], --a[i];
for (int x = 0; x < k; ++x) prvx[0][x] = prvnx[0][x] = -1;
for (int i = 0; i < n; ++i) {
prvx[i][a[i]] = i;
for (int x = 0; x < k; ++x) prvx[i + 1][x] = prvx[i][x];
for (int x = 0; x < k; ++x) if (x != a[i]) prvnx[i][x] = i;
for (int x = 0; x < k; ++x) prvnx[i + 1][x] = prvnx[i][x];
}
for (int x = 0; x < k; ++x) nxtx[n][x] = nxtnx[n][x] = n;
for (int i = n - 1; i >= 0; --i) {
for (int x = 0; x < k; ++x) nxtx[i][x] = nxtx[i + 1][x];
nxtx[i][a[i]] = i;
for (int x = 0; x < k; ++x) nxtnx[i][x] = nxtnx[i + 1][x];
for (int x = 0; x < k; ++x) if (x != a[i]) nxtnx[i][x] = i;
}
memset(dp1, -1, sizeof(dp1));
memset(dp2, -1, sizeof(dp2));
int ans = n;
for (int x = 0; x < k; ++x) ans = min(ans, calc1(0, n - 1, x));
cout << ans << '\n';
}
int main() {
int t;
cin >> t;
while (t--) solve();
}