1624A - Plus One on the Subset
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define forn(i, n) for (int i = 0; i < int(n); i++)
void solve() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
int MIN = INT_MAX;
int MAX = INT_MIN;
for (int i = 0; i < n; ++i) {
MIN = min(MIN, a[i]);
MAX = max(MAX, a[i]);
}
cout << MAX - MIN << '\n';
}
int main() {
int tests;
cin >> tests;
forn(tt, tests) {
solve();
}
}
Идея: senjougaharin
Разбор
Tutorial is loading...
Решение
#include<bits/stdc++.h>
using namespace std;
void solveTest() {
int a, b, c;
cin >> a >> b >> c;
int new_a = b - (c - b);
if(new_a >= a && new_a % a == 0 && new_a != 0) {
cout << "YES\n";
return;
}
int new_b = a + (c - a)/2;
if(new_b >= b && (c-a)%2 == 0 && new_b % b == 0 && new_b != 0) {
cout << "YES\n";
return;
}
int new_c = a + 2*(b - a);
if(new_c >= c && new_c % c == 0 && new_c != 0) {
cout << "YES\n";
return;
}
cout << "NO\n";
return;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int tt;
cin >> tt;
while(tt--)
solveTest();
return 0;
}
1624C - Division by Two and Permutation
Идея: MikeMirzayanov
Разбор
Tutorial is loading...
Решение
#include<bits/stdc++.h>
using namespace std;
void solve(){
int n;
cin >> n;
vector<int>a(n), used(n + 1, false);
for(auto &i : a) cin >> i;
sort(a.begin(), a.end(), [] (int a, int b) {
return a > b;
});
bool ok = true;
for(auto &i : a){
int x = i;
while(x > n or used[x]) x /= 2;
if(x > 0) used[x] = true;
else ok = false;
}
cout << (ok ? "YES" : "NO") << '\n';
}
int main(){
ios_base :: sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
solve();
}
return 0;
}
Идея: senjougaharin
Разбор
Tutorial is loading...
Решение
#include <iostream>
#include <vector>
#include <stack>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MAX_N = 2e5;
int main(int argc, char* argv[]) {
int t;
cin >> t;
for (int _ = 0; _ < t; ++_) {
int n, k;
string s;
cin >> n >> k >> s;
vector<int> cnt(26);
for (char c : s) {
cnt[c - 'a']++;
}
int cntPairs = 0, cntOdd = 0;
for (int c : cnt) {
cntPairs += c / 2;
cntOdd += c % 2;
}
int ans = 2 * (cntPairs / k);
cntOdd += 2 * (cntPairs % k);
if (cntOdd >= k) {
ans++;
}
cout << ans << '\n';
}
}
Идея: Aris
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define sz(v) (int)v.size()
const int N = 1e4;
map<string, bool> have;
map<string, tuple<int,int,int>> pos;
void solve() {
int n, m; cin >> n >> m;
vector<bool> dp(m+1, false);
vector<int> pr(m+1);
vector<string> cache;
dp[0] = true;
forn(i, n) {
string s; cin >> s;
forn(j, m) {
string t;
t += s[j];
for(int k = 1; k <= 2; k++) {
if (k + j >= m) break;
t += s[j+k];
if (!have[t]) {
have[t] = true;
pos[t] = make_tuple(j, j+k, i);
cache.push_back(t);
}
}
}
}
string s; cin >> s;
forn(i, m) {
string t;
t += s[i];
for (int k = 1; k <= 2; k++) {
if (i - k < 0) break;
t = s[i-k] + t;
if (have[t] && dp[i-k]) {
dp[i+1] = true;
pr[i+1] = i-k;
}
if (dp[i+1]) break;
}
}
for (string t : cache) {
have[t] = false;
}
if (!dp[m]) {
cout << "-1\n";
return;
}
vector<tuple<int,int,int>> ans;
for (int k = m; k > 0; ) {
int p = pr[k];
string t = s.substr(p, k - p);
ans.emplace_back(pos[t]);
k = p;
}
cout << sz(ans) << '\n';
reverse(ans.begin(), ans.end());
for (auto [l,r,i] : ans) cout << l+1 << ' ' << r+1 << ' ' << i+1 << '\n';
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
#define int long long
#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(143);
const int inf = 1e10;
const int M = 998244353;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;
signed main(){
int n;
cin >> n;
int l = 1, r = n;
int div = 0;
while(r - l > 1){
int mid = (r + l) / 2;
cout << "+ "<< n - mid << endl;
int d;
cin >> d;
if(d > div)l = mid;
else r = mid;
l = (l + n - mid) % n;
r = (r + n - mid) % n;
if(r == 0) r = n;
div = d;
}
cout << "! " << div * n + l;
return 0;
}
Идея: Vladosiya
Разбор
Tutorial is loading...
Решение
#include <bits/stdc++.h>
#define int long long
#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(143);
const int inf = 1e9;
const int M = 998244353;
const ld pi = atan2(0, -1);
const ld eps = 1e-4;
int n, cur;
vector<vector<pair<int, int>>> sl;
void dfs(int v, vector<bool> &used){
used[v] = true;
for(auto e: sl[v]){
int u = e.x, w = e.y;
if(!used[u] && (cur | w) == cur){
dfs(u, used);
}
}
}
void cnt(int pw){
if(pw < 0) return;
int d = (ll) 1 << pw;
cur -= d;
vector<bool> used(n);
dfs(0, used);
for(bool b: used){
if(!b) {
cur += d;
break;
}
}
cnt(pw - 1);
}
void solve() {
int m;
cin >> n >> m;
sl.assign(n, vector<pair<int, int>>(0));
for(int i = 0; i < m; ++i){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
sl[u].emplace_back(v, w);
sl[v].emplace_back(u, w);
}
cur = 1;
int bit = 0;
for(; cur < inf; bit++){
cur = 2 * cur + 1;
}
cnt(bit);
cout << cur;
}
bool multi = true;
signed main() {
int t = 1;
if (multi) {
cin >> t;
}
for (; t != 0; --t) {
solve();
cout << "\n";
}
return 0;
}