1741A - Сравни размеры футболок
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define eb emplace_back
void ct(char c) {
cout << c << '\n';
}
void solve() {
string a,b; cin >> a >> b;
char ca = a.back();
char cb = b.back();
if (ca == cb) {
if (sz(a) == sz(b)) cout << '=';
else if (ca == 'S') {
cout << (sz(a) < sz(b) ? '>' : '<');
} else {
cout << (sz(a) < sz(b) ? '<' : '>');
}
}else cout << (ca < cb ? '>' : '<');
cout << '\n';
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include "bits/stdc++.h"
using namespace std;
void solve(){
int n;
cin >> n;
if(n == 3){
cout << -1 << endl;
}
else{
for(int i = 3; i <= n; i++) cout << i << ' ';
cout << 2 << ' ' << 1 << endl;
}
}
int main(){
int t;
cin >> t;
while(t--){
solve();
}
}
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 2020;
int n;
int arr[MAXN];
int go(int i, int sum) {
if (i == n) return 0;
for (int j = i + 1, cur = 0; j <= n; ++j) {
cur += arr[j - 1];
if (cur > sum) return n;
if (cur == sum) return max(j - i, go(j, sum));
}
return n;
}
int solve() {
int ans = n;
for (int len = 1, sum = 0; len < n; ++len) {
sum += arr[len - 1];
ans = min(ans, go(0, sum));
}
return ans;
}
int main() {
int t; cin >> t;
while (t--) {
cin >> n;
for (int i = 0; i < n; ++i)
cin >> arr[i];
cout << solve() << endl;
}
}
1741D - Маша и красивое дерево
Идея: Gornak40
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
const int MAXM = 300300;
int n, m;
int arr[MAXM];
int solve(int l, int r) {
if (r - l == 1) return 0;
int mid = (l + r) >> 1;
int mal = *max_element(arr + l, arr + mid);
int mar = *max_element(arr + mid, arr + r);
int ans = 0;
if (mal > mar) {
++ans;
for (int i = 0; i < (mid - l); ++i)
swap(arr[l + i], arr[mid + i]);
}
return solve(l, mid) + solve(mid, r) + ans;
}
int solve() {
int ans = solve(0, m);
if (is_sorted(arr, arr + m))
return ans;
return -1;
}
int main() {
int t; cin >> t;
while (t--) {
cin >> m;
for (int i = 0; i < m; ++i)
cin >> arr[i];
cout << solve() << endl;
}
}
1741E - Передача последовательности по сети
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define eb emplace_back
void solve() {
int n; cin >> n;
vector<int> a(n+1);
for (int i = 1; i <= n; ++i) {
cin >> a[i];
}
vector<bool> dp(n+1, false);
dp[0] = true;
for (int i = 1; i <= n; ++i) {
if (i + a[i] <= n && dp[i-1]) dp[i + a[i]] = true;
if (i - a[i] - 1 >= 0 && dp[i - a[i] - 1]) dp[i] = true;
}
cout << (dp[n] ? "YES" : "NO") << '\n';
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Идея: MikeMirzayanov, senjougaharin
Разбор
Tutorial is loading...
Решение
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <queue>
using namespace std;
int n;
vector<int> calc(vector<vector<int>> a) {
vector<pair<int, int>> l(n), r(n);
for (int i = 0; i < n; ++i) {
l[i] = {a[i][0], i};
r[i] = {a[i][1], i};
}
sort(l.begin(), l.end());
sort(r.begin(), r.end());
vector<vector<pair<int, int>>> suf(n);
vector<pair<int, int>> curr;
for (int i0 = n - 1; i0 >= 0; --i0) {
int xr = r[i0].first;
int i = r[i0].second;
int xl = a[i][0];
int c = a[i][2];
if (curr.empty()) {
curr.emplace_back(xl, c);
} else if (curr.size() == 1) {
if (curr[0].second == c) {
curr[0].first = min(curr[0].first, xl);
} else {
curr.emplace_back(xl, c);
}
} else {
if (curr[0].second == c) {
curr[0].first = min(curr[0].first, xl);
} else if (curr[1].second == c) {
curr[1].first = min(curr[1].first, xl);
} else {
curr.emplace_back(xl, c);
}
}
sort(curr.begin(), curr.end());
if (curr.size() == 3) {
curr.pop_back();
}
suf[i0] = curr;
}
vector<int> ans(n, 1e9);
int j = 0;
for (int i0 = 0; i0 < n; ++i0) {
int xl = l[i0].first, i = l[i0].second;
int xr = a[i][1], c = a[i][2];
while (j < n && r[j].first < xl) {
j++;
}
if (j < n) {
vector<pair<int, int>> s = suf[j];
if (s[0].second != c) {
ans[i] = min(ans[i], max(0, s[0].first - xr));
} else if (s.size() == 2) {
ans[i] = min(ans[i], max(0, s[1].first - xr));
}
}
}
return ans;
}
const int K = 1e9 + 1;
void solve() {
cin >> n;
vector<vector<int>> a(n, vector<int>(3)), b(n, vector<int>(3));
vector<pair<int, int>> l(n), r(n);
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 3; ++j) {
cin >> a[i][j];
if (j == 2) {
b[i][j] = a[i][j];
} else {
b[i][1 - j] = K - a[i][j];
}
}
}
vector<int> ans1 = calc(a), ans2 = calc(b);
for (int i = 0; i < n; ++i) {
cout << min(ans1[i], ans2[i]) << ' ';
}
cout << '\n';
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int t = 1;
cin >> t;
for (int it = 0; it < t; ++it) {
solve();
}
return 0;
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
from collections import deque
def solve():
n, m = map(int, input().split())
sl = [[] for _ in range(n)]
for _ in range(m):
u, v = map(int, input().split())
u -= 1
v -= 1
sl[u] += [v]
sl[v] += [u]
f = int(input())
h = [int(x) - 1 for x in input().split()]
mask = [0] * n
k = int(input())
p = [int(x) - 1 for x in input().split()] + [-1]
for i in range(k):
mask[h[p[i]]] += 1 << i
vars = [set() for _ in range(n)]
dist = [-1] * n
dist[0] = 0
vars[0].add(mask[0])
q = deque([0])
while len(q) > 0:
v = q.popleft()
for u in sl[v]:
if dist[u] == -1:
dist[u] = dist[v] + 1
q.append(u)
if dist[u] == dist[v] + 1:
for msk in vars[v]:
vars[u].add(msk | mask[u])
backpack = [False] * (1 << k)
backpack[0] = True
j = 0
for i in range(f):
if i == p[j]:
j += 1
continue
nw = backpack.copy()
for msk in range(1 << k):
if not backpack[msk]:
continue
for var in vars[h[i]]:
nw[msk | var] = True
backpack, nw = nw, backpack
mn = k
for msk in range(1 << k):
if not backpack[msk]:
continue
ans = 0
for b in range(k):
if msk & (1 << b) == 0:
ans += 1
mn = min(mn, ans)
print(mn)
t = int(input())
for _ in range(t):
solve()