A
根据题意,a为奇数时不可能成立,a为0且b为奇数时也不可能成立,其余情况都成立
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int a, b;
cin >> a >> b;
bool f = true;
if (a % 2 == 1 || a==0&&b%2==1) f = false;
if (f) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
B
因为原始矩阵为美丽矩阵,所以n为平方数,再检查矩阵首尾两行所有字符是否均为‘1’,其余行首尾为’1’,中间为’0’
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int n;
string s;
cin >> n >> s;
bool ok = true;
int a = floor(sqrt(n));
if (a * a != n) { puts("NO"); return; }
else {
for (int i = 0; i < n; i += a) {
if (i == 0 || i == n - a) {
for (int j = 0; j < a; j++) if (s[i + j] != '1') { puts("NO"); return; }
}
else {
string ss = "1";
for (int k = 0; k < a - 2; k++) ss += "0";
ss += "1";
if (s.substr(i, a) != ss) { puts("NO"); return; }
}
}
}
puts("YES");
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
C
根据题目一个一个累加,判断结束条件
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
void solve() {
int l, r;
cin >> l >> r;
int ans = 0;
int sum = l;
int i = 1;
while (1) {
sum += i;
if (sum > r) break;
if (sum == r) { i++; break; }
i++;
}
cout << i << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int t;
cin >> t;
while (t--) {
solve();
}
return 0;
}
D
分析样例二发现一个circle内的所有元素a1,a2,a3...am
有F(a1) = F(a2) = F(a3) = ··· = f(am)
,所以可以在每个circle内求值,遍历的时候进行记忆化
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n;
int* p = new int[n + 1];
bool* st = new bool[n + 1] {0};
for (int i = 1; i <= n; ++i) cin >> p[i];
cin >> s;
vector<int>ans(n + 1);
for (int i = 1; i <= n; ++i) {
if (!st[i]) {
st[i] = 1;
vector<int>circle;
int c = p[i];
circle.push_back(c);
c = p[c];
st[c] = 1;
while (c != p[i]) {
circle.push_back(c);
c = p[c];
st[c] = 1;
}
int cnt = 0;
for (int e : circle) {
if (s[e - 1] == '0')
cnt++;
}
for (int e : circle) ans[e] = cnt;
}
}
for (int i = 1; i <= n; i++) cout << ans[i] << ' ';
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
E
当n
是偶数时,我们只要分别找到奇数位和偶数位相同字符个数的最大值max1
,max2
,答案就是n - max1 - max2
当n
是奇数时,需要考虑删除一个字符后产生的影响,我们可以倒序遍历每一个字符,统计删除ai
后ai
前面和ai
后面奇数位偶数位的字母数量,求最大值
#include<bits/stdc++.h>
using namespace std;
void solve() {
int n;
string s;
cin >> n >> s;
s = " " + s;
int cnt[3][27]{ 0 };
int max1 = 0, max2 = 0;
for (int i = 1; i < s.size(); ++i) {
if (i & 1) {
cnt[1][s[i] - 'a']++;
max1 = max(max1, cnt[1][s[i] - 'a']);
}
else {
cnt[2][s[i] - 'a']++;
max2 = max(max2, cnt[2][s[i] - 'a']);
}
}
if (n % 2 == 0) cout << n - max1 - max2 << endl;
else {
int b[3][30] = { 0 };
int mx = 0;
for (int i = n; i >= 1; --i) {
int k1 = 0, k2 = 0;
if (i & 1) cnt[1][s[i] - 'a']--;
else cnt[2][s[i] - 'a']--;
for (int j = 0; j < 26; j++) {
int x = cnt[1][j], y = cnt[2][j];
x += b[2][j];
y += b[1][j];
k1 = max(k1, x);
k2 = max(k2, y);
}
mx = max(mx, k1 + k2);
if (i % 2 == 1) b[1][s[i] - 'a']++;
else b[2][s[i] - 'a']++;
}
cout << n - mx << endl;
}
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T;
cin >> T;
while (T--) {
solve();
}
return 0;
}
F
逆元,以后再补