闲谈
这一期的 c f cf cf感觉题目质量一般( A A A和 F F F还是不错的),区分度也不好。 C C C题直接放个数论分块也是够无脑了, E E E题也没有 E E E题该有的样子,要我排序的话, C B E D A F CBEDAF CBEDAF。
A
题解给的方法很好,我这里是另一种思路:
排序之后不妨设
a
≥
b
≥
c
a \geq b \geq c
a≥b≥c
第一步先同时减小 a a a和 b b b,把 b b b和 c c c变得一样大,然后再重复 a c , a b , a c , a b . . . . ac,ab,ac,ab.... ac,ab,ac,ab....,直到 a = b = c a=b=c a=b=c或者 b = c = 0 b=c=0 b=c=0, a = b = c a=b=c a=b=c的时候直接加起来除以二就行了。
T = int(input())
for kase in range(T):
a, b, c = [int(x) for x in input().split()]
a, b, c = sorted([a,b,c])
ans = 0
d = b-a
ans += d
c -= d
b -= d
d = min( c - a, a )
ans += 2*d
c -= 2*d
b -= d
a -= d
if a==b==c:
ans += (a+b+c)//2
print(ans)
B
注意到 n ≤ 10 n \leq 10 n≤10,所以只改变最后一位就行了
def chg(x):
if x[3] == '9':
return x[:3]+'0'
else:
return x[:3]+chr(ord(x[3])+1)
T = int(input())
for kase in range(T):
n = int(input())
lis = []
cnt = 0
for i in range(n):
lis.append(input())
for i in range(n):
if lis[i] in lis[:i] or lis[i] in lis[i+1:]:
cnt += 1
while lis[i] in lis[:i] or lis[i] in lis[i+1:]:
lis[i] = chg(lis[i])
print(cnt,*lis,sep='\n')
C
懂点数论的这题都能秒切吧
#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;
}
int main()
{
ll T=read(), n;
while(T--)
{
n=read();
ll i, last;
set<ll> s;
for(i=1;i<=n;i=last+1)
{
last=n/(n/i);
s.insert(n/i);
}
s.insert(0);
printf("%d\n",s.size());
for(auto x:s)printf("%lld ",x);
putchar(10);
}
return 0;
}
D
就是定义一个等价关系然后问你这些单词能划分成几个等价类
我一开始先给每种英文字母开一个集合,然后如果两种字母出现在同一个单词里就合并这两个集合
#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;
}
struct UnionFind
{
int f[maxn];
void init(int n)
{
for(auto i=1;i<=n;i++)f[i]=i;
}
int find(int x){return f[x]==x?x:f[x]=find(f[x]);}
void merge(int x, int y){f[find(x)]=find(y);}
}uf;
ll ans, vis[30], cnt[30];
int main()
{
ll n=read(), i, j, k;
char s[60];
uf.init(26);
rep(i,n)
{
scanf("%s",s);
auto len=strlen(s);
for(j=1;j<len;j++)uf.merge(s[0]-'a'+1,s[j]-'a'+1);
for(j=0;j<len;j++)cnt[s[j]-'a'+1]=1;
}
rep(i,26)if(uf.find(i)==i)ans+=cnt[i];
cout<<ans;
return 0;
}
E
懂点卡特兰数的这题都能秒切吧
左括号就是 + 1 +1 +1,右括号就是 − 1 -1 −1,前缀和序列中没有负数且最后一个数为 0 0 0,就说明合法。最大深度就是前缀和序列的最大值。
#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;
}
struct SegmentTree
{
#define inf 0x3f3f3f3f
int mn[maxn<<2], mx[maxn<<2], sum[maxn<<2], add[maxn<<2], set[maxn<<2], L[maxn<<2], R[maxn<<2];
void maketag_set(int o, int v)
{
add[o]=0;
set[o]=v;
mx[o]=mn[o]=v;
sum[o]=(R[o]-L[o]+1)*v;
}
void maketag_add(int o, int v)
{
add[o]+=v;
mx[o]+=v, mn[o]+=v;
sum[o]+=(R[o]-L[o]+1)*v;
}
void pushdown(int o)
{
if(L[o]==R[o])return;
if(~set[o])
{
maketag_set(o<<1,set[o]);
maketag_set(o<<1|1,set[o]);
set[o]=-1;
}
if(add[o])
{
maketag_add(o<<1,add[o]);
maketag_add(o<<1|1,add[o]);
add[o]=0;
}
}
void pushup(int o)
{
mx[o]=max(mx[o<<1],mx[o<<1|1]);
mn[o]=min(mn[o<<1],mn[o<<1|1]);
sum[o]=sum[o<<1]+sum[o<<1|1];
}
void build(int o, int l, int r)
{
int mid(l+r>>1);
L[o]=l, R[o]=r;
add[o]=0;
set[o]=-1;
if(l==r)
{
mn[o]=mx[o]=sum[o]=0;
return;
}
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
}
void segset(int o, int l, int r, int v)
{
int mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_set(o,v);return;}
pushdown(o);
if(l<=mid)segset(o<<1,l,r,v);
if(r>mid)segset(o<<1|1,l,r,v);
pushup(o);
}
void segadd(int o, int l, int r, int v)
{
int mid(L[o]+R[o]>>1);
if(l<=L[o] and r>=R[o]){maketag_add(o,v);return;}
pushdown(o);
if(l<=mid)segadd(o<<1,l,r,v);
if(r>mid)segadd(o<<1|1,l,r,v);
pushup(o);
}
int segsum(int o, int l, int r)
{
pushdown(o);
int mid(L[o]+R[o]>>1), ans(0);
if(l<=L[o] and r>=R[o])return sum[o];
if(l<=mid)ans+=segsum(o<<1,l,r);
if(r>mid)ans+=segsum(o<<1|1,l,r);
return ans;
}
int segmin(int o, int l, int r)
{
int mid(L[o]+R[o]>>1), ans(inf);
if(l<=L[o] and r>=R[o])return mn[o];
pushdown(o);
if(l<=mid)ans=min(ans,segmin(o<<1,l,r));
if(r>mid)ans=min(ans,segmin(o<<1|1,l,r));
return ans;
}
int segmax(int o, int l, int r)
{
int mid(L[o]+R[o]>>1), ans(-inf);
if(l<=L[o] and r>=R[o])return mx[o];
pushdown(o);
if(l<=mid)ans=max(ans,segmax(o<<1,l,r));
if(r>mid)ans=max(ans,segmax(o<<1|1,l,r));
return ans;
}
#undef inf
}segtree;
char s[maxn];
int now[maxn];
int main()
{
int pos=0, n, LEN=1e6, i;
segtree.build(1,0,LEN);
scanf("%d%s",&n,s+1);
rep(i,n)
{
auto c=s[i];
if(c=='L')pos=max(0,pos-1);
else if(c=='R')pos++;
else
{
segtree.segadd(1,pos,LEN,-now[pos]);
if(c=='(')now[pos]=1;
else if(c==')')now[pos]=-1;
else now[pos]=0;
segtree.segadd(1,pos,LEN,now[pos]);
}
if(segtree.segmin(1,0,LEN)<0 or segtree.segsum(1,LEN,LEN)!=0){printf("-1 ");}
else printf("%d ",segtree.segmax(1,0,LEN));
}
return 0;
}
F
这题不错
有一句话十分关键:“it is guaranteed, that for each tree exists a depth-first search from the node 1, that visits leaves in order of connection to devices.”
一开始没看到这句话,自闭了好久…
做法是这样的, w 0 , i , j w_{0,i,j} w0,i,j表示撤销上方对 [ i , j ] [i,j] [i,j]的供电且不影响其它区域的供电,最多能删几条边; w 1 , i , j w_{1,i,j} w1,i,j表示撤销下方对 [ i , j ] [i,j] [i,j]的供电且不影响其它区域的供电,最多能删几条边。
然后 d p i = m a x ( d p j − 1 + m a x ( w 0 j i , w 1 j i ) ) dp_i = max(dp_{j-1} +max(w_{0ji},w_{1ji})) dpi=max(dpj−1+max(w0ji,w1ji))
做法很简单,但是这其中的科学道理可不像做法这么简单:
因为 d f s dfs dfs序和发电机序列对应,所以会出现以下现象:
如果 d p j − 1 dp_{j-1} dpj−1这个答案所对应的方案是“最后一段撤销上方供电”,那么这个时候 ( d p j − 1 + w 0 j i ) (dp_{j-1}+w_{0ji}) (dpj−1+w0ji)所对应的方案可能是“没有最大化的拆去电线”,但是不用担心,后面有更有的方案会把这个给更新掉。
如果 d p j − 1 dp_{j-1} dpj−1这个答案所对应的方案是"最后一段撤销下方供电",假设 d p j − 1 dp_{j-1} dpj−1所对应方案中最后一个“取消上方供电”的发电机的序号是 k k k,那么 k k k和 j j j有没有将某些电线重复减去了或者说该减去的没减去呢?——答案是没有。首先根据 w 0 i j w_{0ij} w0ij的定义我们没有少减。再者,根据树的性质, l c a ( x [ k ] , x [ k + 1 ] ) , l c a ( x [ k ] , x [ k + 2 ] ) , … , l c a ( x [ k ] , x [ j ] ) lca(x[k],x[k+1]),lca(x[k],x[k+2]),\dots,lca(x[k],x[j]) lca(x[k],x[k+1]),lca(x[k],x[k+2]),…,lca(x[k],x[j])的深度是递减的,我在删掉 k k k所对应的那些边的时候,因为保证了深度较深的 l c a ( x [ k ] , x [ k + 1 ] ) lca(x[k],x[k+1]) lca(x[k],x[k+1]) 与根节点联通,固然其祖先 l c a ( x [ k ] , x [ j ] ) lca(x[k],x[j]) lca(x[k],x[j])也是与祖先联通的了。
#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 2019
#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 w[2][maxn][maxn], lea[2][maxn], a, b, n, dp[maxn], outdeg[maxn], p[2][maxn];
vector<ll> G[2][maxn];
ll bamboo(ll id, ll x)
{
ll ans=0;
while(x>1 and outdeg[x]==0)ans++, outdeg[p[id][x]]--, x=p[id][x];
return ans;
}
void prework(ll id, ll l)
{
ll r, i;
rep(i,2000)outdeg[i]=0;
rep(i,2000)outdeg[p[id][i]]++;
for(r=l;r<=n;r++)
w[id][l][r] = w[id][l][r-1] + bamboo(id,lea[id][r]);
}
int main()
{
ll i, j;
n=read();
a=read();
rep(i,a-1)p[0][i+1]=read();
rep(i,n)lea[0][i]=read();
b=read();
rep(i,b-1)p[1][i+1]=read();
rep(i,n)lea[1][i]=read();
rep(i,n)prework(0,i),prework(1,i);
rep(i,n)rep(j,i)dp[i]=max(dp[i],dp[j-1]+max(w[0][j][i],w[1][j][i]));
printf("%lld",dp[n]);
return 0;
}