A
暴力模拟每个点坐标-1,不变,+1的情况
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dis(ll a, ll b, ll c) {
return abs(a - b) + abs(b - c) + abs(a - c);
}
int main()
{
int q; cin >> q;
while (q--) {
ll a, b, c; cin >> a >> b >> c;
ll ans = dis(a, b, c);
for (int i = -1; i <= 1; ++i) {
for (int j = -1; j <= 1; ++j) {
for (int k = -1; k <= 1; ++k) {
ans = min(ans, dis(a + i, b + j, c + k));
}
}
}
cout << ans << endl;
}
}
B
要形成回路,L和R的数量相同,U和D的数量相同,注意特判上下和左右有一个为0的情况
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int q; cin >> q;
while (q--) {
string str; cin >> str;
int n = str.length();
int cnt[5] = {0};
for (int i = 0; i < n; ++i) {
if (str[i] == 'L') cnt[0]++;
else if (str[i] == 'R') cnt[1]++;
else if (str[i] == 'U') cnt[2]++;
else cnt[3]++;
}
int min_h = min(cnt[0], cnt[1]);
int min_v = min(cnt[2], cnt[3]);
if (min_h == 0 && min_v == 0) {
printf("0\n\n");
}
else if (min_h == 0) {
printf("%d\n%s\n", 2, "UD");
}
else if (min_v == 0) {
printf("%d\n%s\n", 2, "LR");
}
else {
printf("%d\n", (min_h + min_v) * 2);
for (int i = 0; i < min_h; ++i)
putchar('L');
for (int i = 0; i < min_v; ++i)
putchar('U');
for (int i = 0; i < min_h; ++i)
putchar('R');
for (int i = 0; i < min_v; ++i)
putchar('D');
putchar('\n');
}
}
}
C
考虑每个可显示字母的贡献,可以与前面相邻的长度为len的串形成len+1种子串
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
char s[200005], x[5];
int main()
{
//freopen("in.txt", "r", stdin);
int n, m; scanf("%d%d", &n, &m);
scanf("%s", &s);
int vis[30] = {0};
for (int i = 0; i < m; ++i) {
scanf("%s", &x);
vis[x[0] - 'a'] = 1;
}
ll ans = 0, cnt = 0;
for (int i = 0; i < n; ++i) {
if (!vis[s[i] - 'a']) {
cnt = 0;
continue;
}
ans += (++cnt);
}
printf("%lld\n", ans);
}
D
处理前后缀,枚举断点
#include<stdio.h>
#include<algorithm>
using namespace std;
int a[1000005];
int b[1000005];
int c[1000005];
int main()
{
int i,j,m,n;
scanf("%d",&n);
for(i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
b[0]=0,a[0]=0;
int ans=0;
for(i=1;i<=n;i++)
{
if(a[i]>a[i-1])
{
b[i]=b[i-1]+1;
}
else
{
b[i]=1;
}
ans=max(ans,b[i]);
}
c[n+1]=0;
a[n+1]=0;
for(i=n;i>=1;i--)
{
if(a[i]<a[i+1])
{
c[i]=c[i+1]+1;
}
else
{
c[i]=1;
}
}
for(i=1;i<=n;i++)
{
if(a[i-1]<a[i+1])
ans=max(ans,b[i-1]+c[i+1]);
}
printf("%d\n",ans);
return 0;
}
E
令dp[u][0\1]表示节点u距离最近的偶数点\奇数点
考虑反向建图,将每个点可以到达的点的答案向该点更新,多源最短路
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 5;
const int inf = 0x3f3f3f3f;
int a[maxn];
int dp[maxn][3], vis[maxn][3];
vector<int> e[maxn];
int main()
{
int n; scanf("%d", &n);
for (int i = 1; i <= n; ++i) {
scanf("%d", &a[i]);
if (i - a[i] >= 1)
e[i - a[i]].push_back(i);
if (i + a[i] <= n)
e[i + a[i]].push_back(i);
}
queue<pair<int, int>> q;
memset(dp, -1, sizeof(dp));
for (int i = 1; i <= n; ++i) {
dp[i][a[i] & 1] = 0;
vis[i][a[i] & 1] = 1;
q.push(make_pair(i, a[i] & 1));
}
while (!q.empty()) {
pair<int, int> ft = q.front(); q.pop();
int u = ft.first, pt = ft.second;
for (int i = 0; i < e[u].size(); ++i) {
int v = e[u][i];
if (!vis[v][pt])
q.push((dp[v][pt] = dp[u][pt] + 1, vis[v][pt] = 1, make_pair(v, pt)));
}
}
for (int i = 1; i <= n; ++i)
printf("%d ", dp[i][1 ^ (a[i] & 1)]);
}