比赛链接
https://codeforces.com/contest/1272
A
排序后假设 a ≤ b ≤ c a \le b \le c a≤b≤c
如果 c − a ≥ 2 c-a \ge 2 c−a≥2,那么就让 a , c a,c a,c都往里靠( b = a b=a b=a或 b = c b=c b=c的情况下 b b b也要移动),这个时候答案是 2 ∣ a − c ∣ − 4 2|a-c|-4 2∣a−c∣−4
如果 c − a ≤ 1 c-a \le 1 c−a≤1,那么答案显然是 0 0 0
T = int(input())
for i in range(T):
a, b, c = sorted( [int(x) for x in input().split()] )
ans = 2*(c-a)
if c-a>=2:
print(ans-4)
else:
print(0)
B
构造,让他转圈圈就好(特判 L R LR LR和 U D UD UD的情况)
T = int(input())
for kase in range(T):
s = input()
cnt = dict()
cnt['L'], cnt['R'], cnt['U'], cnt['D'] = 0,0,0,0
for c in s:
cnt[c] += 1
ans = ''
if min(cnt['L'],cnt['R'])==0:
if min(cnt['U'],cnt['D'])>=1:
ans = 'UD'
elif min(cnt['U'],cnt['D'])==0:
if min(cnt['L'],cnt['R'])>=1:
ans = 'LR'
else:
h = min(cnt['L'],cnt['R'])
v = min(cnt['U'],cnt['D'])
ans = 'U'*v + 'L'*h + 'D'*v + 'R'*h
print(len(ans))
print(ans)
C
以不能用的字母为边界分段计算
n, k = [int(x) for x in input().split()]
s = input()
c = set(input().split())
cnt = 0
ans = 0
for i in range(n):
if s[i] in c:
cnt += 1
ans += cnt
else:
cnt = 0
print(ans)
D
d p dp dp
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll n, a[maxn], f[maxn], g[maxn], ans;
int main()
{
ll i;
n=read();
rep(i,n)a[i]=read();
a[0]=linf;
a[n+1]=-linf;
rep(i,n)
if(a[i]>a[i-1])f[i]=f[i-1]+1;
else f[i]=1;
for(i=n;i;i--)
if(a[i+1]>a[i])g[i]=g[i+1]+1;
else g[i]=1;
rep(i,n)ans=max(ans,f[i]);
rep(i,n)if(a[i-1]<a[i+1])ans=max(ans,f[i-1]+g[i+1]);
cout<<ans;
return 0;
}
E
图论题,从那些答案为 1 1 1的点开始 B F S BFS BFS就行了
n = int(input())
a = [0] + [int(x) for x in input().split()]
vis = set()
q = []
ans = [-1 for i in range(n+5)]
to = [ [] for i in range(n+5) ]
for i in range(1,n+1):
if i-a[i]>=1:
if a[i-a[i]]%2==a[i]%2:
to[i-a[i]].append(i)
else:
q.append(i)
ans[i] = 1
vis.add(i)
if i+a[i]<=n:
if a[i+a[i]]%2==a[i]%2:
to[i+a[i]].append(i)
elif i not in vis:
q.append(i)
ans[i] = 1
vis.add(i)
head = 0
while head<len(q):
pos = q[head]
head += 1
for t in to[pos]:
if t not in vis:
ans[t] = ans[pos]+1
vis.add(t)
q.append(t)
print(*ans[1:n+1])
F
d p dp dp
写了个 s p f a spfa spfa转移的 d p dp dp,一会 M L E MLE MLE一会 T L E TLE TLE,最后改成循环队列才过
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
char s[210], t[210];
int f[210][210][810], n, m, d[210][210][810], mask=(1<<10)-1, q[210*210*810+10];
bool in[210][210][810];
int compress(int i, int j, int k)
{
return (i<<20)|(j<<10)|k;
}
tuple<int,int,int> decompress(int t)
{
return make_tuple((t>>20)&mask,(t>>10)&mask,t&mask);
}
void spfa()
{
int head=0, tail=0, L=205*205*805+10;
q[tail++]=compress(1,1,0);
in[1][1][0]=true;
f[1][1][0]=0;
while(head!=tail)
{
auto sta=q[head++]; if(head==L)head=0;
auto tmp=decompress(sta);
int i=get<0>(tmp), j=get<1>(tmp), k=get<2>(tmp);
in[i][j][k]=false;
for(auto c:{'(',')'})
{
int ii=i+(s[i]==c), jj=j+(t[j]==c), kk=k+(c=='('?1:-1);
if(kk<0 or kk>800)continue;
if(f[ii][jj][kk]>f[i][j][k]+1)
{
f[ii][jj][kk] = f[i][j][k]+1;
d[ii][jj][kk] = compress(i,j,k);
int ss=compress(ii,jj,kk);
if(!in[ii][jj][kk])
{
q[tail++]=ss; if(tail==L)tail=0;
in[ii][jj][kk]=true;
}
}
}
}
}
stack<char> stk;
int main()
{
scanf("%s%s",s+1,t+1);
n=strlen(s+1), m=strlen(t+1);
memset(f,0x3f,sizeof(f));
spfa();
int i, j, k, ans=f[n+1][m+1][0];
i=n+1, j=m+1, k=0;
while(ans--)
{
auto t = d[i][j][k], ii=(t>>20)&mask, jj=(t>>10)&mask, kk=t&mask;
if(kk<k)stk.em('(');
else stk.em(')');
i=ii, j=jj, k=kk;
}
while(!stk.empty())putchar(stk.top()), stk.pop();
return 0;
}
总结
这套题的还 A , E A,E A,E不错
但是总的来说确实没什么营养,这让我想起一句话:“刷水题不如去打游戏”。