Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
full = 'Yes' * 18
t = int(input())
for _ in range(t):
if full.find(input()) >= 0:
print('YES')
else:
print('NO')
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
t = int(input())
for _ in range(t):
n, s = map(int, input().split())
a = [int(x) for x in input().split()]
s += sum(a)
sm = 0
cnt = 0
for i in range(1, s + 1):
if sm >= s:
break
sm += i
cnt = i
if sm != s or max(a) > cnt or cnt <= n:
print("NO");
else:
print("YES")
Idea: Vladosiya
Tutorial
Tutorial is loading...
Solution
def solve():
l, r, x = map(int, input().split())
a, b = map(int, input().split())
if a == b:
return 0
if abs(a - b) >= x:
return 1
if r - max(a, b) >= x or min(a, b) - l >= x:
return 2
if r - b >= x and a - l >= x or r - a >= x and b - l >= x:
return 3
return -1
t = int(input())
for _ in range(t):
print(solve())
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#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()
#define all(v) v.begin(),v.end()
#define eb emplace_back
using ll = long long;
void solve() {
ll n,m; cin >> n >> m;
ll n0 = n;
int cnt2 = 0, cnt5 = 0;
ll k = 1;
while (n > 0 && n % 2 == 0) {
n /= 2;
cnt2++;
}
while (n > 0 && n % 5 == 0) {
n /= 5;
cnt5++;
}
while (cnt2 < cnt5 && k * 2 <= m) {
cnt2++;
k *= 2;
}
while (cnt5 < cnt2 && k * 5 <= m) {
cnt5++;
k *= 5;
}
while (k * 10 <= m) {
k *= 10;
}
if (k == 1) {
cout << n0 * m << endl;
} else {
k *= m / k; // 1 <= m/k < 10
cout << n0 * k << endl;
}
}
int main() {
int t;
cin >> t;
forn(tt, t) {
solve();
}
}
Idea: Gornak40
Tutorial
Tutorial is loading...
Solution
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 200200;
int n;
int arr[MAXN];
int solve(int i, long long h, int s2, int s3) {
if (i == n) return 0;
if (arr[i] < h)
return solve(i + 1, h + (arr[i] / 2), s2, s3) + 1;
int ans1 = (s2 ? solve(i, h * 2, s2 - 1, s3) : 0);
int ans2 = (s3 ? solve(i, h * 3, s2, s3 - 1) : 0);
return max(ans1, ans2);
}
int main() {
int t; cin >> t;
while(t--) {
long long h; cin >> n >> h;
for (int i = 0; i < n; ++i)
cin >> arr[i];
sort(arr, arr + n);
cout << solve(0, h, 2, 1) << endl;
}
}
Idea: senjougaharin
Tutorial
Tutorial is loading...
Solution
#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef tree<pair<int, int>, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
int newDigit = -1;
bool check(set<int> digits, int l, int r, bool useNewDigit) {
for (int i = l; i <= r; ++i) {
if (useNewDigit && i == newDigit) {
continue;
}
if (!digits.count(i)) {
return false;
}
}
return true;
}
void solve() {
int n, p;
cin >> n >> p;
vector<int> a(n + 1);
set<int> digits;
for (int i = 1; i <= n; ++i) {
cin >> a[i];
digits.insert(a[i]);
}
if (digits.size() == p) {
cout << "0\n";
return;
}
for (int i = n - 1; i >= 0; --i) {
if (a[i] < p - 1) {
newDigit = a[i] + 1;
break;
}
}
int l = 0, r = p - 1;
int x = a[n];
while (l < r) {
int m = (l + r) >> 1;
bool res = false;
if (x + m >= p) {
if (check(digits, x + m + 1 - p, x - 1, true)) {
res = true;
}
} else {
if (check(digits, 0, x - 1, false) && check(digits, x + m + 1, p - 1, false)) {
res = true;
}
}
if (res) {
r = m;
} else {
l = m + 1;
}
}
cout << l << '\n';
}
bool multitest = true;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cout.precision(25);
size_t number_of_tests = 1;
if (multitest) {
cin >> number_of_tests;
}
for (size_t _ = 0; _ < number_of_tests; ++_) {
solve();
}
return 0;
}
1759G - Restore the Permutation
Idea: MikeMirzayanov
Tutorial
Tutorial is loading...
Solution
#include "bits/stdc++.h"
using namespace std;
int n;
void solve(){
cin >> n;
vector<int>b(n / 2), p(n);
vector<bool>isUsed(n + 1, false);
set<int>unused;
for(int i = 0; i < n / 2; i++){
cin >> b[i];
p[i * 2 + 1] = b[i];
isUsed[b[i]] = true;
}
for(int i = 1; i <= n; i++){
if(!isUsed[i]) unused.insert(i);
}
if(int(unused.size()) != n / 2){
cout << "-1\n";
return;
}
for(int i = n / 2 - 1; i >= 0; i--){
auto k = unused.upper_bound(p[2 * i + 1]);
if(k == unused.begin()){
cout << "-1\n";
return;
}
k--;
if(*k < p[2 * i + 1]){
p[2 * i] = *k;
unused.erase(k);
}
else{
cout << "-1\n";
return;
}
}
for(auto i : p) cout << i << ' ';
cout << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while(t--){
solve();
}
}