T1.回文树裸题。
#include<cstdio> #include<iostream> #define ll long long using namespace std; inline int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';} return x*f; } int cnt=0; int f[300005],l[300005],p[300005]; int c[300005][26]; char s[300005]; int main() { freopen("palindrome.in","r",stdin); freopen("palindrome.out","w",stdout); scanf("%s",s+1); f[0]=1;l[++cnt]=-1; for(int i=1,j=1;s[i];++i) { while(s[i]!=s[i-l[j]-1]) j=f[j]; if(!c[j][s[i]-'a']) { l[++cnt]=l[j]+2; int k=f[j]; while(s[i]!=s[i-l[k]-1]) k=f[k]; f[cnt]=c[k][s[i]-'a']; c[j][s[i]-'a']=cnt; } j=c[j][s[i]-'a']; ++p[j]; } ll ans=0; for(int i=cnt;i>1;i--) { ans=max(ans,1ll*p[i]*l[i]); p[f[i]]+=p[i]; } cout<<ans; return 0; }
T2.斜率优化
f[i]=max(f[j]+s[j]*(s[i]-s[j]))
令g[i]=f[i]-s[i]^2
j比k优 那么g[j]+s[i]s[j]>g[k]+s[i][k]
g[j]-g[k]>s[i](s[k]-s[j])
g[j]-g[k]/(s[j]-s[k])<-s[i]
所以维护队列上凸即可
#include<iostream> #include<cstdio> #include<queue> #define MAXN 100000 #include<cstring> #define ll long long using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } int n,k; int from[202][MAXN+5]; ll f[MAXN+5]; ll s[MAXN+5]; ll g[2][MAXN+5]; int q[MAXN+5]; int top=0,tail=1; ll get(int pre,int x,int t) { while(top-tail>=1&&(g[pre][q[tail]]-g[pre][q[tail+1]])<s[x]*(s[q[tail+1]]-s[q[tail]])) ++tail; from[t][x]=q[tail]; return g[pre][q[tail]]+s[q[tail]]*s[x]; }; void ins(int t,int k) { while(top-tail>=1) { int i=q[top-1],j=q[top]; if((g[t][k]-g[t][j])*(s[i]-s[j])<(g[t][j]-g[t][i])*(s[j]-s[k])) top--; else break; } q[++top]=k; } int main() { //freopen("sequence.in","r",stdin); //freopen("sequence.out","w",stdout); n=read();k=read(); for(int i=1;i<=n;i++) { s[i]=read();s[i]+=s[i-1]; } int pre=0,nown=1; for(int i=1;i<=n;i++) g[pre][i]=-(s[i]*s[i]); for(int l=2;l<=k+1;l++) { memset(f,0,sizeof(f));top=0;tail=1; memset(g[nown],0,sizeof(g[nown])); ins(pre,l-1); for(int i=l;i<=n;i++) { f[i]=get(pre,i,l); g[nown][i]=f[i]-(s[i]*s[i]); if(s[i]!=s[i-1])ins(pre,i); //printf("%d %d %d %I64d ",l,i,from[l][i],f[i]); } nown=1-nown; pre=1-pre; } printf("%lld ",f[n]); return 0; }
T3.
树形dp,把根节点作为第一个存在的点,那么每一段蓝线总是 爷爷-爸爸-点 的形式。
所以用f1表示一个点作为中间的点的最大值,f2表示一个点不是中间点的最大答案。
树形dp之后O(1)换根
#include<iostream> #include<cstdio> #include<cstring> #define ll long long #define MAXN 200000 #define INF 2000000000 using namespace std; int read() { int x=0,f=1;char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1;ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0'; ch=getchar();} return x*f; } ll ans=0; int n,cnt=0; ll f[MAXN+5],f2[MAXN+5]; int mx[MAXN+5],mx2[MAXN+5],from[MAXN+5],from2[MAXN+5]; int fa[MAXN+5],head[MAXN+5],p[MAXN+5]; struct edge{ int to,next,w; }e[MAXN*2+5]; void ins(int f,int t,int w) { e[++cnt].next=head[f]; head[f]=cnt; e[cnt].to=t; e[cnt].w=w; } void renew(int x,int t,int f) { //cout<<"ins"<<x<<" "<<t<<" "<<f<<endl; if(t>mx[x]) { from2[x]=from[x]; mx2[x]=mx[x]; mx[x]=t; from[x]=f; } else if(t>mx2[x]) { mx2[x]=t; from2[x]=f; } } void dfs(int x,int pre,int father) { fa[x]=father; for(int i=head[x];i;i=e[i].next) { int v=e[i].to; if(v!=fa[x]) { dfs(v,e[i].w,x);f2[x]+=max(f[v],f2[v]); renew(x,e[i].w+f2[v]-max(f[v],f2[v]),v); } } f[x]=f2[x]+mx[x]+pre; if(f[x]<0) f[x]=0; //cout<<x<<" "<<f[x]<<" "<<f2[x]<<endl; } void solve(int x) { if(fa[x]) { int v=fa[x];f2[x]+=max(f[v],f2[v]); renew(x,p[x]+f2[v]-max(f[v],f2[v]),v); } ans=max(ans,f2[x]);f[x]=f2[x]+mx[x]; //cout<<"solve"<<x<<" "<<f2[x]<<" "<<f[x]<<endl; for(int i=head[x];i;i=e[i].next) { int v=e[i].to; if(v!=fa[x]) { ll t2=max(f[v],f2[v]); f[x]+=e[i].w-t2; if(from[x]==v) f[x]=f[x]-mx[x]+mx2[x]; f2[x]-=t2;p[v]=e[i].w; solve(v); f2[x]+=t2; f[x]=f2[x]+mx[x]; } } } int main() { freopen("beads.in","r",stdin); freopen("beads.out","w",stdout); n=read(); for(int i=1;i<n;i++) { int u=read(),v=read(),w=read(); ins(u,v,w);ins(v,u,w); } for(int i=1;i<=n;i++) mx[i]=mx2[i]=-INF; dfs(1,0,0); solve(1); cout<<ans; return 0; }