A. Make it White
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
void solve() {
int n;
string s;
cin >> n >> s;
int l = 0 , r = n - 1;
while( l < n and s[l] == 'W') l ++;
while( r >= 0 and s[r] == 'W' ) r --;
cout << max( 0ll , r - l + 1 ) << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int TC;
for( cin >> TC ; TC ; TC -- )
solve();
return 0;
}
B. Following the String
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
void solve() {
int n;
cin >> n;
vi cnt(26);
for (int i = 0, x; i < n; i++) {
cin >> x;
for (int j = 0; j < 26; j++) {
if (cnt[j] != x) continue;
cout << char(j + 'a');
cnt[j]++;
break;
}
}
cout << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int TC;
for (cin >> TC; TC; TC--)
solve();
return 0;
}
C. Choose the Different Ones!
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
void solve() {
int n, m, k;
cin >> n >> m >> k;
vector<bool> a(k), b(k), c(k);
for (int i = 1, x; i <= n; i++) {
cin >> x, x--;
if (x < k) a[x] = 1, c[x] = 1;
}
for (int i = 1, x; i <= m; i++) {
cin >> x, x--;
if (x < k) b[x] = 1, c[x] = 1;
}
if (accumulate(a.begin(), a.end(), 0) < k / 2) {
cout << "NO\n";
return;
}
if (accumulate(b.begin(), b.end(), 0) < k / 2) {
cout << "NO\n";
return;
}
if (accumulate(c.begin(), c.end(), 0) < k) {
cout << "NO\n";
return;
}
cout << "YES\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int TC;
for (cin >> TC; TC; TC--)
solve();
return 0;
}
D. Find the Different Ones!
这题我用了区间最值查询来实现
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
int lgN;
vi lg2(2e5 + 1);
void solve() {
int n;
cin >> n;
vi a(n + 1);
for (int i = 1; i <= n; i++) cin >> a[i];
auto exMax = [&a](int i, int j) {
if (a[i] > a[j]) return i;
else return j;
};
auto exMin = [&a](int i, int j) {
if (a[i] < a[j]) return i;
return j;
};
vector f(n + 1, vi(lgN + 1)), g(n + 1, vi(lgN + 1));
for (int i = 1; i <= n; i++)
f[i][0] = g[i][0] = i;
for (int j = 1; j <= lgN; j++)
for (int i = 1; i + (1 << j) - 1 <= n; i++) {
f[i][j] = exMax(f[i][j - 1], f[i + (1 << j - 1)][j - 1]);
g[i][j] = exMin(g[i][j - 1], g[i + (1 << j - 1)][j - 1]);
}
auto calcMax = [&exMax, &f](int l, int r) {
int s = lg2[r - l + 1];
return exMax(f[l][s], f[r - (1 << s) + 1][s]);
};
auto calcMin = [&exMin, &g](int l, int r) {
int s = lg2[r - l + 1];
return exMin(g[l][s], g[r - (1 << s) + 1][s]);
};
int q;
cin >> q;
for (int l, r, i, j; q; q--) {
cin >> l >> r;
i = calcMax(l, r), j = calcMin(l, r);
if (a[i] == a[j]) i = j = -1;
cout << i << " " << j << "\n";
}
cout << "\n";
return;
}
void init() {
lg2[0] = -1;
for (int i = 1; i < lg2.size(); i++)
lg2[i] = lg2[i / 2] + 1;
lgN = lg2.back();
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
init();
int TC;
for (cin >> TC; TC; TC--)
solve();
return 0;
}
E. Klever Permutation
一个神秘的构造题,简单来说就是要求任意长度为k的子区间的和之差不超过1,这样的话实际上也就是要要求相邻区间的变化应该是加一或减一这样的,因此我们可以规定奇数位为加1,这样分别从两端开始填入就好
#include<bits/stdc++.h>
using namespace std;
using i32 = int32_t;
using i64 = long long;
using i128 = __int128;
using ldb = long double;
#define int i64
using vi = vector<int>;
using pii = pair<int, int>;
using vii = vector<pii>;
const int inf = INT_MAX, INF = 1e18;
const int mod = 998244353;
const vi dx = {0, 0, 1, -1}, dy = {1, -1, 0, 0};
using edge = array<int, 3>;
int power(int x, int y) {
int ans = 1;
while (y) {
if (y & 1) ans = ans * x % mod;
x = x * x % mod, y /= 2;
}
return ans;
}
int inv(int x) {
return power(x, mod - 2);
}
void solve() {
int n, k, m;
cin >> n >> k;
vi res(n + 1);
int l = 1, r = n;
for (int i = 1; i <= k; i++) {
for (int j = i; j <= n; j += k) {
if (i & 1)
res[j] = l++;
else
res[j] = r--;
}
}
for( int i = 1 ; i <= n ; i ++ )
cout << res[i] << " ";
cout << "\n";
return;
}
i32 main() {
ios::sync_with_stdio(false), cin.tie(nullptr);
int TC;
for (cin >> TC; TC; TC--)
solve();
return 0;
}
标签:return,int,vi,Codeforces,ans,using,Div,923,mod
From: https://www.cnblogs.com/PHarr/p/18020136