Thank you all for participating! 👊👊👊
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, s;
cin >> n >> s;
vector<int> x(n);
for (int i = 0; i < n; i++) cin >> x[i];
int ans = min(abs(s - x[0]), abs(s - x.back())) + x.back() - x[0];
cout << ans << '\n';
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
vector<int> cnt(26, 0);
for (auto c : s) cnt[c - 'a']++;
int flag = 0;
for (int i = 0; i < 26; i++) {
if (cnt[i] >= 3) flag = 1;
else if (cnt[i] == 2 && (s[0] - 'a' != i || s.back() - 'a' != i)) flag = 1;
}
if (flag) cout << "Yes" << '\n';
else cout << "No" << '\n';
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, m;
cin >> n >> m;
vector<vector<int>> a(n, vector<int> (m));
int mx = 0, cnt_mx = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
cin >> a[i][j];
if (a[i][j] > mx) {
mx = a[i][j], cnt_mx = 1;
} else if (a[i][j] == mx) {
cnt_mx++;
}
}
}
vector<int> r(n), c(m);
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i][j] == mx) {
r[i]++;
c[j]++;
}
}
}
int flag = 0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (r[i] + c[j] - (a[i][j] == mx) == cnt_mx) {
flag = 1;
}
}
}
cout << mx - flag << '\n';
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
vector<int> a(n), b(n);
for (int i = 0; i < n; i++) cin >> a[i];
for (int i = 0; i < n; i++) cin >> b[i];
vector<pair<int, int>> ans;
for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
if (a[j - 1] > a[j]) {
swap(a[j - 1], a[j]);
ans.push_back({1, j});
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 1; j < n; j++) {
if (b[j - 1] > b[j]) {
swap(b[j - 1], b[j]);
ans.push_back({2, j});
}
}
}
for (int i = 0; i < n; i++) {
if (a[i] > b[i]) {
ans.push_back({3, i + 1});
}
}
cout << ans.size() << '\n';
for (auto [x, y] : ans) cout << x << " " << y << '\n';
}
return 0;
}
2121E - Sponsor of Your Problems
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
string l, r;
cin >> l >> r;
if (l == r) {
cout << 2 * l.size() << '\n';
continue;
}
int ptr = 0;
while (ptr < l.size() && l[ptr] == r[ptr]) ptr++;
if (l[ptr] + 1 < r[ptr]) {
cout << 2 * ptr << '\n';
} else {
int res = 2 * ptr + 1;
for (int i = ptr + 1; i < l.size(); i++) {
if (l[i] == '9' && r[i] == '0') res++;
else break;
}
cout << res << '\n';
}
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 2e5 + 5;
int a[maxn];
ll pref[maxn], s;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n, x;
cin >> n >> s >> x;
for (int i = 1; i <= n; i++) {
cin >> a[i];
pref[i] = pref[i - 1] + a[i];
}
ll ans = 0;
map<ll, int> cnt;
int lef = 1;
for (int r = 1; r <= n; r++) {
if (a[r] > x) cnt.clear(), lef = r + 1;
else if (a[r] == x) {
while (lef <= r) {
cnt[pref[lef - 1]]++;
lef++;
}
}
ans += cnt[pref[r] - s];
}
cout << ans << '\n';
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int const maxn = 2e5 + 5;
int pref[maxn];
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
string s;
cin >> s;
ll ans = 0;
for (int i = 0; i < n; i++) {
pref[i + 1] = pref[i];
if (s[i] == '0') pref[i + 1]--;
else pref[i + 1]++;
}
for (int i = 1; i <= n; i++) {
ans += (ll)i * (n - i + 1);
}
sort(pref, pref + n + 1);
for (int i = 0; i <= n; i++) {
ans += (ll)pref[i] * (i - (n - i));
}
cout << ans / 2 << '\n';
}
return 0;
}
Tutorial
Tutorial is loading...
Code
#include<bits/stdc++.h>
using namespace std;
main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int t;
cin >> t;
while (t--) {
int n;
cin >> n;
multiset<int> dp;
for (int i = 1; i <= n; i++) {
int l, r;
cin >> l >> r;
auto it = dp.upper_bound(r);
if (it != dp.end()) {
dp.erase(it);
}
dp.insert(l);
cout << dp.size() << " ";
}
cout << '\n';
}
return 0;
}



