Разбор
Tutorial is loading...
Решение (Vovuh, O(n + m))
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<int> cnt(m + 2);
for (int i = 0; i < n; ++i) {
int l, r;
cin >> l >> r;
++cnt[l];
--cnt[r + 1];
}
for (int i = 1; i <= m; ++i)
cnt[i] += cnt[i - 1];
vector<int> ans;
for (int i = 1; i <= m; ++i) {
if (cnt[i] == 0)
ans.push_back(i);
}
cout << ans.size() << endl;
for (auto it : ans) cout << it << " ";
cout << endl;
return 0;
}
Решение (Vovuh, O(n * m))
n, m = map(int, input().split())
seg = [list(map(int, input().split())) for i in range(n)]
def bad(x):
for i in range(n):
if (seg[i][0] <= x and x <= seg[i][1]): return False
return True
ans = list(filter(bad, [i for i in range(1, m + 1)]))
print(len(ans))
print(' '.join([str(x) for x in ans]))
Разбор
Tutorial is loading...
Решение (Vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n;
string s, t;
cin >> n >> s >> t;
vector<int> ans;
for (int i = 0; i < n; ++i) {
if (s[i] == t[i]) continue;
int pos = -1;
for (int j = i + 1; j < n; ++j) {
if (s[j] == t[i]) {
pos = j;
break;
}
}
if (pos == -1) {
cout << -1 << endl;
return 0;
}
for (int j = pos - 1; j >= i; --j) {
swap(s[j], s[j + 1]);
ans.push_back(j);
}
}
assert(s == t);
cout << ans.size() << endl;
for (auto it : ans) cout << it + 1 << " ";
cout << endl;
return 0;
}
Разбор
Tutorial is loading...
Решение (Vovuh)
#include <bits/stdc++.h>
using namespace std;
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
int n, m;
cin >> n >> m;
vector<pair<int, int>> a(n);
long long sum = 0;
for (int i = 0; i < n; ++i) {
cin >> a[i].first >> a[i].second;
sum += a[i].first;
}
sort(a.begin(), a.end(), [&](pair<int, int> a, pair<int, int> b) { return a.first - a.second > b.first - b.second; });
for (int i = 0; i < n; ++i) {
if (sum <= m) {
cout << i << endl;
return 0;
}
sum -= a[i].first - a[i].second;
}
if (sum <= m)
cout << n << endl;
else
cout << -1 << endl;
return 0;
}
Разбор
Tutorial is loading...
Решение (BledDest)
def step(cur, x):
if(cur - x > 0):
return cur - x
else:
return cur + x
n, k, s = map(int, input().split())
cur = 1
if(k > s or k * (n - 1) < s):
print('NO')
else:
print('YES')
while(k > 0):
l = min(n - 1, s - (k - 1))
cur = step(cur, l)
print(cur, end = ' ')
s -= l
k -= 1
1015E1 - Рисование звезд (упрощенная редакция)
Разбор
Tutorial is loading...
Решение (MikeMirzayanov, O(n^3))
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
const int dx[] = {1, 0, -1, 0};
const int dy[] = {0, 1, 0, -1};
const int N = 1001;
int n, m;
vector<string> f;
int d[N][N][4];
int r[N][N], a[N][N], b[N][N];
int getd(int i, int j, int k) {
int dxk = dx[k];
int dyk = dy[k];
int result = 0;
while (i >= 0 && i < n && j >= 0 && j < m && f[i][j] == '*')
result++, i += dxk, j += dyk;
return result;
}
int main() {
cin >> n >> m;
f = vector<string>(n);
forn(i, n)
cin >> f[i];
memset(d, -1, sizeof(d));
int result = 0;
for (int i = 1; i + 1 < n; i++)
for (int j = 1; j + 1 < m; j++)
if (f[i][j] == '*') {
bool around = true;
forn(k, 4)
around = around && (f[i + dx[k]][j + dy[k]] == '*');
if (around) {
r[i][j] = INT_MAX;
forn(k, 4)
r[i][j] = min(r[i][j], getd(i, j, k) - 1);
result++;
a[i][j - r[i][j]] = max(a[i][j - r[i][j]], 2 * r[i][j] + 1);
b[i - r[i][j]][j] = max(b[i - r[i][j]][j], 2 * r[i][j] + 1);
}
}
vector<string> g(n, string(m, '.'));
forn(i, n) {
int v = 0;
forn(j, m) {
v = max(v - 1, a[i][j]);
if (v > 0)
g[i][j] = '*';
}
}
forn(j, m) {
int v = 0;
forn(i, n) {
v = max(v - 1, b[i][j]);
if (v > 0)
g[i][j] = '*';
}
}
if (f == g) {
cout << result << endl;
forn(i, n)
forn(j, m)
if (r[i][j] > 0)
cout << i + 1 << " " << j + 1 << " " << r[i][j] << endl;
} else
cout << -1 << endl;
}
1015E2 - Рисование звезд (усложненная редакция)
Разбор
Tutorial is loading...
Решение (Vovuh, O(n^2))
#include <bits/stdc++.h>
using namespace std;
int n, m;
vector<string> s;
vector<string> draw(const vector<pair<pair<int, int>, int>> &r) {
vector<string> f(n, string(m, '.'));
vector<vector<int>> h(n, vector<int>(m));
vector<vector<int>> v(n, vector<int>(m));
for (auto it : r) {
int x = it.first.first;
int y = it.first.second;
int len = it.second;
++v[x - len][y];
if (x + len + 1 < n)
--v[x + len + 1][y];
++h[x][y - len];
if (y + len + 1 < m)
--h[x][y + len + 1];
}
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i > 0) v[i][j] += v[i - 1][j];
if (j > 0) h[i][j] += h[i][j - 1];
if (v[i][j] > 0 || h[i][j] > 0)
f[i][j] = '*';
}
}
return f;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> m;
s = vector<string>(n);
for (int i = 0; i < n; ++i) {
cin >> s[i];
}
vector<vector<int>> l(n, vector<int>(m));
vector<vector<int>> r(n, vector<int>(m));
vector<vector<int>> u(n, vector<int>(m));
vector<vector<int>> d(n, vector<int>(m));
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (i > 0) {
if (s[i][j] != '.')
u[i][j] = u[i - 1][j] + 1;
} else {
u[i][j] = s[i][j] != '.';
}
if (j > 0) {
if (s[i][j] != '.')
l[i][j] = l[i][j - 1] + 1;
} else {
l[i][j] = s[i][j] != '.';
}
}
}
for (int i = n - 1; i >= 0; --i) {
for (int j = m - 1; j >= 0; --j) {
if (i < n - 1) {
if (s[i][j] != '.')
d[i][j] = d[i + 1][j] + 1;
} else {
d[i][j] = s[i][j] != '.';
}
if (j < m - 1) {
if (s[i][j] != '.')
r[i][j] = r[i][j + 1] + 1;
} else {
r[i][j] = s[i][j] != '.';
}
}
}
vector<pair<pair<int, int>, int>> ans;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < m; ++j) {
if (s[i][j] == '*') {
int len = min(min(u[i][j], l[i][j]), min(d[i][j], r[i][j])) - 1;
if (len != 0) {
ans.push_back(make_pair(make_pair(i, j), len));
}
}
}
}
if (draw(ans) != s) {
cout << -1 << endl;
} else {
cout << ans.size() << endl;
for (auto it : ans) {
cout << it.first.first + 1 << " " << it.first.second + 1 << " " << it.second << endl;
}
}
return 0;
}
Разбор
Tutorial is loading...
Решение (Vovuh)
#include <bits/stdc++.h>
using namespace std;
const int N = 203;
const int MOD = 1e9 + 7;
int n, ssz;
string s;
int len[N][2];
int dp[N][N][N][2];
int calc(const string &t) {
int tsz = t.size();
for (int i = tsz; i > 0; --i) {
if (s.substr(0, i) == t.substr(tsz - i, i))
return i;
}
return 0;
}
void add(int &a, int b) {
a += b;
if (a >= MOD)
a -= MOD;
if (a < 0)
a += MOD;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
#endif
cin >> n >> s;
ssz = s.size();
if (s[0] == '(')
len[0][0] = 1;
else
len[0][1] = 1;
string pref;
for (int i = 0; i < ssz; ++i) {
pref += s[i];
pref += '(';
len[i + 1][0] = calc(pref);
pref.pop_back();
pref += ')';
len[i + 1][1] = calc(pref);
pref.pop_back();
}
dp[0][0][0][0] = 1;
for (int i = 0; i < 2 * n; ++i) {
for (int j = 0; j <= n; ++j) {
for (int pos = 0; pos <= ssz; ++pos) {
for (int f = 0; f < 2; ++f) {
if (dp[i][j][pos][f] == 0) continue;
if (j + 1 <= n)
add(dp[i + 1][j + 1][len[pos][0]][f | (len[pos][0] == ssz)], dp[i][j][pos][f]);
if (j > 0)
add(dp[i + 1][j - 1][len[pos][1]][f | (len[pos][1] == ssz)], dp[i][j][pos][f]);
}
}
}
}
int ans = 0;
for (int i = 0; i <= ssz; ++i)
add(ans, dp[2 * n][0][i][1]);
cout << ans << endl;
return 0;
}