题解:
Code:
A. Ascending Rating
#include<cstdio> const int N=10000010; int T,n,m,k,P,Q,R,MOD,i,a[N],q[N],h,t;long long A,B; int main(){ scanf("%d",&T); while(T--){ scanf("%d%d%d%d%d%d%d",&n,&m,&k,&P,&Q,&R,&MOD); for(i=1;i<=k;i++)scanf("%d",&a[i]); for(i=k+1;i<=n;i++)a[i]=(1LL*P*a[i-1]+1LL*Q*i+R)%MOD; for(h=1,t=A=B=0,i=n;i;i--){ while(h<=t&&a[q[t]]<=a[i])t--; q[++t]=i; if(i+m-1<=n){ while(q[h]>=i+m)h++; A+=i^a[q[h]]; B+=i^(t-h+1); } } printf("%lld %lld\n",A,B); } }
B. Cut The String
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int N=100010,S=26; int T,n,m,i,j,len,x,y,ca,cb,ans;char s[N]; struct AP{ int s,d,r,f; //k*d+s 0<=k<=r AP(){} AP(int _s,int _d,int _r,int _f){s=_s,d=_d,r=_r,f=_f;} }a[N],b[N]; struct DS{ int all,son[N][S],fail[N],len[N],diff[N],top[N],text[N],last,tot,f[N]; int newnode(int l){ for(int i=0;i<S;i++)son[tot][i]=0; fail[tot]=diff[tot]=top[tot]=0,len[tot]=l; return tot++; } void init(){ last=tot=all=0; newnode(0),newnode(-1); text[0]=-1,fail[0]=1; } int getfail(int x){ while(text[all-len[x]-1]!=text[all])x=fail[x]; return x; } void add(int w){ w-=‘a‘; text[++all]=w; int x=getfail(last); if(!son[x][w]){ int y=newnode(len[x]+2); int z=son[getfail(fail[x])][w]; son[x][w]=y; fail[y]=z; diff[y]=len[y]-len[z]; if(diff[y]!=diff[z]||len[z]<1)top[y]=y;else top[y]=top[z]; } last=son[x][w]; f[all]=last; } void get(int x,AP q[],int&cnt){ cnt=0; x=f[x]; while(len[x]>0){ int y=top[x]; q[++cnt]=AP(len[y],diff[x],(len[x]-len[y])/diff[x],len[x]); //printf("! %d %d %d\n",len[y],len[x],diff[x]); x=fail[y]; } } }pre,suf; ll exgcd(ll a,ll b,ll&x,ll&y){ if(!b)return x=1,y=0,a; ll d=exgcd(b,a%b,x,y),t=x; return x=y,y=t-a/b*y,d; } inline ll solve(ll a,ll b,ll c,ll xl,ll xr,ll yl,ll yr){ if(xl>xr)return 0; if(yl>yr)return 0; if(!a&&!b){ if(c)return 0; return (xr-xl+1)*(yr-yl+1); } if(!b){ swap(a,b); swap(xl,yl); swap(xr,yr); } if(!a){ if(c%b)return 0; ll y=-c/b; if(y<yl||y>yr)return 0; return xr-xl+1; } ll x,y,d=exgcd((a%abs(b)+abs(b))%abs(b),abs(b),x,y); if(c%d)return 0; x=(x%abs(b)+abs(b))%abs(b)*((((-c)%abs(b))+abs(b))%abs(b)/d)%abs(b/d); d=abs(b/d); ll kl=(xl-x)/d-3,kr=(xr-x)/d+3; while(x+kl*d<xl)kl++; while(x+kr*d>xr)kr--; ll A=(-yl*b-a*x-c)/(a*d),B=(-yr*b-a*x-c)/(a*d); if(A>B)swap(A,B); kl=max(kl,A-3); kr=min(kr,B+3); while(kl<=kr){ ll y=(-c-a*x-a*d*kl)/b; if(yl<=y&&y<=yr)break; kl++; } while(kl<=kr){ ll y=(-c-a*x-a*d*kr)/b; if(yl<=y&&y<=yr)break; kr--; } if(kl>kr)return 0; return kr-kl+1; } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d%s",&n,&m,s+1); pre.init(); suf.init(); for(i=1;i<=n;i++)pre.add(s[i]); for(i=n;i;i--)suf.add(s[i]); while(m--){ scanf("%d%d",&x,&y); len=y-x+1; pre.get(y,a,ca); suf.get(n-x+1,b,cb); ans=0; for(i=1;i<=ca;i++)for(j=1;j<=cb;j++){ if(a[i].s+b[j].s>len)continue; if(a[i].f+b[j].f<len)continue; ans+=solve(a[i].d,b[j].d,a[i].s+b[j].s-len,0,a[i].r,0,b[j].r); } printf("%d\n",ans); } } }
C. Dynamic Graph Matching
#include<cstdio> const int N=1<<10,P=1000000007; int T,n,m,all,i,x,y,S,f[N],cnt[N],ans[N];char op[9]; inline void add(int&a,int b){a=a+b<P?a+b:a+b-P;} inline void sub(int&a,int b){a=a-b>=0?a-b:a-b+P;} int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); all=1<<n; for(i=0;i<all;i++)f[i]=0,cnt[i]=__builtin_popcount(i); f[0]=1; while(m--){ scanf("%s%d%d",op,&x,&y); x--,y--; S=(1<<x)|(1<<y); if(op[0]==‘+‘){ for(i=all-1;~i;i--)if(!(i&S))add(f[i^S],f[i]); }else{ for(i=0;i<all;i++)if(!(i&S))sub(f[i^S],f[i]); } for(i=1;i<=n;i++)ans[i]=0; for(i=1;i<all;i++)add(ans[cnt[i]],f[i]); for(i=2;i<=n;i+=2)printf("%d%c",ans[i],i<n?‘ ‘:‘\n‘); } } }
D. Euler Function
#include<cstdio> int T,k; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&k); if(k==1)puts("5");else printf("%d\n",5+k); } }
E. Find The Submatrix
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; const int N=10010; const ll inf=1LL<<60; int T,n,m,X,Y,i,j,k,pool[N];ll a[N],f[4][2][N],g[4][2][N],ans; inline void up(ll&a,ll b){a<b?(a=b):0;} inline void clr(){ for(i=0;i<=Y;i++)for(j=0;j<2;j++)for(k=0;k<=X;k++)g[i][j][k]=-inf; } inline void nxt(){ for(i=0;i<=Y;i++)for(j=0;j<2;j++)for(k=0;k<=X;k++)f[i][j][k]=g[i][j][k]; } void solve1(int o,int l,int r,int dl,int dr){ int mid=(l+r)>>1,dm=dl; ll ret=-inf; for(int i=dl;i<=dr&&i<=mid;i++){ ll now=f[o][0][i]; if(mid-i<=m)now+=a[mid-i]; if(now>ret)ret=now,dm=i; } up(g[o][0][mid],ret); if(l<mid)solve1(o,l,mid-1,dl,dm); if(r>mid)solve1(o,mid+1,r,dm,dr); } void solve2(int o,int l,int r,int dl,int dr){ int mid=(l+r)>>1,dm=dl; ll ret=-inf; for(int i=dl;i<=dr&&i<=mid;i++){ ll now=f[o][1][i]; if(mid-i<=m)now+=a[mid-i]; if(now>ret)ret=now,dm=i; } up(g[o+1][0][mid],ret); if(l<mid)solve2(o,l,mid-1,dl,dm); if(r>mid)solve2(o,mid+1,r,dm,dr); } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d%d%d",&n,&m,&X,&Y); clr(); nxt(); f[0][1][0]=0; while(n--){ for(i=1;i<=m;i++)scanf("%d",&pool[i]); sort(pool+1,pool+m+1); a[0]=0; for(i=1;i<=m;i++)a[0]+=pool[i]; for(i=1;i<=m;i++)a[i]=a[i-1]-pool[i]; clr(); for(i=0;i<=Y;i++)solve1(i,0,X,0,X); for(i=0;i<Y;i++)solve2(i,0,X,0,X); for(i=0;i<=Y;i++)for(k=0;k<=X;k++){ up(g[i][1][k],f[i][0][k]); up(g[i][1][k],f[i][1][k]); } nxt(); } ans=0; for(i=0;i<=Y;i++)for(j=0;j<2;j++)for(k=0;k<=X;k++)up(ans,f[i][j][k]); printf("%lld\n",ans); } }
F. Grab The Tree
#include<cstdio> int T,n,i,x,y,sum; int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); sum=0; for(i=1;i<=n;i++)scanf("%d",&x),sum^=x; for(i=1;i<n;i++)scanf("%d%d",&x,&y); puts(sum?"Q":"D"); } }
G. Interstellar Travel
#include<cstdio> #include<algorithm> using namespace std; const int N=200010; int T,n,t,i,f[N];bool must[N]; struct P{int x,y,p;}a[N],q[N]; inline bool cmp(const P&a,const P&b){ if(a.x!=b.x)return a.x<b.x; if(a.y!=b.y)return a.y>b.y; return a.p<b.p; } int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); a[i].p=i; } sort(a+1,a+n+1,cmp); for(t=0,i=1;i<=n;i++){ if(i>1&&a[i].x==a[i-1].x)continue; while(t>1&&1LL*(q[t].y-q[t-1].y)*(a[i].x-q[t].x)<1LL*(a[i].y-q[t].y)*(q[t].x-q[t-1].x))t--; q[++t]=a[i]; } for(i=1;i<=t;i++)must[i]=0; must[1]=must[t]=1; for(i=2;i<t;i++)if(1LL*(q[i].y-q[i-1].y)*(q[i+1].x-q[i].x)!=1LL*(q[i+1].y-q[i].y)*(q[i].x-q[i-1].x))must[i]=1; for(i=t;i;i--)if(must[i])f[i]=q[i].p;else f[i]=min(f[i+1],q[i].p); for(i=1;i<t;i++)if(f[i]==q[i].p)printf("%d ",f[i]); printf("%d\n",f[t]); } }
H. Monster Hunter
#include<cstdio> #include<algorithm> #include<queue> #include<cstdlib> using namespace std; typedef long long ll; const int N=100010; int T,n,i,x,y,g[N],v[N<<1],nxt[N<<1],ed,f[N],vis[N],del[N],pos; struct P{ ll a,b;//- a then + b P(){} P(ll _a,ll _b){a=_a,b=_b;} bool operator<(const P&o)const{//true means bigger int sgn1=a<b,sgn2=o.a<o.b; if(sgn1!=sgn2)return sgn1<sgn2; if(a<b)return a>o.a; return b<o.b; } void operator+=(const P&o){ ll na=max(a,a-b+o.a),nb=b+o.b-a-o.a+na; a=na,b=nb; } }a[N]; typedef pair<int,int>PI; typedef pair<P,PI>PII; priority_queue<PII>q; inline void add(int x,int y){v[++ed]=y;nxt[ed]=g[x];g[x]=ed;} void dfs(int x,int y){ f[x]=y; for(int i=g[x];i;i=nxt[i])if(v[i]!=y)dfs(v[i],x); } int F(int x){return del[f[x]]?f[x]=F(f[x]):f[x];} int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(ed=pos=i=0;i<=n;i++)vis[i]=del[i]=g[i]=0; a[1]=P(0,0); for(i=2;i<=n;i++)scanf("%lld%lld",&a[i].a,&a[i].b); for(i=1;i<n;i++)scanf("%d%d",&x,&y),add(x,y),add(y,x); dfs(1,0); for(i=2;i<=n;i++)q.push(PII(a[i],PI(0,i))); while(!q.empty()){ PII t=q.top(); q.pop(); x=t.second.second; if(del[x])continue; if(t.second.first!=vis[x])continue; del[x]=1; y=F(x); a[y]+=a[x]; if(y>1)q.push(PII(a[y],PI(vis[y]=++pos,y))); } printf("%lld\n",a[1].a); } }
I. Random Sequence
#include<cstdio> const int N=110,M=1500,P=1000000007; int T,n,m,i,j,k,x,y,cnt,gcd[N][N],v[N],a[N],id[N][N][N],g[M][N],w[M][N],f[N][M],ans; int getgcd(int a,int b){return b?getgcd(b,a%b):a;} int po(int a,int b){int t=1;for(;b;b>>=1,a=1LL*a*a%P)if(b&1)t=1LL*t*a%P;return t;} inline void up(int&a,int b){a=a+b<P?a+b:a+b-P;} int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++)scanf("%d",&a[i]); for(i=1;i<=m;i++)scanf("%d",&v[i]); for(i=1;i<=m;i++)for(j=1;j<=m;j++)gcd[i][j]=getgcd(i,j); cnt=0; for(i=1;i<=m;i++)for(j=i;j<=m;j+=i)for(k=j;k<=m;k+=j)id[i][j][k]=++cnt; for(i=1;i<=m;i++)for(j=i;j<=m;j+=i)for(k=j;k<=m;k+=j){ x=id[i][j][k]; for(y=1;y<=m;y++){ g[x][y]=id[gcd[j][y]][gcd[k][y]][y]; w[x][y]=v[gcd[i][y]]; } } for(i=1;i<=n;i++)for(j=1;j<=cnt;j++)f[i][j]=0; for(i=1;i<=m;i++)for(j=1;j<=m;j++)for(k=1;k<=m;k++){ if(a[1]&&i!=a[1])continue; if(a[2]&&j!=a[2])continue; if(a[3]&&k!=a[3])continue; up(f[3][id[gcd[gcd[i][j]][k]][gcd[j][k]][k]],1); } for(i=3;i<n;i++)for(j=1;j<=cnt;j++)if(f[i][j])for(k=1;k<=m;k++){ if(a[i+1]&&k!=a[i+1])continue; up(f[i+1][g[j][k]],1LL*f[i][j]*w[j][k]%P); } ans=0; for(j=1;j<=cnt;j++)up(ans,f[n][j]); for(i=1;i<=n;i++)if(!a[i])ans=1LL*ans*po(m,P-2)%P; printf("%d\n",ans); } }
J. Rectangle Radar Scanner
#include<cstdio> #include<algorithm> using namespace std; const int N=100010,M=1000010,E=262150,inf=~0U>>1; int T,n,m,K,i,a[N],b[N],vma[E],vmi[E],vprod[E]; int xl[M],xr[M],yl[M],yr[M],ansma[M],ansmi[M],ansprod[M],q[M],pool[M],e[M]; int A,B,U,C,D; inline bool cmpl(int x,int y){return xl[x]>xl[y];} inline bool cmpr(int x,int y){return xr[x]<xr[y];} inline void umax(int&a,int b){a<b?(a=b):0;} inline void umin(int&a,int b){a>b?(a=b):0;} void dfs(int x,int a,int b){ if(!vma[x])return; vma[x]=0,vmi[x]=inf,vprod[x]=1; if(a==b)return; int mid=(a+b)>>1; dfs(x<<1,a,mid),dfs(x<<1|1,mid+1,b); } void add(int x,int a,int b){ umax(vma[x],D); umin(vmi[x],D); vprod[x]=1LL*vprod[x]*D%K; if(a==b)return; int mid=(a+b)>>1; if(C<=mid)add(x<<1,a,mid);else add(x<<1|1,mid+1,b); } void ask(int x,int a,int b){ if(C<=a&&b<=D){ umax(A,vma[x]); umin(B,vmi[x]); U=1LL*U*vprod[x]%K; return; } int mid=(a+b)>>1; if(C<=mid)ask(x<<1,a,mid); if(D>mid)ask(x<<1|1,mid+1,b); } void solve(int l,int r,int L,int R){ if(l>r||L>R)return; int mid=(l+r)>>1,i,j,k,cL=L-1,cR=R+1,ce=0; for(i=L;i<=R;i++){ j=q[i]; if(xr[j]<mid)pool[++cL]=j; else if(xl[j]>mid)pool[--cR]=j; else e[++ce]=j; } for(i=L;i<=R;i++)q[i]=pool[i]; sort(e+1,e+ce+1,cmpl); for(i=1,j=mid;i<=ce;i++){ k=e[i]; while(j>=xl[k]){ C=a[j],D=b[j]; add(1,1,n); j--; } C=yl[k],D=yr[k],A=ansma[k],B=ansmi[k],U=ansprod[k]; ask(1,1,n); ansma[k]=A,ansmi[k]=B,ansprod[k]=U; } dfs(1,1,n); sort(e+1,e+ce+1,cmpr); for(i=1,j=mid+1;i<=ce;i++){ k=e[i]; while(j<=xr[k]){ C=a[j],D=b[j]; add(1,1,n); j++; } C=yl[k],D=yr[k],A=ansma[k],B=ansmi[k],U=ansprod[k]; ask(1,1,n); ansma[k]=A,ansmi[k]=B,ansprod[k]=U; } dfs(1,1,n); solve(l,mid-1,L,cL); solve(mid+1,r,cR,R); } int main(){ for(i=0;i<E;i++)vmi[i]=inf,vprod[i]=1; scanf("%d",&T); while(T--){ scanf("%d",&n); for(i=1;i<=n;i++)scanf("%d%d",&a[i],&b[i]); int a0,b0,c0,d0,pp,qq,rr,P; scanf("%d%d%d%d%d%d%d%d%d%d",&m,&a0,&b0,&c0,&d0,&pp,&qq,&rr,&P,&K); for(i=1;i<=m;i++){ int na=(1LL*pp*a0+1LL*qq*b0+rr)%P; int nb=(1LL*pp*b0+1LL*qq*a0+rr)%P; int nc=(1LL*pp*c0+1LL*qq*d0+rr)%P; int nd=(1LL*pp*d0+1LL*qq*c0+rr)%P; xl[i]=min(na%n+1,nb%n+1); xr[i]=max(na%n+1,nb%n+1); yl[i]=min(nc%n+1,nd%n+1); yr[i]=max(nc%n+1,nd%n+1); a0=na,b0=nb,c0=nc,d0=nd; q[i]=i; ansma[i]=0; ansmi[i]=inf; ansprod[i]=1; } solve(1,n,1,m); long long fin=0; for(i=1;i<=m;i++)if(ansma[i])fin+=ansprod[i]^ansma[i]^ansmi[i]; printf("%lld\n",fin); } }
K. Transport Construction
#include<cstdio> #include<algorithm> using namespace std; typedef long long ll; typedef pair<ll,int>E; const int N=100010; const ll inf=1LL<<60; int T,n,i,j,k,m,c[N],q[N],st[N],en[N],qx[N],qk[N],stq[N],enq[N],tmp[N]; int hull[N],on[N],f[N]; E e[N]; ll ans; struct P{int x,y;}a[N]; inline bool cmpc(int x,int y){return c[x]<c[y];} inline bool cmpx(int x,int y){ if(a[x].x!=a[y].x)return a[x].x<a[y].x; if(a[x].y!=a[y].y)return a[x].y<a[y].y; return c[x]<c[y]; } inline bool cmpk(int x,int y){return 1LL*a[x].y*a[y].x<1LL*a[y].y*a[x].x;} inline ll dot(const P&a,const P&b){return 1LL*a.x*b.x+1LL*a.y*b.y;} inline void work(int A,int B){//update B with A‘s hull int l1=stq[A],r1=enq[A]; int l2=stq[B],r2=enq[B]; int t=0,i,j,x,cnt=0; for(i=l1;i<=r1;i++){ if(i>l1&&a[qx[i]].x==a[qx[i-1]].x)continue; //while(t>1&&slope(tmp[t],tmp[t-1])>slope(qx[i],tmp[t]))t--; while(t>1&&1LL*(a[tmp[t]].y-a[tmp[t-1]].y)*(a[qx[i]].x-a[tmp[t]].x)>1LL*(a[qx[i]].y-a[tmp[t]].y)*(a[tmp[t]].x-a[tmp[t-1]].x))t--; tmp[++t]=qx[i]; } for(i=1;i<=t;i++){ if(i==1||i==t)hull[++cnt]=i; //else if(slope(tmp[i],tmp[i-1])!=slope(tmp[i+1],tmp[i]))hull[++cnt]=i; else if(1LL*(a[tmp[i]].y-a[tmp[i-1]].y)*(a[tmp[i+1]].x-a[tmp[i]].x)!=1LL*(a[tmp[i+1]].y-a[tmp[i]].y)*(a[tmp[i]].x-a[tmp[i-1]].x))hull[++cnt]=i; } for(i=1;i<cnt;i++){ on[i]=c[tmp[hull[i]]]; for(j=hull[i];j<=hull[i+1];j++)on[i]=min(on[i],c[tmp[j]]); } for(i=1;i<=cnt;i++)hull[i]=tmp[hull[i]]; for(i=l2,j=1;i<=r2;i++){ x=qk[i]; while(j<cnt&&dot(a[x],a[hull[j]])>dot(a[x],a[hull[j+1]]))j++; ll now=dot(a[x],a[hull[j]]); if(j<cnt&&now==dot(a[x],a[hull[j+1]]))e[c[x]]=min(e[c[x]],E(now,on[j])); else e[c[x]]=min(e[c[x]],E(now,c[hull[j]])); } } void solve(int l,int r){ if(l==r){ stq[l]=stq[l]=enq[l-1]+1; int cur=enq[l-1]; for(int i=st[l];i<=en[l];i++){ qx[++cur]=q[i]; qk[cur]=q[i]; } enq[l]=cur; sort(qx+stq[l],qx+cur+1,cmpx); sort(qk+stq[l],qk+cur+1,cmpk); return; } int mid=(l+r)>>1; solve(l,mid); solve(mid+1,r); work(l,mid+1); work(mid+1,l); int l1=stq[l],r1=enq[l]; int l2=stq[mid+1],r2=enq[mid+1]; int k=l1-1; while(l1<=r1&&l2<=r2)tmp[++k]=cmpx(qx[l1],qx[l2])?qx[l1++]:qx[l2++]; while(l1<=r1)tmp[++k]=qx[l1++]; while(l2<=r2)tmp[++k]=qx[l2++]; for(int i=stq[l];i<=k;i++)qx[i]=tmp[i]; l1=stq[l],r1=enq[l]; l2=stq[mid+1],r2=enq[mid+1]; k=l1-1; while(l1<=r1&&l2<=r2)tmp[++k]=cmpk(qk[l1],qk[l2])?qk[l1++]:qk[l2++]; while(l1<=r1)tmp[++k]=qk[l1++]; while(l2<=r2)tmp[++k]=qk[l2++]; for(int i=stq[l];i<=k;i++)qk[i]=tmp[i]; enq[l]=r2; } int F(int x){return f[x]==x?x:f[x]=F(f[x]);} inline void merge(int x,int y,ll z){if(F(x)!=F(y))ans+=z,f[f[x]]=f[y];} int main(){ scanf("%d",&T); while(T--){ scanf("%d",&n); for(i=1;i<=n;i++){ scanf("%d%d",&a[i].x,&a[i].y); c[i]=q[i]=i; } ans=0; while(1){ sort(q+1,q+n+1,cmpc); for(m=0,i=1;i<=n;i=j){ for(j=i;j<=n&&c[q[i]]==c[q[j]];j++); m++; for(k=i;k<j;k++)c[q[k]]=m; } if(m==1)break; for(i=1;i<=n;i++)en[c[q[i]]]=i; for(i=n;i;i--)st[c[q[i]]]=i; for(i=1;i<=m;i++)e[i]=E(inf,0); solve(1,m); for(i=1;i<=m;i++)f[i]=i; for(i=1;i<=m;i++)merge(i,e[i].second,e[i].first); for(i=1;i<=n;i++)c[i]=F(c[i]); } printf("%lld\n",ans); } }
L. Visual Cube
#include<cstdio> const int N=200; int T,a,b,c,n,m,i,j;char f[N][N]; int main(){ scanf("%d",&T); while(T--){ scanf("%d%d%d",&a,&b,&c); n=b*2+c*2+1; m=a*2+b*2+1; for(i=1;i<=n;i++)for(j=1;j<=m;j++)f[i][j]=‘.‘; for(i=1;i<=b;i++)for(j=1;j<=a;j++){ f[i*2-1][j*2+1+b*2-i*2]=‘+‘; f[i*2-1][j*2+2+b*2-i*2]=‘-‘; f[i*2-1][j*2+3+b*2-i*2]=‘+‘; f[i*2][j*2+b*2-i*2]=‘/‘; f[i*2][j*2+2+b*2-i*2]=‘/‘; } for(i=1;i<=c;i++)for(j=1;j<=a;j++){ f[i*2+b*2-1][j*2-1]=‘+‘; f[i*2+b*2-1][j*2]=‘-‘; f[i*2+b*2-1][j*2+1]=‘+‘; f[i*2+b*2][j*2-1]=‘|‘; f[i*2+b*2][j*2+1]=‘|‘; f[i*2+b*2+1][j*2-1]=‘+‘; f[i*2+b*2+1][j*2]=‘-‘; f[i*2+b*2+1][j*2+1]=‘+‘; } for(i=1;i<=c;i++)for(j=1;j<=b;j++){ f[i*2+b*2-j*2][a*2+j*2+1]=‘|‘; f[i*2+b*2-j*2+1][a*2+j*2+1]=‘+‘; f[i*2+b*2-j*2+2][a*2+j*2]=‘/‘; } for(i=1;i<=n;i++){ for(j=1;j<=m;j++)putchar(f[i][j]); puts(""); } } }
M. Walking Plan
#include<cstdio> #define rep(i) for(int i=0;i<n;i++) const int N=55,M=105,inf=1000000000; int T,n,m,x,y,q,K,A,B,z,g[N][N],d[N][N],f[N][N],a[M][N][N],b[M][N][N]; inline void up(int&a,int b){if(a>b)a=b;} inline void mul(int a[][N],int b[][N],int c[][N]){ static int f[N][N]; rep(i)rep(j){ f[i][j]=inf; rep(k)up(f[i][j],a[i][k]+b[k][j]); } rep(i)rep(j)c[i][j]=f[i][j]; } int main(){ scanf("%d",&T); while(T--){ scanf("%d%d",&n,&m); rep(i)rep(j)g[i][j]=inf; while(m--){ scanf("%d%d%d",&x,&y,&z); x--,y--; up(g[x][y],z); } rep(i)rep(j)a[0][i][j]=b[0][i][j]=i==j?0:inf; for(int i=1;i<M;i++)mul(a[i-1],g,a[i]); for(int i=1;i<M;i++)mul(b[i-1],a[100],b[i]); rep(i)rep(j)d[i][j]=i==j?0:g[i][j]; rep(k)rep(i)rep(j)up(d[i][j],d[i][k]+d[k][j]); for(x=0;x<M;x++){ rep(i)rep(j)f[i][j]=inf; rep(i)rep(j)rep(k)up(f[i][j],b[x][i][k]+d[k][j]); rep(i)rep(j)b[x][i][j]=f[i][j]; } scanf("%d",&q); while(q--){ scanf("%d%d%d",&x,&y,&K); x--,y--; A=K%100,B=K/100; z=inf; rep(i)up(z,a[A][x][i]+b[B][i][y]); if(z>=inf)z=-1; printf("%d\n",z); } } }
原文地址:https://www.cnblogs.com/clrs97/p/9426621.html
时间: 2024-11-02 23:35:21