2017 国庆湖南 Day1
卡特兰数
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int f[1001]; int main() { freopen("stack.in","r",stdin); freopen("stack.out","w",stdout); int n; scanf("%d",&n); f[0]=1; f[1]=1; for(int i=2;i<=n;i++) for(int j=1;j<=i;j++) f[i]=(f[i]+f[j-1]*f[i-j])%7; printf("%d",f[n]); }
没看到取模的高精
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; struct node { int len,a[1001]; node() { len=0; memset(a,0,sizeof(a)); } void operator = (int k) { a[1]=k; len=1; } void operator = (node b) { len=b.len; for(int i=1;i<=len;i++) a[i]=b.a[i]; } node operator * (int b) { node tmp; for(int i=1;i<=len;i++) tmp.a[i]=a[i]*b; for(int i=1;i<=len;i++) tmp.a[i+1]+=tmp.a[i]/10,tmp.a[i]%=10; tmp.len=tmp.a[len+1] ? len+1 : len; while(tmp.a[tmp.len]>=10) tmp.a[++tmp.len]=tmp.a[tmp.len-1]/10,tmp.a[tmp.len-1]%=10; return tmp; } node operator / (int b) { node tmp,an; for(int i=len,j=1;i;i--,j++) tmp.a[j]=a[i]; int x=0; for(int i=1;i<=len;i++) { x=x*10+tmp.a[i]; if(x>=b) { an.a[i]=x/b; x-=an.a[i]*b; } } int L=0; for(int i=1;i<=len;i++) if(an.a[i]) { for(int j=len,k=1;j>=i;j--,L++,k++) tmp.a[k]=an.a[j]; break; } tmp.len=L; return tmp; } void print() { for(int i=len;i;i--) printf("%d",a[i]); } }f[1001]; int main() { int n; scanf("%d",&n); f[0]=1; f[1]=1; for(int i=2;i<=n;i++) f[i]=f[i-1]*(4*i-2)/(i+1); f[n].print(); }
加权并查集
#include<cstdio> using namespace std; #define N 30001 int fa[N],under[N],siz[N]; int find(int i) { if(i!=fa[i]) { int f=find(fa[i]); under[i]+=under[fa[i]]; return fa[i]=f; } return i; } void unionn(int x,int y) { int X=find(x),Y=find(y); under[X]+=siz[Y]; siz[Y]+=siz[X]; fa[X]=Y; } int main() { freopen("cube.in","r",stdin); freopen("cube.out","w",stdout); int n,m; scanf("%d",&n); m=n; for(int i=1;i<=n;i++) fa[i]=i,siz[i]=1; char s[3]; int x,y; while(m--) { scanf("%s",s); if(s[0]=='M') { scanf("%d%d",&x,&y); unionn(x,y); } else { scanf("%d",&x); find(x); printf("%d\n",under[x]); } } }
区间DP
#include <cstdio> #include <algorithm> #include <iostream> #include <cstring> using namespace std; const int N = 200 + 5; int dp[N][N]; int n; char s[N]; int num[N], col[N], tot; int main() { freopen("zuma.in", "r", stdin); freopen("zuma.out", "w", stdout); scanf("%s", s + 1); n = strlen(s + 1); int now = s[1] - '0', cnt = 1; for (int i = 2; i <= n; i ++) { if (s[i] - '0' == now) { cnt ++; } else { num[++ tot] = cnt; col[tot] = now; cnt = 1; now = s[i] - '0'; } } num[++ tot] = cnt; col[tot] = now; for (int i = 1; i <= tot; i ++) dp[i][i] = 3 - num[i]; for (int len = 2; len <= tot; len ++) { for (int i = 1, j = i + len - 1; j <= tot; i ++, j ++) { dp[i][j] = 10000; if (col[i] == col[j]) { dp[i][j] = min(dp[i][j], dp[i + 1][j - 1] + (num[i] + num[j] < 3 ? 1 : 0)); for (int k = i + 1; k < j; k ++) if (col[k] == col[i]) dp[i][j] = min(dp[i][j], dp[i + 1][k - 1] + dp[k + 1][j - 1]); } for (int k = i; k < j; k ++) dp[i][j] = min(dp[i][j], dp[i][k] + dp[k + 1][j]); } } printf("%d\n", dp[1][tot]); return 0; }
75分大爆搜
#include<cstdio> #include<cstring> #define N 201 using namespace std; int n,len; int l,r,tot; char s[N]; int col[N]; int ans=1000001; void find(int x) { l=x-1; r=x+1; tot=0; if(col[x]!=-1) tot++; while(col[x]==-1 && x>1) x--; if(x && col[x]!=-1) { while(col[l]==col[x] || col[l]==-1) { if(!l) break; if(col[l]==col[x]) tot++; l--; if(!l) break; } } if(col[x]==-1 || !x) while(col[x]==-1 && x<len) x++; while(col[r]==col[x] || col[r]==-1) { if(r==len+1) break; if(col[r]==col[x]) tot++; r++; if(r==len+1) break; } } void dfs(int sum,int last) { if(sum>=ans) return; if(sum>len*3) return; if(!n) { ans=sum; return; } int m[N]; bool f; int tmp=n; for(int i=1;i<=len;i++) m[i]=col[i]; for(int i=last;i<=len;i++) if(col[i]!=-1) { f=false; find(i); if(tot>=2) f=true; n-=tot; for(int j=l+1;j<i;j++) col[j]=-1; for(int j=r-1;j>=i;j--) col[j]=-1; while(n) { find(i); if(tot>=3) { n-=tot; for(int j=l+1;j<i;j++) col[j]=-1; for(int j=r-1;j>=i;j--) col[j]=-1; } else break; } if(f) dfs(sum+1,i+1); else dfs(sum+2,i+1); n=tmp; for(int j=1;j<=len;j++) col[j]=m[j]; } } int main() { freopen("zuma.in","r",stdin); freopen("zuma.out","w",stdout); scanf("%s",s+1); len=strlen(s+1); n=len; for(int i=1;i<=len;i++) col[i]=s[i]=='1'; dfs(0,1); printf("%d",ans); }