没有正解,都是我的暴力
T1神奇的幻方
模拟
#include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long #define N 1609 using namespace std; int n; int h[N],l[N]; int a[42][42]; int main() { scanf("%d",&n); h[1]=1;l[1]=(n+1)/2;a[h[1]][l[1]]=1; int p;p=n*n; for(int i=2;i<=p;i++) { int t=i-1; if(h[t]==1&&l[t]!=n) { a[n][l[t]+1]=i; h[i]=n;l[i]=l[t]+1; continue; } if(l[t]==n&&h[t]!=1) { a[h[t]-1][1]=i; h[i]=h[t]-1; l[i]=1; continue; } if(h[t]==1&&l[t]==n) { a[h[t]+1][l[t]]=i; h[i]=h[t]+1; l[i]=l[t]; continue; } if(h[t]!=1&&l[t]!=n) { if(a[h[t]-1][l[t]+1]==0) { a[h[t]-1][l[t]+1]=i; h[i]=h[t]-1; l[i]=l[t]+1; }else { a[h[t]+1][l[t]]=i; h[i]=h[t]+1; l[i]=l[t]; } } } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) printf("%d ",a[i][j]); printf(" "); } return 0; }
T2信息传递
模拟+卡时
#include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long #define N 200005 using namespace std; int n,ans,js; int a[N]; int main() { scanf("%d",&n);ans=n; for(int i=1;i<=n;i++) scanf("%d",&a[i]); for(int i=1;i<=n;i++) { int now;now=i; for(int j=1;j<=n;j++) { now=a[now];js++; if(now==i) { ans=min(ans,j); break; } } if(js>=4384380)break; } printf("%d ",ans); return 0; }
Tarjian求最小环
#include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long #define N 200005 using namespace std; int n,top; int sumedge,tim,sumclr; int head[N],cnt[N]; int Stack[N],instack[N],low[N],dfn[N]; struct Edge { int x,y,nxt; Edge(int x=0,int y=0,int nxt=0): x(x),y(y),nxt(nxt){} }edge[N]; void add(int x,int y) { edge[++sumedge]=Edge(x,y,head[x]); head[x]=sumedge; } void Tarjian(int x) { low[x]=dfn[x]=++tim; Stack[++top]=x;instack[x]=true; for(int i=head[x];i;i=edge[i].nxt) { int v=edge[i].y; if(instack[v]) low[x]=min(low[x],dfn[v]); else if(!dfn[v]) { Tarjian(v); low[x]=min(low[x],low[v]); } } if(low[x]==dfn[x]) { sumclr++; while(Stack[top+1]!=x) { cnt[sumclr]++; instack[Stack[top+1]]=false; top--; } } } int main() { scanf("%d",&n); for(int i=1;i<=n;i++) { int x; scanf("%d",&x); add(i,x); } for(int i=1;i<=n;i++) if(!dfn[i]) Tarjian(i); int ans=n+1; for(int i=1;i<=sumclr;i++) if(cnt[i]!=1) ans=min(ans,cnt[i]); cout<<ans<<endl; return 0; }
T3斗地主...
T1跳石头
二分答案
#include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long using namespace std; int Len,N,M; int ans; int L,R; int a[50005]; bool check(int x) { int mv,now;mv=now=0; for(int i=1;i<=N+1;i++) { if(a[i]-now<x)mv++; else now=a[i]; } return mv<=M; } int main() { scanf("%d%d%d",&Len,&N,&M); /* 起点到终点的距离 起点到终点的岩石数 至多移走的岩石数 */ for(int i=1;i<=N;i++)scanf("%d",&a[i]); a[N+1]=Len; L=1;R=Len; while(L<=R) { int mid=(L+R)>>1; if(check(mid))ans=mid,L=mid+1; else R=mid-1; } printf("%d ",ans); return 0; }
T3运输计划
// luogu-judger-enable-o2 #include<iostream> #include<cmath> #include<ctime> #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #define LL long long #define N 300005 using namespace std; int n,m; int sumedge; int head[N],s[N],t[N]; LL sum[N]; LL res,ans; struct E { int x,y,z; }e[N]; struct Edge { int x,y,z,nxt; Edge(int x=0,int y=0,int z=0,int nxt=0): x(x),y(y),z(z),nxt(nxt){} }edge[N<<1]; void add(int x,int y,int z) { edge[++sumedge]=Edge(x,y,z,head[x]); head[x]=sumedge; } 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';ch=getchar();} return x*f; } void dfs(int now,int pre,int ed,int ss,int tt,LL p) { if(now==ed) { ans=max(ans,p); return ; } for(int i=head[now];i;i=edge[i].nxt) { int v=edge[i].y; if(v==pre) continue; if((v==ss&&now==tt)||(v==tt&&now==ss)) { dfs(v,now,ed,ss,tt,p); }else dfs(v,now,ed,ss,tt,p+edge[i].z); } } int main() { n=read();m=read(); for(int i=1;i<n;i++) { int x,y,z; x=read();y=read();z=read(); e[i].x=x;e[i].y=y;e[i].z=z; sum[y]=sum[x]+z; add(x,y,z); add(y,x,z); } for(int i=1;i<=m;i++) { s[i]=read();t[i]=read(); } if(m==1) { res=1000000000; for(int i=1;i<n;i++) { ans=0; for(int j=1;j<=m;j++) { dfs(s[j],0,t[j],e[i].x,e[i].y,0); } res=min(res,ans); } printf("%lld ",res); return 0; } res=1000000000; for(int i=1;i<n;i++) { int xx,yy;xx=e[i].x;yy=e[i].y; if(xx>yy) swap(xx,yy);ans=0; for(int j=1;j<=m;j++) { int xd,yd;xd=s[j];yd=t[j]; if(xd>yd) swap(xd,yd); if(xx>=xd&&yy<=yd)ans=max(ans,sum[yd]-sum[xd]-e[i].z); else ans=max(ans,sum[yd]-sum[xd]); } res=min(res,ans); } printf("%lld ",res); return 0; } /* 5 1 1 2 1 2 4 3 2 5 2 1 3 2 3 4 */