Разбор Е скоро будет.
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
int main() {
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int>a(n), ans;
int cnt0 = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
if (!a[i]) cnt0++;
}
int cnt1 = n - cnt0;
if (cnt0 >= n / 2) {
cout << cnt0 << '\n';
for (int i = 0; i < cnt0; i++) cout << 0 << ' ';
} else {
cout << cnt1 - cnt1 % 2 << '\n';
for (int i = 0; i < cnt1 - cnt1 % 2; i++) {
cout << 1 << ' ';
}
}
cout << '\n';
}
return 0;
}
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int a[n];
int mi = 0;
for (int i = 0; i < n; i++) {
cin >> a[i];
mi = (a[i] > a[mi] ? i : mi);
}
vector<int> b(n);
b[0] = a[mi]; a[mi] = 0;
int cg = b[0];
for (int i = 1; i < n; i++) {
int ci = 0, cans = 0;
for (int j = 0; j < n; j++)
if (a[j] && __gcd(a[j], cg) > cans) {
cans = __gcd(a[j], cg);
ci = j;
}
b[i] = a[ci];
cg = cans;
a[ci] = 0;
}
for (int i = 0; i < n; i++)
cout << b[i] << ' ';
cout << '\n';
}
int main() {
int t;
cin >> t;
while (t--)
solve();
return 0;
}
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
int ask(int x, int y) {
cout << "? " << x + 1 << ' ' << y + 1 << endl;
int z;
cin >> z;
return z;
}
int main() {
int n;
cin >> n;
vector<int>ans(n, -1);
int mx = 0;
for (int i = 1; i < n; i++) {
int a = ask(mx, i);
int b = ask(i, mx);
if (a > b) {
ans[mx] = a;
mx = i;
} else {
ans[i] = b;
}
}
ans[mx] = n;
cout << "! ";
for (int i = 0; i < n; i++) cout << ans[i] << ' ';
cout << endl;
return 0;
}
1407D — Дискретные Центробежные Прыжки
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 1;
const int maxn = 3e5 + 1;
int h[maxn], dp[maxn], lge[maxn], lle[maxn], rge[maxn], rle[maxn];
vector<int>jumps[maxn];
int main() {
int n;
cin >> n;
for (int i = 0; i < n; i++) {
cin >> h[i];
dp[i] = INF;
}
dp[0] = 0;
vector<pair<int, int> >st;
for (int i = 0; i < n; i++) { // the nearest greater from the left
while (!st.empty() && st.back().first < h[i]) {
st.pop_back();
}
if (st.empty()) lge[i] = -1;
else lge[i] = st.back().second;
st.push_back({h[i], i});
}
st.clear();
for (int i = 0; i < n; i++) { // the nearest less from the left
while (!st.empty() && st.back().first > h[i]) {
st.pop_back();
}
if (st.empty()) lle[i] = -1;
else lle[i] = st.back().second;
st.push_back({h[i], i});
}
st.clear();
for (int i = n - 1; i >= 0; i--) { // the nearest greater from the right
while (!st.empty() && st.back().first < h[i]) {
st.pop_back();
}
if (st.empty()) rge[i] = -1;
else rge[i] = st.back().second;
st.push_back({h[i], i});
}
st.clear();
for (int i = n - 1; i >= 0; i--) { // the nearest less from the right
while (!st.empty() && st.back().first > h[i]) {
st.pop_back();
}
if (st.empty()) rle[i] = -1;
else rle[i] = st.back().second;
st.push_back({h[i], i});
}
st.clear();
for (int i = 0; i < n; i++) {
if (rle[i] != -1) jumps[i].push_back(rle[i]);
if (rge[i] != -1) jumps[i].push_back(rge[i]);
if (lle[i] != -1) jumps[lle[i]].push_back(i);
if (lge[i] != -1) jumps[lge[i]].push_back(i);
}
for (int i = 0; i < n; i++) {
for (int to : jumps[i]) {
dp[to] = min(dp[to], dp[i] + 1);
}
}
cout << dp[n - 1];
return 0;
}
1407E — Егор в республике Дагестан
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e6 + 1;
vector<int> bg[maxn], rg[maxn];
int b[maxn], r[maxn], d[maxn], col[maxn];
int n, m;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, t;
cin >> u >> v >> t;
--u; --v;
if (!t) bg[v].push_back(u);
else rg[v].push_back(u);
}
for (int i = 0; i < n; i++)
d[i] = b[i] = r[i] = n;
queue<int> q;
q.push(n - 1);
d[n - 1] = r[n - 1] = b[n - 1] = 0;
while (!q.empty()) {
int x = q.front();
q.pop();
for (auto to : bg[x]) {
if (b[to] < n) continue;
b[to] = d[x] + 1;
if (max(b[to], r[to]) < n) {
q.push(to);
d[to] = max(b[to], r[to]);
}
}
for (auto to : rg[x]) {
if (r[to] < n) continue;
r[to] = d[x] + 1;
if (max(b[to], r[to]) < n) {
q.push(to);
d[to] = max(b[to], r[to]);
}
}
}
if (d[0] == n) cout << "-1\n";
else cout << d[0] << '\n';
for (int i = 0; i < n; i++) {
if (b[i] > r[i]) col[i] = 0;
else col[i] = 1;
cout << col[i];
}
return 0;
}