1311A - Add Odd or Subtract Even
Idea: vovuh
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int a, b;
cin >> a >> b;
if (a == b) cout << 0 << endl;
else cout << 1 + int((a < b) ^ ((b - a) & 1)) << endl;
}
return 0;
}
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution (n^2)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> p(n);
for (int i = 0; i < m; ++i) {
int pos;
cin >> pos;
p[pos - 1] = 1;
}
while (true) {
bool ok = false;
for (int i = 0; i < n; ++i) {
if (p[i] && a[i] > a[i + 1]) {
ok = true;
swap(a[i], a[i + 1]);
}
}
if (!ok) break;
}
bool ok = true;
for (int i = 0; i < n - 1; ++i) {
ok &= a[i] <= a[i + 1];
}
if (ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
Solution (n log n)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<int> p(n);
for (int i = 0; i < m; ++i) {
int pos;
cin >> pos;
p[pos - 1] = 1;
}
for (int i = 0; i < n; ++i) {
if (p[i] == 0) continue;
int j = i;
while (j < n && p[j]) ++j;
sort(a.begin() + i, a.begin() + j + 1);
i = j;
}
bool ok = true;
for (int i = 0; i < n - 1; ++i) {
ok &= a[i] <= a[i + 1];
}
if (ok) cout << "YES" << endl;
else cout << "NO" << endl;
}
return 0;
}
Idea: vovuh
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n, m;
string s;
cin >> n >> m >> s;
vector<int> pref(n);
for (int i = 0; i < m; ++i) {
int p;
cin >> p;
++pref[p - 1];
}
for (int i = n - 1; i > 0; --i) {
pref[i - 1] += pref[i];
}
vector<int> ans(26);
for (int i = 0; i < n; ++i) {
ans[s[i] - 'a'] += pref[i];
++ans[s[i] - 'a'];
}
for (int i = 0; i < 26; ++i) {
cout << ans[i] << " ";
}
cout << endl;
}
return 0;
}
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int a, b, c;
cin >> a >> b >> c;
int ans = 1e9;
int A = -1, B = -1, C = -1;
for (int cA = 1; cA <= 2 * a; ++cA) {
for (int cB = cA; cB <= 2 * b; cB += cA) {
for (int i = 0; i < 2; ++i) {
int cC = cB * (c / cB) + i * cB;
int res = abs(cA - a) + abs(cB - b) + abs(cC - c);
if (ans > res) {
ans = res;
A = cA;
B = cB;
C = cC;
}
}
}
}
cout << ans << endl << A << " " << B << " " << C << endl;
}
return 0;
}
1311E - Construct the Binary Tree
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int t;
cin >> t;
while (t--) {
int n, d;
cin >> n >> d;
int ld = 0, rd = n * (n - 1) / 2;
for (int i = 1, cd = 0; i <= n; ++i) {
if (!(i & (i - 1))) ++cd;
ld += cd - 1;
}
if (!(ld <= d && d <= rd)) {
cout << "NO" << endl;
continue;
}
vector<int> par(n);
iota(par.begin(), par.end(), -1);
vector<int> cnt(n, 1);
cnt[n - 1] = 0;
vector<int> bad(n);
vector<int> dep(n);
iota(dep.begin(), dep.end(), 0);
int cur = n * (n - 1) / 2;
while (cur > d) {
int v = -1;
for (int i = 0; i < n; ++i) {
if (!bad[i] && cnt[i] == 0 && (v == -1 || dep[v] > dep[i])) {
v = i;
}
}
assert(v != -1);
int p = -1;
for (int i = 0; i < n; ++i) {
if (cnt[i] < 2 && dep[i] < dep[v] - 1 && (p == -1 || dep[p] < dep[i])) {
p = i;
}
}
if (p == -1) {
bad[v] = 1;
continue;
}
assert(dep[v] - dep[p] == 2);
--cnt[par[v]];
--dep[v];
++cnt[p];
par[v] = p;
--cur;
}
cout << "YES" << endl;
for (int i = 1; i < n; ++i) cout << par[i] + 1 << " ";
cout << endl;
}
return 0;
}
Idea: vovuh
Tutorial
Tutorial is loading...
Solution (Fenwick tree)
#include <bits/stdc++.h>
using namespace std;
long long get(vector<long long> &f, int pos) {
long long res = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
res += f[pos];
return res;
}
void upd(vector<long long> &f, int pos, int val) {
for (; pos < int(f.size()); pos |= pos + 1) {
f[pos] += val;
}
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<pair<int, int>> p(n);
for (auto &pnt : p) cin >> pnt.first;
for (auto &pnt : p) cin >> pnt.second;
sort(p.begin(), p.end());
vector<int> vs;
for (auto &pnt : p) vs.push_back(pnt.second);
sort(vs.begin(), vs.end());
vs.resize(unique(vs.begin(), vs.end()) - vs.begin());
long long ans = 0;
vector<long long> cnt(vs.size()), xs(vs.size());
for (auto &pnt : p) {
int pos = lower_bound(vs.begin(), vs.end(), pnt.second) - vs.begin();
ans += get(cnt, pos) * 1ll * pnt.first - get(xs, pos);
upd(cnt, pos, 1);
upd(xs, pos, pnt.first);
}
cout << ans << endl;
return 0;
}
Solution (pbds)
#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef
tree<
pair<int, int>,
null_type,
less<pair<int, int>>,
rb_tree_tag,
tree_order_statistics_node_update>
ordered_set;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
cin >> n;
vector<pair<int, int>> p(n);
for (auto &pnt : p) cin >> pnt.first;
for (auto &pnt : p) cin >> pnt.second;
sort(p.begin(), p.end());
ordered_set s;
long long ans = 0;
for (int i = 0; i < n; ++i) {
int cnt = s.order_of_key(make_pair(p[i].second + 1, -1));
ans += cnt * 1ll * p[i].first;
s.insert(make_pair(p[i].second, i));
}
s.clear();
for (int i = n - 1; i >= 0; --i) {
int cnt = int(s.size()) - s.order_of_key(make_pair(p[i].second - 1, n));
ans -= cnt * 1ll * p[i].first;
s.insert(make_pair(p[i].second, i));
}
cout << ans << endl;
return 0;
}