Идея: kbats183
Решение
Tutorial is loading...
Код
def solve():
n, m = [int(i) for i in input().split()]
ans = 0
for i in range(n):
l = input()
if len(l) <= m:
m -= len(l)
ans += 1
else:
for i in range(i + 1, n):
input()
break
print(ans)
t = int(input())
for i in range(t):
solve()
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
int n; cin >> n;
vector<int> a(n);
for (int &x : a) cin >> x;
long long ods = 0, evs = 0;
for (int i = 0; i < n; i++) {
if (i & 1) ods += a[i];
else evs += a[i];
}
int odc = n / 2, evc = n / 2;
if (n & 1) evc++;
if (ods % odc != 0 || evs % evc != 0 || ods / odc != evs / evc) {
cout << "NO";
return;
}
cout << "YES";
}
int main() {
int TESTS; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << '\n';
}
return 0;
}
Идея: DanGolov
Решение
Tutorial is loading...
Код
def solve():
s = [int(x) for x in list(input())]
sm = sum(s)
twos = s.count(2)
threes = s.count(3)
for i in range(min(10, twos + 1)):
for j in range(min(10, threes + 1)):
if (sm + i * 2 + j * 6) % 9 == 0:
print('YES')
return
print('NO')
t = int(input())
for _ in range(t):
solve()
2050D - Максимизация цифровой строки
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
void solve() {
string s; cin >> s;
for (int i = 0; i < s.size(); i++) {
int best = s[i] - '0', pos = i;
for (int j = i; j < min(i + 10, (int) s.size()); j++) {
if (s[j] - '0' - (j - i) > best) {
best = s[j] - '0' - (j - i);
pos = j;
}
}
while (pos > i) {
swap(s[pos], s[pos - 1]);
pos--;
}
s[i] = char(best + '0');
}
cout << s;
}
int main() {
int TESTS = 1; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << '\n';
}
return 0;
}
Решение
Tutorial is loading...
Код
#include <iostream>
#include <algorithm>
static const int inf = 1e9;
void solve() {
std::string a, b, res;
std::cin >> a >> b >> res;
int n = (int) a.size(), m = (int) b.size();
int dp[n + 1][m + 1];
std::fill(&dp[0][0], &dp[0][0] + (n + 1) * (m + 1), inf);
dp[0][0] = 0;
for (int i = 0; i < n; i++) {
dp[i + 1][0] = dp[i][0] + (a[i] != res[i]);
}
for (int j = 0; j < m; j++) {
dp[0][j + 1] = dp[0][j] + (b[j] != res[j]);
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
dp[i][j] = std::min(dp[i - 1][j] + (a[i - 1] != res[i + j - 1]),
dp[i][j - 1] + (b[j - 1] != res[i + j - 1]));
}
}
std::cout << dp[n][m] << std::endl;
}
int main() {
int tests;
std::cin >> tests;
while (tests--) {
solve();
}
}
2050F - Максимальное модульное равенство
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
using namespace std;
const int LOGN = 20;
vector<vector<int>> stGCD;
int get_gcd(int l, int r) {
int k = __lg(r - l + 1);
return __gcd(stGCD[k][l], stGCD[k][r - (1 << k) + 1]);
}
void solve() {
stGCD.clear();
int n, q; cin >> n >> q;
vector<int> a(n);
for (int &x : a) cin >> x;
vector<int> b;
for (int i = 1; i < n; i++)
b.push_back(abs(a[i - 1] - a[i]));
stGCD.resize(LOGN, vector<int>(b.size(), 1));
for (int i = 0; i < b.size(); i++)
stGCD[0][i] = b[i];
for (int i = 1; i < LOGN; i++)
for (int j = 0; j + (1 << (i - 1)) < b.size(); j++)
stGCD[i][j] = __gcd(stGCD[i - 1][j], stGCD[i - 1][j + (1 << (i - 1))]);
while (q--) {
int l, r; cin >> l >> r;
if (l == r) {
cout << 0 << " ";
continue;
}
l--; r -= 2;
int gcd = get_gcd(l, r);
cout << gcd << " ";
}
}
int main() {
int TESTS = 1; cin >> TESTS;
while (TESTS --> 0) {
solve();
cout << "\n";
}
return 0;
}
Идея: AVdovin
Решение
Tutorial is loading...
Код
#include <bits/stdc++.h>
#define int long long
#define x first
#define y second
using namespace std;
void dfs(int v, int p, vector<vector<int>> &sl, vector<pair<int, int>> &dp){
dp[v].x = sl[v].size();
int m1 = -1, m2 = -1;
for(int u: sl[v]){
if(u == p){
continue;
}
dfs(u, v, sl, dp);
dp[v].x = max(dp[v].x, dp[u].x + (int)sl[v].size() - 2);
m2 = max(m2, dp[u].x);
if(m1 < m2) swap(m1, m2);
}
dp[v].y = dp[v].x;
if(m2 != -1){
dp[v].y = m1 + m2 + sl[v].size() - 4;
}
}
void solve(int tc){
int n;
cin >> n;
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);
}
vector<pair<int, int>> dp(n);
dfs(0, 0, sl, dp);
int ans = 0;
for(int i = 0; i < n; ++i){
ans = max(ans, max(dp[i].x, dp[i].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;
}