Idea: Vladosiya, MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
string s;
cin >> s;
transform(s.begin(), s.end(), s.begin(), [] (char c) {
return tolower(c);
});
s.erase(unique(s.begin(), s.end()), s.end());
cout << (s == "meow" ? "YES" : "NO") << "\n";
}
int main(){
int t;
cin >> t;
while(t--) solve();
}
1800B - Посчитай количество пар
Idea: myav
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int N = 26;
void solve(){
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int>big(N, 0), small(N, 0);
for(auto &i : s){
if('A' <= i && 'Z' >= i) big[i - 'A']++;
else small[i - 'a']++;
}
int answer = 0;
for(int i = 0; i < N; i++){
int pairs = min(small[i], big[i]);
answer += pairs;
small[i] -=pairs; big[i] -= pairs;
int add = min(k, max(small[i], big[i]) / 2);
k -= add; answer += add;
}
cout << answer << endl;
}
int main(){
int t;
cin >> t;
while(t--) solve();
return 0;
}
1800C1 - Усиление героев (простая версия)
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
def solve():
n = int(input())
s = [int(x) for x in input().split()]
ans = 0
buffs = [0] * n
for e in s:
if e > 0:
buffs += [e]
j = len(buffs) - 1
while buffs[j] < buffs[j - 1]:
buffs[j], buffs[j - 1] = buffs[j - 1], buffs[j]
j -= 1
else:
ans += buffs.pop()
print(ans)
t = int(input())
for _ in range(t):
solve()
1800C2 - Усиление героев (сложная версия)
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
from queue import PriorityQueue
def solve():
n = int(input())
s = [int(x) for x in input().split()]
ans = 0
buffs = PriorityQueue()
for e in s:
if e > 0:
buffs.put(-e)
elif not buffs.empty():
ans -= buffs.get()
print(ans)
t = int(input())
for _ in range(t):
solve()
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include <iostream>
#include <vector>
#include <queue>
#include <map>
#include <set>
using namespace std;
void solve() {
int n;
cin >> n;
string s;
cin >> s;
int res = n - 1;
for (int i = 1; i + 1 < n; ++i) {
if (s[i - 1] == s[i + 1]) {
res--;
}
}
cout << res << '\n';
}
int main(int argc, char* argv[]) {
int t;
cin >> t;
for (int i = 0; i < t; ++i) {
solve();
}
}
1800E1 - Непростительное заклятие (простая версия)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define eb emplace_back
void slow_solve(int n, int k, string s, string t) {
set<string> was;
queue<string> q;
q.push(s);
was.insert(s);
auto add = [&](string& s, int i, int j) {
if (i >= 0 && i < j && j < n) {
swap(s[i], s[j]);
if (!was.count(s)) {
was.insert(s);
q.push(s);
}
swap(s[i], s[j]);
}
};
while (!q.empty()) {
s = q.front(); q.pop();
for (int i = 0; i < n; ++i) {
add(s, i, i+k);
add(s, i, i+k+1);
add(s, i-k, i);
add(s, i-k-1, i);
}
}
cout << (was.count(t) ? "Yes" : "No") << '\n';
}
void solve() {
int n,k; cin >> n >> k;
string s; cin >> s;
string t; cin >> t;
if (n <= 5) {
slow_solve(n, k, s, t);
return;
}
map<char, int> cnt;
for (char c : s) {
cnt[c]++;
}
for (char c : t) {
cnt[c]--;
}
bool ok = true;
for (auto [c, x] : cnt) {
ok &= x == 0;
}
cout << (ok ? "Yes" : "No") << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
1800E2 - Непростительное заклятие (сложная версия)
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
#define sz(v) (int)v.size()
#define all(v) v.begin(),v.end()
#define eb emplace_back
void solve() {
int n, k; cin >> n >> k;
string s; cin >> s;
string t; cin >> t;
vector<int> cnt(26, 0);
bool ok = true;
for (int i = 0; i < n; ++i) {
if (i >= k || i+k < n){
cnt[s[i] - 'a']++;
cnt[t[i] - 'a']--;
} else {
ok &= s[i] == t[i];
}
}
cout << (ok && count(all(cnt), 0) == 26 ? "YES" : "NO") << '\n';
}
int main() {
int t;
cin >> t;
while (t--) {
solve();
}
}
Idea: Gornak40
Tutorial
Tutorial is loading...
Solution
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2,avx,fma,bmi2")
#include <bits/stdc++.h>
#include <immintrin.h>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
#define endl '\n'
//#define int long long
#define all(arr) arr.begin(), arr.end()
#define multitest() int _gorilla_silverback; cin >> _gorilla_silverback; while (_gorilla_silverback --> 0)
#define pikachu push_back
#define ls(id) (id << 1 | 1)
#define rs(id) ((id << 1) + 2)
#define sqr(x) ((x) * (x))
#define dlg(x) (31 - __builtin_clz(x))
#define ulg(x) (32 - __builtin_clz(x))
typedef pair<int, int> ipair;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> treap;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MAXN = 200200;
const int L = 26;
int n;
string srr[MAXN];
int arr[MAXN], brr[MAXN], crr[MAXN];
void build() {
for (int i = 0; i < n; ++i) {
for (char c: srr[i]) {
arr[i] ^= (1 << (c - 'a'));
brr[i] |= (1 << (c - 'a'));
}
}
}
long long calc(int c) {
int k = 0;
for (int i = 0; i < n; ++i)
if (brr[i] >> c & 1 ^ 1) crr[k++] = arr[i];
sort(crr, crr + k);
int mask = -1 & ((1 << L) - 1) ^ (1 << c);
long long ans = 0;
for (int i = 0; i < k; ++i) {
auto itl = lower_bound(crr, crr + k, crr[i] ^ mask);
auto itr = upper_bound(crr, crr + k, crr[i] ^ mask);
ans += itr - itl;
}
return ans >> 1LL;
}
long long solve() {
long long ans = 0;
for (int c = 0; c < L; ++c)
ans += calc(c);
return ans;
}
signed main() {
ios_base::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
cin >> n;
for (int i = 0; i < n; ++i)
cin >> srr[i];
build();
cout << solve() << endl;
}
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
#define int long long
#define pb emplace_back
#define mp make_pair
#define x first
#define y second
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
typedef long double ld;
typedef long long ll;
using namespace std;
mt19937 rnd(time(nullptr));
const int inf = 2e18;
const ll M = 1e9;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
int last;
map<vector<int>, int> eq;
map<int, bool> symmetrical;
int dfs(int v, int p, vector<vector<int>> &sl){
vector<int> children;
for(int u: sl[v]){
if(u == p) continue;
children.emplace_back(dfs(u, v, sl));
}
sort(all(children));
if(!eq.count(children)){
map<int, int> cnt;
for(int e: children){
cnt[e]++;
}
int odd = 0, bad = 0;
for(auto e: cnt){
if(e.y & 1){
odd++;
bad += !symmetrical[e.x];
}
}
eq[children] = last;
symmetrical[last] = odd < 2 && bad == 0;
last++;
}
return eq[children];
}
void solve(int tc){
int n;
cin >> n;
eq.clear();
symmetrical.clear();
eq[vector<int>(0)] = 0;
symmetrical[0] = true;
last = 1;
vector<vector<int>> sl(n);
for(int i = 1; i < n; ++i){
int u, v;
cin >> u >> v;
sl[--u].emplace_back(--v);
sl[v].emplace_back(u);
}
cout << (symmetrical[dfs(0, 0, sl)]? "YES" : "NO");
}
bool multi = true;
signed main() {
int t = 1;
if (multi)cin >> t;
for (int i = 1; i <= t; ++i) {
solve(i);
cout << "\n";
}
return 0;
}