1574A - Правильные скобочные последовательности
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
t = int(input())
for i in range(t):
n = int(input())
for j in range(n):
print("()" * j + "(" * (n - j) + ")" * (n - j))
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
for _ in range(int(input())):
a, b, c, m = map(int, input().split())
a, b, c = sorted([a, b, c])
print("YES" if c - (a + b + 1) <= m <= a + b + c - 3 else "NO")
Идея: BledDest
Разбор
Tutorial is loading...
Решение (Neon)
#include <bits/stdc++.h>
using namespace std;
using li = long long;
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n;
cin >> n;
vector<li> a(n);
for (auto &x : a) cin >> x;
sort(a.begin(), a.end());
li sum = accumulate(a.begin(), a.end(), 0LL);
int m;
cin >> m;
while (m--) {
li x, y;
cin >> x >> y;
int i = lower_bound(a.begin(), a.end(), x) - a.begin();
li ans = 2e18;
if (i > 0) ans = min(ans, (x - a[i - 1]) + max(0LL, y - sum + a[i - 1]));
if (i < n) ans = min(ans, max(0LL, y - sum + a[i]));
cout << ans << '\n';
}
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (awoo)
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
using namespace std;
int n;
vector<vector<int>> a;
int m;
vector<vector<int>> b;
int main() {
scanf("%d", &n);
a.resize(n);
forn(i, n){
int c;
scanf("%d", &c);
a[i].resize(c);
forn(j, c) scanf("%d", &a[i][j]);
}
scanf("%d", &m);
b.resize(m);
forn(i, m){
b[i].resize(n);
forn(j, n){
scanf("%d", &b[i][j]);
--b[i][j];
}
}
sort(b.begin(), b.end());
vector<int> ult(n);
forn(i, n) ult[i] = int(a[i].size()) - 1;
if (!binary_search(b.begin(), b.end(), ult)){
forn(i, n) printf("%d ", ult[i] + 1);
puts("");
return 0;
}
int mx = 0;
vector<int> ans(n, -1);
forn(i, m){
vector<int> tmp = b[i];
int sum = 0;
forn(j, n) sum += a[j][tmp[j]];
forn(j, n) if (tmp[j] != 0){
--tmp[j];
if (!binary_search(b.begin(), b.end(), tmp) && sum - a[j][tmp[j] + 1] + a[j][tmp[j]] > mx){
mx = sum - a[j][tmp[j] + 1] + a[j][tmp[j]];
ans = tmp;
}
++tmp[j];
}
}
forn(i, n){
printf("%d ", ans[i] + 1);
}
puts("");
}
Идея: Roms
Разбор
Tutorial is loading...
Решение (Roms)
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
const int N = 1'000'009;
int sum (int a, int b) {
int res = a + b;
if (res < 0) res += MOD;
if (res >= MOD) res -= MOD;
return res;
}
int n, m, k;
map <pair <int, int>, char> c;
int cntr[N][2], cntc[N][2];
int cntx[2];
set <int> badr, badc;
set<int> ur, uc;
int p2[N];
void upd2(int pos, int col, int add, int cnt[N][2], set <int> &bad, set<int> &u) {
cnt[pos][col] += add;
assert(cnt[pos][col] >= 0);
if (cnt[pos][0] > 0 && cnt[pos][1] > 0){
if (!bad.count(pos))
bad.insert(pos);
} else {
if (bad.count(pos))
bad.erase(pos);
}
if (cnt[pos][0] > 0 || cnt[pos][1] > 0){
if (!u.count(pos))
u.insert(pos);
} else {
if (u.count(pos))
u.erase(pos);
}
}
void upd(int x, int y, int t) {
int col = (x & 1) ^ (y & 1);
if (c.count({x, y})) {
int ncol = col ^ c[{x, y}];
--cntx[ncol];
upd2(x, ncol, -1, cntr, badr, ur);
upd2(y, ncol, -1, cntc, badc, uc);
c.erase({x, y});
}
if (t == -1)
return;
int ncol = col ^ t;
++cntx[ncol];
upd2(x, ncol, 1, cntr, badr, ur);
upd2(y, ncol, 1, cntc, badc, uc);
c[{x, y}] = t;
}
int main(){
p2[0] = 1;
for (int i = 1; i < N; ++i)
p2[i] = sum(p2[i - 1], p2[i - 1]);
scanf("%d%d%d", &n, &m, &k);
for (int i = 0; i < k; ++i) {
int x, y, t;
scanf("%d %d %d", &x, &y, &t);
--x, --y;
upd(x, y, t);
int res = 0;
if(badr.size() > 0 && badc.size() > 0) {
res = 0;
} else if (badr.size() > 0) {
assert(m - uc.size() >= 0);
res = p2[m - uc.size()];
} else if (badc.size() > 0) {
assert(n - ur.size() >= 0);
res = p2[n - ur.size()];
} else {
if (ur.size() == 0 && uc.size() == 0)
res = sum(sum(p2[n], p2[m]), -2);
else {
assert(m - uc.size() >= 0);
res = sum(res, p2[m - uc.size()]);
assert(n - ur.size() >= 0);
res = sum(res, p2[n - ur.size()]);
if (cntx[0] == 0 || cntx[1] == 0) {
assert(cntx[0] != 0 || cntx[1] != 0);
res = sum(res, -1);
}
}
}
printf("%d\n", res);
}
return 0;
}
Идея: BledDest
Разбор
Tutorial is loading...
Решение (BledDest)
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int main()
{
int n, m, k;
scanf("%d %d %d", &n, &m, &k);
vector<vector<int>> A(n);
vector<int> bad_num(k);
for(int i = 0; i < n; i++)
{
int c;
scanf("%d", &c);
A[i].resize(c);
for(int j = 0; j < c; j++)
{
scanf("%d", &A[i][j]);
A[i][j]--;
}
}
for(int i = 0; i < n; i++)
{
if(set<int>(A[i].begin(), A[i].end()).size() != A[i].size())
{
for(auto x : A[i])
bad_num[x] = 1;
}
}
vector<vector<int>> nxt(k);
vector<vector<int>> prv(k);
for(int i = 0; i < n; i++)
for(int j = 0; j + 1 < A[i].size(); j++)
{
nxt[A[i][j]].push_back(A[i][j + 1]);
prv[A[i][j + 1]].push_back(A[i][j]);
}
for(int i = 0; i < k; i++)
{
sort(nxt[i].begin(), nxt[i].end());
sort(prv[i].begin(), prv[i].end());
nxt[i].erase(unique(nxt[i].begin(), nxt[i].end()), nxt[i].end());
prv[i].erase(unique(prv[i].begin(), prv[i].end()), prv[i].end());
}
vector<int> used(k, 0);
vector<int> cnt(k + 1, 0);
for(int i = 0; i < k; i++)
{
if(used[i]) continue;
queue<int> q;
vector<int> comp;
q.push(i);
used[i] = 1;
while(!q.empty())
{
int z = q.front();
q.pop();
comp.push_back(z);
for(auto x : nxt[z])
if(!used[x])
{
used[x] = 1;
q.push(x);
}
for(auto x : prv[z])
if(!used[x])
{
used[x] = 1;
q.push(x);
}
}
bool bad = false;
int cnt_beg = 0;
for(auto x : comp)
{
if(prv[x].empty())
cnt_beg++;
if(prv[x].size() > 1 || nxt[x].size() > 1 || bad_num[x])
bad = true;
}
bad |= (cnt_beg == 0);
if(!bad)
cnt[comp.size()]++;
}
vector<int> nonzero;
for(int i = 1; i <= k; i++)
if(cnt[i] > 0)
nonzero.push_back(i);
vector<int> dp(m + 1, 0);
dp[0] = 1;
for(int i = 1; i <= m; i++)
for(auto x : nonzero)
if(x <= i)
dp[i] = add(dp[i], mul(cnt[x], dp[i - x]));
printf("%d\n", dp[m]);
return 0;
}