思路:
1003是大水题
#include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<string> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pb push_back #define mp make_pair #define Maxn 100010 #define Maxm 200010 #define LL __int64 #define Abs(x) ((x)>0?(x):(-x)) #define lson(x) (x<<1) #define rson(x) (x<<1|1) #define inf 100000 #define lowbit(x) (x&(-x)) #define clr(x,y) memset(x,y,sizeof(x)) #define Mod 1000000007 using namespace std; char ss[20]="nanodesu"; int main() { int t,i,j,l; int Case=0; char str[110]; scanf("%d",&t); while(t--){ scanf("%s",&str); l=strlen(str); printf("Case #%d: ",++Case); if(l>=4&&str[l-1]=='u'&&str[l-2]=='s'&&str[l-3]=='e'&&str[l-4]=='d'){ str[l-4]='\0'; printf("%s",str); printf("%s\n",ss); continue; } printf("%s",str); printf("%s\n",ss); } return 0; }
1004贪心
#include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<string> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pb push_back #define mp make_pair #define Maxn 100010 #define Maxm 200010 #define LL __int64 #define Abs(x) ((x)>0?(x):(-x)) #define lson(x) (x<<1) #define rson(x) (x<<1|1) #define inf 100000 #define lowbit(x) (x&(-x)) #define clr(x,y) memset(x,y,sizeof(x)) #define Mod 1000000007 using namespace std; int main() { int i,n,m,t,Case=1; scanf("%d",&t); while(t--) { scanf("%d%d",&m,&n); printf("Case #%d: ",Case++); if(m==1) { for(i=0;i<n;i++) printf("a"); printf("\n"); } else if(m>=3) { int l=n/3; for(i=0;i<l;i++) printf("abc"); if(n%3==1) printf("a"); else if(n%3==2) printf("ab"); printf("\n"); } else if(n==1) printf("a\n"); else if(n==2) printf("ab\n"); else if(n==3) printf("aab\n"); else if(n==4) printf("aabb\n"); else if(n==5) printf("aaaba\n"); else if(n==6) printf("aaabab\n"); else if(n==7) printf("aaababb\n"); else if(n==8) printf("aaababbb\n"); else { int r=(n-2)%6; int w=(n-2)/6; printf("aa"); for(i=0;i<w;i++) printf("aababb"); if(r<=4) { for(i=0;i<r;i++) printf("a"); } if(r==5) printf("aaaab"); printf("\n"); } } return 0; }
1007 动态规划
#include<iostream> #include<cstdio> #include<cstring> using namespace std; char x[11],y[11]; int map[5000][11]; int c1,c2; int w[]={1,2,4,8,16,32,64,128,256,512}; int jisuan(int a,int b) { if(b<0) return 0; if(a==0) return 1; if(map[b][a]>=0) return map[b][a]; if(a==1) {map[b][a]=(10<=b?10:b+1);return map[b][a];} int j,num=0; for(j=0;j<=9;j++) num+=jisuan(a-1,b-j*w[a-1]); map[b][a]=num; return num; } int main() { int i,j,n; scanf("%d",&n); memset(map,-1,sizeof(map)); for(j=1;j<=n;j++) { scanf("%s%s",&x,&y); c1=strlen(x);c2=strlen(y); int zs=0; for(i=0;i<c1;i++) zs+=(x[i]-'0')*w[c1-i-1]; int t=0,num=0; while(t<c2) { if(t==c2-1) { if(zs>=0) num+=(zs-(y[t]-'0')>=0 ? y[t]-'0'+1 : zs+1); break; } for(i=0;i<=y[t]-'0';i++) { if(i==(y[t]-'0')) { zs-=i*w[c2-t-1]; break; } num+=jisuan(c2-t-1,zs-i*w[c2-t-1]); } t++; } printf("Case #%d: %d\n",j,num); } return 0; }
1008 DLX 加个剪枝就行
#include<set> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<string> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pb push_back #define mp make_pair #define Maxn 60 #define Maxm 80002 #define LL __int64 #define Abs(x) ((x)>0?(x):(-x)) #define lson(x) (x<<1) #define rson(x) (x<<1|1) #define inf 1000000 #define lowbit(x) (x&(-x)) #define clr(x,y) memset(x,y,sizeof(x)) #define Mod 1000000007 using namespace std; int D[Maxn*Maxn],U[Maxn*Maxn],L[Maxn*Maxn],R[Maxn*Maxn],S[Maxn*Maxn],C[Maxn*Maxn],H[Maxn],X[Maxn*Maxn],Q[Maxn*Maxn],id,d,n,K,cnt,f,ans; int dis[60][60],ty[60]; bool vi[Maxn]; void init(int m) { int i; for(i=0;i<=m;i++){ D[i]=U[i]=i; L[i+1]=i; R[i]=i+1; S[i]=0; } R[m]=0; id=m+1; } void ins(int r, int c) { U[id] = c; D[id] = D[c]; U[D[c]] = id; D[c] = id; if (H[r] < 0) H[r] = L[id] = R[id] = id; else { L[id] = H[r]; R[id] = R[H[r]]; L[R[H[r]]] = id; R[H[r]] = id; } S[c]++; X[id]=r; C[id++] = c; } void Remove(int c) { int i; for(i=D[c];i!=c;i=D[i]){ L[R[i]]=L[i]; R[L[i]]=R[i]; } } void Resume(int c) { int i; for(i=D[c];i!=c;i=D[i]){ L[R[i]]=i; R[L[i]]=i; } } int A() { int i,j,k,ret=0; memset(vi,false,sizeof(vi)); for(i=R[0];i;i=R[i]) if(!vi[i]){ ret++; for(j=D[i];j!=i;j=D[j]){ for(k=R[j];k!=j;k=R[k]) vi[C[k]]=true; } } return ret; } void dfs(int step) { if(step+A()>K) return ; int nn=0; int i,j,temp,c; for(i=0;i<step;i++){ nn+=ty[X[Q[i]]]; } if(step-nn>=ans){ return ; } if(R[0]==0) { f=1; nn=0; for(i=0;i<step;i++){ nn+=ty[X[Q[i]]]; } ans=min(ans,step-nn); return ; } temp=inf; for(i=R[0];i;i=R[i]) if(S[i]<temp){ temp=S[i]; c=i; } for(i=D[c];i!=c;i=D[i]){ Q[step]=i; Remove(i); for(j=R[i];j!=i;j=R[j]){ Remove(j); } dfs(step+1); for(j=R[i];j!=i;j=R[j]) Resume(j); Resume(i); } return ; } void floyd() { int i,j,k; for(k=1;k<=n;k++){ for(i=1;i<=n;i++){ for(j=1;j<=n;j++){ dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } } } } void build() { int i,j,k; init(n); for(i=1;i<=n;i++){ H[i]=-1; for(j=1;j<=n;j++){ if(dis[i][j]<=d){ ins(i,j); } } } } int main() { int t,i,j,u,v,val,Case=0; scanf("%d",&t); while(t--){ clr(dis,63); K=0; scanf("%d%d",&n,&d); for(i=1;i<=n;i++){ scanf("%d",ty+i); K+=ty[i]; dis[i][i]=0; } for(i=1;i<n;i++){ scanf("%d%d%d",&u,&v,&val); dis[u][v]=dis[v][u]=val; } floyd(); build(); f=0; ans=inf; dfs(0); printf("Case #%d: ",++Case); if(!f){ printf("-1\n"); continue; } printf("%d\n",ans); } return 0; }
1010 单调队列
#include<set> #include<map> #include<cmath> #include<queue> #include<cstdio> #include<vector> #include<string> #include<cstdlib> #include<cstring> #include<iostream> #include<algorithm> #define pb push_back #define mp make_pair #define Maxn 200010 #define Maxm 200010 #define LL __int64 #define Abs(x) ((x)>0?(x):(-x)) #define lson(x) (x<<1) #define rson(x) (x<<1|1) #define inf 100000 #define lowbit(x) (x&(-x)) #define clr(x,y) memset(x,y,sizeof(x)) #define Mod 1000000007 using namespace std; int cnt[40],num[Maxn]; int que[Maxn]; int getnum(int val,int c) { int temp=val,cc=0; int sum=0; while(val){ if(val%2) cnt[cc]+=c; val>>=1; cc++; } for(int i=0;i<=30;i++) if(cnt[i]){ sum+=(1<<i); } return sum; } int main() { int t,n,m,i,j,Case=0; scanf("%d",&t); while(t--){ scanf("%d%d",&n,&m); for(i=1;i<=n;i++){ scanf("%d",num+i); } clr(cnt,0); int head=0,rear=0; int temp; LL ans=0; for(i=1;i<=n;i++){ temp=getnum(num[i],1); que[rear++]=i; while(rear>=head&&temp>=m){ temp=getnum(num[que[head]],-1); head++; } ans+=(LL)rear-head; } printf("Case #%d: %I64d\n",++Case,ans); } return 0; }