Идея: myav
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n;
cin >> n;
int sq = ceil(sqrt(n));
if (sq * sq == n) {
cout << 0 << ' ' << sq << "\n";
} else {
cout << "-1\n";
}
}
int main() {
int t;
cin >> t;
while (t--) solve();
}
2114B - Не очень палиндромная строка
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#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 = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
void solve(int tc){
int n, k;
cin >> n >> k;
string s;
cin >> s;
vector<int> cnt(2);
for(char c: s){
cnt[c - '0']++;
}
int mn = max(cnt[0], cnt[1]) - n / 2;
int mx = cnt[0] / 2 + cnt[1] / 2;
if(k >= mn && (k - mn) % 2 == 0 && k <= mx) cout << "YES";
else cout << "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;
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
void solve(int tc){
int n;
cin >> n;
int last = -1, ans = 0;
for(int i = 0; i < n; ++i){
int a;
cin >> a;
if(a - last > 1){
ans++;
last = a;
}
}
cout << ans;
}
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;
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#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 = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
struct min_max{
int mx1, mx2, mn1, mn2;
void fix_mx(){
if(mx1 < mx2){
swap(mx1, mx2);
}
}
void fix_mn(){
if(mn1 > mn2){
swap(mn1, mn2);
}
}
min_max(int a, int b){
mx1 = mn1 = a;
mx2 = mn2 = b;
fix_mx();
fix_mn();
}
void add(int x){
mx2 = max(mx2, x);
mn2 = min(mn2, x);
fix_mx();
fix_mn();
}
int get_seg(int x){
pair<int, int> res = {mn1, mx1};
if(x == mn1) res.x = mn2;
if(x == mx1) res.y = mx2;
return res.y - res.x + 1;
}
};
void solve(int tc){
int n;
cin >> n;
vector<pair<int, int>> coord(n);
for(auto &e: coord){
cin >> e.x >> e.y;
}
if(n <= 2){
cout << n;
return;
}
min_max xc(coord[0].x, coord[1].x), yc(coord[0].y, coord[1].y);
for(int i = 2; i < n; ++i){
xc.add(coord[i].x);
yc.add(coord[i].y);
}
int ans = xc.get_seg(-1) * yc.get_seg(-1);
for(int i = 0; i < n; ++i){
int x = xc.get_seg(coord[i].x);
int y = yc.get_seg(coord[i].y);
if(x * y == n - 1){
ans = min(ans, min((x + 1) * y, x * (y + 1)));
}
else{
ans = min(ans, x * y);
}
}
cout << ans;
}
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;
}
2114E - Кирей атакует поместье
Идея: Gornak40
Разбор
Tutorial is loading...
Решение
from math import inf
from sys import setrecursionlimit
def solve(v, p, mini, maxi):
global res
res[v] = max(arr[v], mini * -1 + arr[v])
mini = min(arr[v], maxi * -1 + arr[v])
for u in gr[v]:
if u == p:
continue
solve(u, v, mini, res[v])
setrecursionlimit(400_000)
t = int(input())
for _ in range(t):
n = int(input())
arr = list(map(int, input().split()))
gr = [[] for _ in range(n)]
for j in range(n - 1):
v, u = map(int, input().split())
gr[v - 1].append(u - 1)
gr[u - 1].append(v - 1)
res = [0] * n
solve(0, -1, 0, 0)
print(*res)
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#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 = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
int get_ans(int x, int k){
if(x == 1) return 0;
vector<int> divs;
for(int i = 1; i * i <= x; i++){
if(x % i == 0){
divs.push_back(i);
divs.push_back(x / i);
}
}
sort(all(divs));
int n = divs.size();
vector<int> dp(n, 100);
dp[0] = 0;
for(int i = 1; i < n; i++){
for(int j = i - 1; j >= 0; j--){
if(divs[i] / divs[j] > k){
break;
}
if(divs[i] % divs[j] == 0) {
dp[i] = min(dp[i], dp[j] + 1);
}
}
}
return dp[n - 1] == 100 ? -1 : dp[n - 1];
}
void solve(int tc){
int x, y, k;
cin >> x >> y >> k;
int g = gcd(x, y);
x /= g;
y /= g;
int ax = get_ans(x, k);
int ay = get_ans(y, k);
if(ax == -1 || ay == -1) cout << -1;
else cout << ax + ay;
}
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;
}
Идея: myav
Разбор
Tutorial is loading...
Решение
#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 = 1e9;
const int M = 1e9 + 7;
const ld pi = atan2(0, -1);
const ld eps = 1e-6;
int max_op(int a, int b) {
int min_part = a;
while (min_part % 2 == 0 && min_part / 2 != b) {
min_part /= 2;
}
if (min_part % 2 == 1) {
return a / min_part;
}
int true_min = min_part;
while (true_min % 2 == 0) {
true_min /= 2;
}
return 1 + (a - min_part) / true_min;
}
void solve(int tc){
int n, k;
cin >> n >> k;
vector<int> a(n);
for (int &e: a) cin >> e;
vector<int> pre(n, 0);
for (int j = 1; j < n; ++j) {
pre[j] = pre[j - 1] + max_op(a[j - 1], a[j]);
}
vector<int> suf(n, 0);
for (int j = n - 2; j >= 0; --j) {
suf[j] = suf[j + 1] + max_op(a[j + 1], a[j]);
}
for (int i = 0; i < n; i++) {
int res = max_op(a[i], 0) + pre[i] + suf[i];
if (res >= k) {
cout << "YES";
return;
}
}
cout << "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;
}



