10_10
A
- 赛时解法:
前缀的 A 不应该修改,所以找到第一个不等于 A 的位置 x。为了字典序最小,若存在 T,一定让某个 T 移动到 x 的位置,同时再判断将某个 T 位置翻转到 x 后字典序是否是所有 T 位置翻过来里最小的。
具体的,枚举每个 的位置,找到 使得翻转 是最优的。现在我们需要快速判断两个字符串的字典序大小(两个串都属于原串或者是原串的翻转串的子串),那么就可以用字符串 hash+ 二分 找到第一个不同的位置,然后判断下一个位置的大小即可。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define i128 __int128
#define ull unsigned long long
#define rep(i,X,Y) for (int i=(X);i<=(Y);++i)
#define per(i,X,Y) for (int i=(X);i>=(Y);--i)
#define FIO(X) freopen(#X".in","r",stdin),freopen(#X".out","w",stdout);
#define mem(X,val) memset(X,val,sizeof(X));
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fi first
#define se second
#define ALL(X) X.begin(),X.end()
#define pb push_back
#define eb emplace_back
const bool MUL=false;
const int N=5e5+10;
const ull B=37;
map<char,char> mp;
map<char,int> ord;
string s,t;
int n;
ull hs[N],ht[N],pw[N];
ull gt(ull *p,int l,int r) {
return p[r]-p[l-1]*pw[r-l+1];
}
bool cmp(int x,int y,int z) {
// cout<<" "<<x<<' '<<y<<' '<<z<<'\n';
if(gt(hs,z,y)!=gt(hs,x-(y-z),x)) {
// cout<<"A\n";
int l=0,r=y-z;
while(l<=r) {
int mid=l+r>>1;
if(gt(hs,y-mid,y)==gt(hs,x-mid,x)) l=mid+1;
else r=mid-1;
}
return ord[mp[s[x-r-1]]]<ord[mp[s[y-r-1]]];
} else if(gt(hs,y+1,x)!=gt(ht,n+2-(x-(y-z)),n-z+1)) {
// cout<<"B\n";
int l=0,r=x-y-1;
while(l<=r) {
int mid=l+r>>1;
if(gt(hs,y+1,y+1+mid)==gt(ht,n+2-(x-(y-z)),n+2-(x-(y-z))+mid)) l=mid+1;
else r=mid-1;
}
return ord[t[n+2-(x-(y-z))+l]]<ord[s[y+1+l]];
} else return false;
}
void trans(int l,int r) {
rep(i,l,r) s[i]=mp[s[i]];
reverse(s.begin()+l,s.begin()+r+1);
}
int Solve() {
mp['A']='T';
mp['T']='A';
mp['G']='C';
mp['C']='G';
ord['A']=1;
ord['C']=2;
ord['G']=3;
ord['T']=4;
cin>>s; n=s.size(); t=s; s=' '+s;
pw[0]=1; rep(i,1,n) pw[i]=pw[i-1]*B;
rep(i,1,n) {
hs[i]=hs[i-1]*B+ord[s[i]];
}
reverse(ALL(t)); t=' '+t;
rep(i,1,n) {
t[i]=mp[t[i]];
ht[i]=ht[i-1]*B+ord[t[i]];
}
bool flag1=false;
rep(i,1,n) {if(s[i]=='T') flag1=true;}
if(flag1) {
rep(i,1,n) {
if(s[i]!='A') {
int p=-1;
rep(j,i,n) {
if(s[j]=='T') {
if(p==-1||cmp(j,p,i)) p=j;
}
}
trans(i,p);
break;
}
}
} else {
bool flag2=false;
rep(i,1,n) {
if(s[i]!='A'&&s[i]!='C') {
int p=-1;
rep(j,i,n) {
if(s[j]=='G') {
if(p==-1||cmp(j,p,i)) p=j;
}
}
trans(i,p);
flag2=true;
break;
}
}
if(!flag2) {
trans(n,n);
}
}
cout<<(s.substr(1));
// int lst=-1;
// rep(i,0,n-1) {
// if(s[i]=='G'||s[i]=='T') {if(lst==-1) lst=i;}
// else if(~lst) {
// trans(lst,i-1); lst=-1; break;
// }
// }
// if(~lst) trans(lst,n-1);
// cout<<s;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T=1; if(MUL) cin>>T; while (T--) { Solve(); }
return 0;
}B
- 赛时解法:
题意为找到有多少个连通子图存在绝对众数。尝试量化这个条件。若枚举了颜色 ,对每个点赋权值 ,若 则 ,否则 。相当于找有多少个连通子图使得子图内权值总和 。
考虑树形 dp。 表示连通块以 为根,有多少个连通块的权值 ,。转移则有
这个不太好搞,我们转换成转移了 的若干个儿子后 。那么 dp 转移式就是 ,起始状态是 ,在做完所有转移后 (这里是赋值操作,下标左/右移一位,因为点 对权值的贡献也要算入)以及 (表示不选),这是个经典的树形背包的转移,在转移时, 下标的范围设为当前状态的 ,根据经典树形背包的复杂度分析,转移就是 的。
分析复杂度,直接做因为要先枚举颜色,中间的 跟子树大小有关,dp 转移 ,总共 。考虑进一步优化。
设子树内 的个数为 ,我们发现 状态中, 的取值范围在 范围内, 自然就表示整棵树颜色为枚举的颜色的点的个数。我们注意到,由于有所有颜色的总个数 ,所以有了这一层优化后我们将 轮枚举颜色的总 dp 状态优化到了 ,我们希望转移能够同样优化。
注意到由于状态同时也和子树大小有关,猜测状态数不会很多,更新完一个点的状态后求一下有意义的 dp 状态的上下界在哪,然后只进行上下界内的转移,记得将上下界和我们讨论的范围取交,这样理应优化很多复杂度。最后可以通过。
- 真正的复杂度优化
发现我们只要把状态范围和 取交后,转移的均摊总复杂度也优化成 了,这一步也就是经典树形背包进行的状态的优化,放在这题上和颜色个数的状态优化做交复杂度就正确了,足以通过此题。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define i128 __int128
#define ull unsigned long long
#define rep(i,X,Y) for (int i=(X);i<=(Y);++i)
#define per(i,X,Y) for (int i=(X);i>=(Y);--i)
#define FIO(X) freopen(#X".in","r",stdin),freopen(#X".out","w",stdout);
#define mem(X,val) memset(X,val,sizeof(X));
#define pii pair<int,int>
#define pll pair<long long,long long>
#define fi first
#define se second
#define ALL(X) X.begin(),X.end()
#define pb push_back
#define eb emplace_back
const bool MUL=false;
const int N=5e3+10;
const ll P=998244353;
void Fadd(ll &x,ll y) {x+=y; if(x>=P) x-=P;}
int n,c[N],sum[N];
vector<int> g[N];
int lb[N],ub[N];
ll dp[N][N],f[N];
ll ans=0;
int dfs(int u,int fa,int col) {
dp[u][0]=1; lb[u]=0; ub[u]=0;
int s=(c[u]==col);
for(auto v:g[u]) if(v^fa) {
s+=dfs(v,u,col);
rep(i,0,ub[u]+ub[v]-lb[u]-lb[v]) f[i]=0;
rep(i,0,ub[u]-lb[u]) {
rep(j,0,ub[v]-lb[v]) {
Fadd(f[i+j],dp[u][i]*dp[v][j]%P);
}
}
rep(i,0,ub[u]+ub[v]-lb[u]-lb[v]) dp[u][i]=f[i];
ub[u]+=ub[v]; lb[u]+=lb[v];
}
if(c[u]==col) {
++lb[u]; ++ub[u];
} else {
--lb[u]; --ub[u];
}
// rep(i,0,ub[u]-lb[u]) cout<<dp[u][i]<<' '; cout<<'\n';
if(lb[u]>0) {
per(i,ub[u]-lb[u],0) dp[u][i+lb[u]]=dp[u][i];
rep(i,0,lb[u]-1) dp[u][i]=0;
lb[u]=0;
}
rep(i,ub[u]+1-lb[u],0-lb[u]) dp[u][i]=0;
ub[u]=max(ub[u],0);
++dp[u][-lb[u]];
if(lb[u]<s-sum[col]) {
rep(i,s-sum[col]-lb[u],ub[u]-lb[u]) dp[u][i-(s-sum[col]-lb[u])]=dp[u][i];
lb[u]=s-sum[col];
}
// cout<<"DFS: "<<u<<' '<<col<<" "<<lb[u]<<' '<<ub[u]<<'\n';
// rep(i,0,ub[u]-lb[u]) cout<<dp[u][i]<<' '; cout<<'\n';
rep(j,max(1,lb[u]),ub[u]) {
Fadd(ans,dp[u][j-lb[u]]);
}
return s;
}
int Solve() {
cin>>n;
rep(i,1,n) {
cin>>c[i]; sum[c[i]]++;
}
rep(i,1,n-1) {
int u,v; cin>>u>>v;
g[u].pb(v); g[v].pb(u);
}
rep(i,1,n) {
dfs(1,0,i);
}
cout<<ans;
return 0;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int T=1; if(MUL) cin>>T; while (T--) { Solve(); }
return 0;
}