A
除了1以外每个数都可以被质数构成
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t; cin >> t;
while (t--) {
ll x, y; cin >> x >> y;
if (x == y || x == y + 1) cout << "No" << endl;
else cout << "Yes" << endl;
}
}
B
排序去重,暴力
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + 5;
int a[maxn];
int main()
{
int q; cin >> q;
while (q--) {
int n, r; cin >> n >> r;
for (int i = 1; i <= n; ++i)
cin >> a[i];
sort(a + 1, a + n + 1);
n = unique(a + 1, a + n + 1) - a - 1;
int sum = 0, cnt = 0;
for (int i = n; i >= 1; --i) {
if (a[i] <= sum) break;
cnt++; sum += r;
}
cout << cnt << endl;
}
}
C
贪心,判断下一块板和下下一块板的距离差,大于2则将下一块板下面的板露出来
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
、int h[maxn];
int main()
{
int q; cin >> q;
while (q--) {
int H, n; cin >> H >> n;
for (int i = 1; i <= n; ++i)
cin >> h[i];
h[++n] = 0;
int ans = 0;
for (int i = 1; i < n; ++i) {
if (i == n - 1) continue;
if (h[i + 1] == h[i + 2] + 1) i++;
else {
h[i + 1]--;
ans++;
}
}
cout << ans << endl;
}
}
D
正难则反,思考存在字符不属于任意回文串的字串数量。
一个字串,有一种字母只出现一次而且出现在字串头尾,该字串就不合法
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 3e5 + 5;
char str[maxn];
ll a[maxn];
int main()
{
int n;
scanf("%d%s", &n, &str);
int cnt = 0, tot = 0;
for (int i = 0; i < n; ++i) {
cnt++;
if (str[i] != str[i + 1]) {
a[tot++] = cnt;
cnt = 0;
}
}
ll ans = (ll)n * (n - 1) / 2;
for (int i = 1; i < tot; ++i)
ans -= (a[i] + a[i - 1] - 1);
printf("%lld\n", ans);
}