Editorial
Tutorial is loading...
Solution (fcspartakm)
#include <bits/stdc++.h>
using namespace std;
int a1, a2, k1, k2, n;
inline void read() {
cin >> a1 >> a2 >> k1 >> k2 >> n;
}
inline void solve() {
if (k1 > k2) {
swap(k1, k2);
swap(a1, a2);
}
int minCnt = max(0, n — a1 * (k1 — 1) — a2 * (k2 — 1));
int maxCnt = 0;
if (n <= a1 * k1) {
maxCnt = n / k1;
} else {
maxCnt = a1 + (n — a1 * k1) / k2;
}
cout << minCnt << ' ' << maxCnt << endl;
}
int main () {
#ifdef fcspartakm
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cerr << setprecision(10) << fixed;
read();
solve();
//cerr << "TIME: " << clock() << endl;
}
1215B - The Number of Products
Editorial
Tutorial is loading...
Solution (fcspartakm)
#include <bits/stdc++.h>
using namespace std;
const int N = 200 * 1000 + 13;
int n;
int a[N];
inline void read() {
cin >> n;
for (int i = 0; i < n; i++) {
cin >> a[i];
}
}
inline void solve() {
int pos = -1;
li ans0 = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
pos = i;
}
if (pos != -1) {
ans0 += pos + 1;
}
}
int cnt1 = 0, cnt2 = 0;
int bal = 0;
li ansP = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
cnt1 = 0, cnt2 = 0, bal = 0;
continue;
}
if (bal % 2 == 0) {
cnt1++;
} else {
cnt2++;
}
if (a[i] < 0) {
bal++;
}
if (bal % 2 == 0) {
ansP += cnt1;
} else {
ansP += cnt2;
}
}
cout << n * 1ll * (n + 1) / 2 - ans0 - ansP << ' ' << ansP << endl;
}
int main () {
#ifdef fcspartakm
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cerr << setprecision(10) << fixed;
read();
solve();
//cerr << "TIME: " << clock() << endl;
}
Editorial
Tutorial is loading...
Solution (fcspartakm)
#include<bits/stdc++.h>
using namespace std;
int n;
string s, t;
inline void read() {
cin >> n >> s >> t;
}
inline void solve() {
vector<int> pos01, pos10;
for (int i = 0; i < n; i++) {
if (s[i] != t[i]) {
if (s[i] == 'a') {
pos01.pb(i);
} else {
pos10.pb(i);
}
}
}
if (sz(pos01) % 2 != sz(pos10) % 2) {
cout << -1 << endl;
return;
}
vector<pair<int, int> > ans;
for (int i = 0; i + 1 < sz(pos01); i += 2) {
ans.pb(mp(pos01[i], pos01[i + 1]));
}
for (int i = 0; i + 1 < sz(pos10); i += 2) {
ans.pb(mp(pos10[i], pos10[i + 1]));
}
if (sz(pos01) % 2) {
int x = pos01.back();
int y = pos10.back();
ans.pb(mp(x, x));
ans.pb(mp(x, y));
}
cout << sz(ans) << endl;
for (int i = 0; i < sz(ans); i++) {
cout << ans[i].ft + 1 << ' ' << ans[i].sc + 1 << endl;
}
}
int main () {
#ifdef fcspartakm
freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
#endif
srand(time(NULL));
cerr << setprecision(10) << fixed;
read();
solve();
//cerr << "TIME: " << clock() << endl;
}
Editorial
Tutorial is loading...
Solution (fcspartakm)
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int n;
int main() {
cin >> n;
long double sum1 = 0, sum2 = 0;
string s;
cin >> s;
for (int i = 0; i < n; i++) {
if (s[i] != '?') {
if (i < n / 2) {
sum1 += (long double)(s[i] - '0');
} else {
sum2 += (long double)(s[i] - '0');
}
} else {
if (i < n / 2) {
sum1 += (long double)4.5;
} else {
sum2 += (long double)4.5;
}
}
}
if (fabsl(sum1 - sum2) < 1e-9) {
cout << "Bicarp" << endl;
} else {
cout << "Monocarp" << endl;
}
}
Editorial
Tutorial is loading...
Solution (fcspartakm)
#include <iostream>
#include <set>
#include <string>
#include <vector>
#include <algorithm>
using namespace std;
const int N = 1000 * 1000 + 13;
const int M = 20 + 1;
const long long INF = 1000 * 1000 * 1000 * 1ll * 1000 * 1000 * 1000;
int n;
long long d[(1 << M)];
long long cnt[M][M];
vector<int> col[M];
int main() {
cin >> n;
for (int i = 0; i < n; i++) {
int x;
cin >> x;
x--;
col[x].push_back(i);
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (i == j) {
continue;
}
if ((int)col[i].size() == 0 || (int)col[j].size() == 0) {
continue;
}
int pos2 = 0;
for (int pos1 = 0; pos1 < (int)col[i].size(); pos1++) {
while (true) {
if (pos2 == (int)col[j].size() - 1 || col[j][pos2 + 1] > col[i][pos1]) {
break;
}
pos2++;
}
if (col[i][pos1] > col[j][pos2]) {
cnt[i][j] += pos2 + 1;
}
}
}
}
for (int mask = 0; mask < (1 << 20); mask++) {
d[mask] = INF;
}
d[0] = 0;
for (int mask = 0; mask < (1 << 20); mask++) {
vector<int> was;
for (int i = 0; i < 20; i++) {
if (mask & (1 << i)) {
was.push_back(i);
}
}
for (int i = 0; i < 20; i++) {
if (mask & (1 << i)) {
continue;
}
long long sum = 0;
for (int j = 0; j < (int)was.size(); j++) {
sum += cnt[was[j]][i];
}
int nmask = mask | (1 << i);
d[nmask] = min(d[nmask], d[mask] + sum);
}
}
cout << d[(1 << 20) - 1] << endl;
}
Editorial
Tutorial is loading...
Solution (BledDest)
#include<bits/stdc++.h>
using namespace std;
const int N = 1600043;
vector<int> g[N];
vector<int> gt[N];
int used[N];
vector<int> order;
int comp[N];
vector<int> result;
int v;
void add_edge(int v1, int v2)
{
g[v1].push_back(v2);
gt[v2].push_back(v1);
}
void add_disjunction(int v1, int v2)
{
add_edge(v1 ^ 1, v2);
add_edge(v2 ^ 1, v1);
}
void dfs1(int v)
{
if(used[v]) return;
used[v] = 1;
for(auto u : g[v])
dfs1(u);
order.push_back(v);
}
void dfs2(int v, int cc)
{
if(comp[v]) return;
comp[v] = cc;
for(auto u : gt[v])
dfs2(u, cc);
}
bool solve2SAT()
{
for(int i = 0; i < v * 2; i++)
dfs1(i);
reverse(order.begin(), order.end());
int cc = 0;
for(auto x : order)
{
if(comp[x] == 0)
{
cc++;
dfs2(x, cc);
}
}
for(int i = 0; i < v; i++)
{
if(comp[i * 2] == comp[i * 2 + 1])
return false;
else if(comp[i * 2] > comp[i * 2 + 1])
result.push_back(i);
}
return true;
}
int main()
{
int n, p, M, m;
scanf("%d %d %d %d", &n, &p, &M, &m);
v = p + M - 1;
for(int i = 0; i < n; i++)
{
int x, y;
scanf("%d %d", &x, &y);
--x;
--y;
add_disjunction(x * 2, y * 2);
}
for(int i = 0; i < p; i++)
{
int l, r;
scanf("%d %d", &l, &r);
if(l != 1)
add_disjunction((l - 2 + p) * 2, i * 2 + 1);
if(r != M)
add_disjunction((r - 1 + p) * 2 + 1, i * 2 + 1);
}
for(int i = 0; i < m; i++)
{
int x, y;
scanf("%d %d", &x, &y);
--x;
--y;
add_disjunction(x * 2 + 1, y * 2 + 1);
}
for(int i = 2; i <= M - 1; i++)
{
int f1 = i - 2 + p;
int f2 = f1 + 1;
add_disjunction(f1 * 2, f2 * 2 + 1);
}
if(!solve2SAT())
{
puts("-1");
return 0;
}
int k = 1;
vector<int> stations;
for(auto x : result)
if(x < p)
stations.push_back(x);
else
k = max(k, x - p + 2);
printf("%d %d\n", int(stations.size()), k);
for(auto x : stations)
printf("%d ", x + 1);
return 0;
}








.
. Firstly, each submatrix
non-intersecting submatrices fit in matrix
.
. Due to rounding down, it's enough to check only this value of
and push these values to heap. When finished, 



and the second sigma by 




solution:
, but in fact it's pretty fast.
(let's call this process reduction, we will need it later). If we get 