【CSP-S 2019模拟题解】

T1:

对每个TT预处理一下到其他TT的距离然后状压dpdp即可

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
#define int long long
cs int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
#define pli pair<ll,int>
char xxx;
int n,k,t,lx,rx;
int r,b,g;
cs int N=505,M=(1<<12)|5;
char s[N][N];
ll f[12][M],tr[15][15];
int id[N][N],tot,str,cnt[M];
int px[12],py[12],num,vis[N*N];
ll val[N*N],dis[N*N];
vector<int> e[N*N];
char yyy;
inline void add(int u,int v){
	e[u].pb(v);
}
priority_queue<pli,vector<pli>,greater<pli> > q;
inline void dijkstra(int st){
	memset(dis,127/3,sizeof(dis));
	memset(vis,0,sizeof(vis));
	dis[st]=0,q.push(pli(0,st));
	while(!q.empty()){
		int u=q.top().se;q.pop();
		if(vis[u])continue;
		vis[u]=1;
		for(int i=0;i<e[u].size();i++){
			int v=e[u][i];
			if(dis[v]>dis[u]+val[v]){
				dis[v]=dis[u]+val[v];
				q.push(pli(dis[v],v));
			}
		}
	}
}
signed main(){
	n=read(),k=read(),t=read(),lx=read(),rx=read();
	r=read(),g=read(),b=read();
	for(int i=1;i<=n;i++){
		scanf("%s",s[i]+1);
		for(int j=1;j<=n;j++){
			id[i][j]=++tot;
			if(s[i][j]=='T')px[++num]=i,py[num]=j;
			if(s[i][j]=='S')str=tot;
			if(s[i][j]=='R')val[tot]=r;
			if(s[i][j]=='G')val[tot]=g;
			if(s[i][j]=='B')val[tot]=b;
		}
	}
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++){
		if(i>1)add(id[i][j],id[i-1][j]);
		if(j>1)add(id[i][j],id[i][j-1]);
		if(i<n)add(id[i][j],id[i+1][j]);
		if(j<n)add(id[i][j],id[i][j+1]);
	}
	for(int i=1;i<=num;i++){
		dijkstra(id[px[i]][py[i]]);
		for(int j=1;j<=num;j++)
		tr[i][j]=dis[id[px[j]][py[j]]];
	}
	dijkstra(str);int sta=1<<num;
	for(int i=1;i<sta;i++)cnt[i]=cnt[i>>1]+(i&1);
	memset(f,127/3,sizeof(f));
	for(int i=1;i<=num;i++)f[i][1<<(i-1)]=dis[id[px[i]][py[i]]]-t;
	for(int s=1;s<sta;s++){
		for(int i=1;i<=num;i++)if(s&(1<<(i-1))){
			for(int j=1;j<=num;j++)if(!(s&(1<<(j-1)))){
				chemn(f[j][s|(1<<(j-1))],f[i][s]+tr[i][j]-t);
			}
		}
	}
	ll res=1e18;
	for(int i=1;i<=num;i++)
	for(int s=1;s<sta;s++)if(lx<=cnt[s]&&cnt[s]<=rx)chemn(res,f[i][s]);
	cout<<-res;
	return 0;
}

T2:

脑残写了一个10nlog3n10*nlog^3n的结果差点就跑过去了
我想的是对每个叶子做根然后倍增找gcdgcd变化的
其实用树剖+STST表可以做到10nlog2n10*nlog^2n
但可以直接在树上暴力维护所有gcdgcd变化的地方,然后删去无效的地方即可
复杂度O(10nlogn)O(10nlogn)但是并不想写了

我的考场垃圾代码

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
cs int N=160005;
char xx;
int fa[N][19],g[N][19],dep[N],val[N];
int adj[N],nxt[N<<1],to[N<<1],cnt;
inline void addedge(int u,int v){
	nxt[++cnt]=adj[u],adj[u]=cnt,to[cnt]=v;
}
inline int gcd(int a,int b){
	return b?gcd(b,a-a/b*b):a;
}
int leaf[N],num,in[N],vis[N];
int n;
char yyy;
void dfs1(int u){
	for(int i=1;i<=18;i++)fa[u][i]=0,g[u][i]=0;
	vis[u]=0;
	for(int e=adj[u];e;e=nxt[e]){
		int v=to[e];
		if(v==fa[u][0])continue;
		fa[v][0]=u,g[v][0]=val[u],dep[v]=dep[u]+1;
		dfs1(v);
	}
}
inline void gettag(int u){
	while(u&&!vis[u])vis[u]=1,u=fa[u][0];
}
void dfs2(int u){
	for(int i=1;i<=18;i++)fa[u][i]=fa[fa[u][i-1]][i-1],g[u][i]=gcd(g[u][i-1],g[fa[u][i-1]][i-1]);
	for(int e=adj[u];e;e=nxt[e]){
		int v=to[e];
		if(!vis[v]||v==fa[u][0])continue;
		dfs2(v);
	}
}
ll ans;
inline void calc(int u){
	int pre=val[u],pp=u;
	while(u){
		for(int i=18;~i;i--)
		if(fa[u][i]&&gcd(pre,g[u][i])==pre)u=fa[u][i];
		chemx(ans,1ll*pre*(dep[pp]-dep[u]+1));
		pre=gcd(pre,g[u][0]),u=fa[u][0];
	}
}
int mxdep,root;
void getrt1(int u,int fa,int dep){
	if(dep>mxdep)mxdep=dep,root=u;
	for(int e=adj[u];e;e=nxt[e]){
		int v=to[e];
		if(v==fa)continue;
		getrt1(v,u,dep+1);
	}
}
inline void solve1(){
	getrt1(1,0,1);mxdep=0;
	getrt1(root,0,1);
	cout<<1ll*val[1]*mxdep<<'\n';
}
int main(){
	n=read();bool flag=1;
	for(int i=1;i<=n;i++){val[i]=read();if(val[i]!=val[i-1])flag=0;}
	for(int i=1;i<n;i++){
		int u=read(),v=read();
		addedge(u,v),addedge(v,u);
		in[u]++,in[v]++;
	}
	if(flag){solve1();return 0;}
	for(int i=1;i<=n;i++)if(in[i]==1)leaf[++num]=i;
	for(int i=1;i<=num;i++){
//		cerr<<leaf[i]<<'\n';
		fa[leaf[i]][0]=0,dep[leaf[i]]=1,g[leaf[i]][0]=0;
		dfs1(leaf[i]);
		for(int j=i+1;j<=num;j++)gettag(leaf[j]);
//		lim=log2(lim);
		dfs2(leaf[i]);
		for(int j=1;j<=n;j++)if(vis[j])calc(j);
	}
	cout<<ans<<'\n';
	return 0;
}

T3

首先考虑s=1,t=ns=1,t=n
f[i][j][k]f[i][j][k]表示前ii,有jj个连出来,kk个连进去的方案
枚举当前这个点接入接出即可

对于s1,tns\not=1,t\not=n可以类似分类讨论
具体可以参照题解的讨论

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define ll long long
cs int RLEN=1<<20|1;
inline char gc(){
	static char ibuf[RLEN],*ib,*ob;
	(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
	return (ib==ob)?EOF:*ib++;
}
#define gc getchar
inline int read(){
	char ch=gc();
	int res=0;bool f=1;
	while(!isdigit(ch))f^=ch=='-',ch=gc();
	while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
	return f?res:-res;
}
template<class tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
template<class tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
cs int mod=1e9+7;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b;a+=(a>>31&mod);}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b,a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
int n;
char ss[2006];
cs int N=2005;
int f[N][N],str,des;
int main(){
	#ifdef Stargazer
	freopen("lx.cpp","r",stdin);
	#endif
	scanf("%s",ss+1);
	n=strlen(ss+1);
	str=read(),des=read();
	f[0][0]=1;
	for(int i=1;i<=n;i++)
	for(int j=0;j<=i;j++)
	if(f[i-1][j]){
		int tmp=f[i-1][j],cnt=j-(i>str)-(i>des);
		if(i==str){
			if(ss[i]!='L')Add(f[i][j+1],tmp);
			if(ss[i]!='R')Add(f[i][j],mul(tmp,cnt));
			continue;
		}
		if(i==des){
			Add(f[i][j+1],tmp);
			Add(f[i][j],mul(tmp,cnt));
			continue;
		}
		if(ss[i]!='L'){
			Add(f[i][j+1],tmp);
			Add(f[i][j],mul(tmp,cnt));
			if(i>str)Add(f[i][j],tmp);
		}
		if(ss[i]!='R'){
			Add(f[i][j],mul(cnt,tmp));
			if(j)Add(f[i][j-1],mul(cnt*(cnt-1),tmp));
			if(j&&i>des)Add(f[i][j-1],mul(cnt,tmp));
			if(i>des)Add(f[i][j],tmp);
			if(i>str&&j)Add(f[i][j-1],mul(cnt,tmp));
		}
	//	cerr<<i<<" : \n";
	//	for(int j=0;j<=n;j++)cerr<<j<<":"<<f[i][j]<<'\n';	
	}
	if(str==n||des==n)cout<<f[n-1][1]<<'\n';
	else cout<<f[n-1][2]<<'\n';
	return 0;
}
posted @ 2019-11-14 18:29  Stargazer_cykoi  阅读(134)  评论(0编辑  收藏  举报