标签:click block can node nsf tran blog open memset
#include<iostream> #include<cstring> #include<cstdio> using namespace std; int n,k,cnt; char s[1000010]; int main(){ freopen("trans.in","r",stdin);freopen("trans.out","w",stdout); //freopen("Cola.txt","r",stdin); while(scanf("%d%d",&n,&k)!=EOF){ scanf("%s",s);cnt=0; int rest=0; for(int i=0;i<n;i++){ if(s[i]==‘2‘&&s[i+1]==‘3‘){ if(i>0&&(i+1)%2==0&&s[i-1]==‘2‘&&s[i+1]==‘3‘){ rest=k-cnt;break; } else if((i+1)%2==1&&s[i+1]==‘3‘&&s[i+2]==‘3‘){ rest=k-cnt;break; } else{ if((i+1)%2==0)s[i]=‘3‘; else s[i+1]=‘2‘; cnt++; if(cnt==k)break; } } } rest%=2; if(rest==0||cnt==k){ printf("%s\n",s); } else{ for(int i=0;i<n;i++){ if(s[i]==‘2‘&&s[i+1]==‘3‘){ if((i+1)%2==0)s[i]=‘3‘; else s[i+1]=‘2‘; break; } } printf("%s\n",s); } } }
#include<iostream> #include<cstdio> #define maxn 210 using namespace std; int n,m,map[maxn][7],num,ans,head[maxn],edge[maxn*6+10][maxn*6+10]; struct node{ int to,pre,v; }e[maxn*7*3]; int make_id(int i,int j){return (i-1)*5+j;} void Insert(int from,int to,int v){ e[++num].to=to; e[num].v=v; e[num].pre=head[from]; head[from]=num; edge[from][to]=num; } void dfs(int x,int y,int sc,int len){ if(x>n){ ans=max(ans,sc); return; } int now=make_id(x,y); for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(e[i].v==0)continue; int xx=(to-1)/5+1,yy=to%5; if(yy==0)yy=5; e[i].v=0; if(map[xx][yy]>=0){ int p=map[xx][yy]; map[xx][yy]=0; dfs(xx,yy,sc,len+p); map[xx][yy]=p; } else { if(len+map[xx][yy]<0){ans=max(ans,sc);} else { int p=map[xx][yy]; map[xx][yy]=0; dfs(xx,yy,sc-p,len+p); map[xx][yy]=p; } } e[i].v=1; } } int main(){ freopen("snakevsblock.in","r",stdin);freopen("snakevsblock.out","w",stdout); //freopen("Cola.txt","r",stdin); scanf("%d",&n); for(int i=1;i<=n;i++) for(int j=1;j<=5;j++){ int id=make_id(i,j); if(j!=1)Insert(id,make_id(i,j-1),1);//向左 if(j!=5)Insert(id,make_id(i,j+1),1);//向右 Insert(id,make_id(i+1,j),1);//向下 scanf("%d",&map[i][j]); } scanf("%d",&m); int x,y; for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); int id1=make_id(x,y),id2=make_id(x,y+1); e[edge[id1][id2]].v=0; e[edge[id2][id1]].v=0; } dfs(1,3,0,4); printf("%d",ans); }
、
#include<iostream> #include<cstring> #include<cstdio> #define maxn 100010 using namespace std; int n,m,k,num,head[maxn],fa[maxn],dep[maxn],h[maxn],cnt; bool vis[maxn],v[maxn]; int c[16][1<<15]; struct node{ int to,pre; }e[maxn*2]; struct Node{ int l,r; }p[300010]; void Insert(int from,int to){ e[++num].to=to; e[num].pre=head[from]; head[from]=num; } int count(int x){ int res=0; while(x){ if(x&1)res++; x>>=1; } return res; } void dfs(int now,int father){ fa[now]=father;dep[now]=dep[father]+1; for(int i=head[now];i;i=e[i].pre){ int to=e[i].to; if(to==father)continue; dfs(to,now); } } int sta; bool lca(int a,int b){ if(vis[a]||vis[b])return 1; if(dep[a]<dep[b])swap(a,b); while(dep[a]>dep[b]){ a=fa[a]; if(vis[a])return 1; } while(a!=b){ a=fa[a];b=fa[b]; if(vis[a]||vis[b])return 1; } return 0; } bool ok(int s){ memset(vis,0,sizeof(vis)); int pos=0; while(s){ pos++; if(s&1)vis[h[pos]]=1; s>>=1; } for(int i=1;i<=k;i++){ int a=p[i].l,b=p[i].r; if(lca(a,b))continue; else return 0; } return 1; } bool check(int x){ for(int i=1;i<=c[x][0];i++){ int s=c[x][i]; if(ok(s)){ sta=s; return 1; } } return 0; } int main(){ freopen("ping.in","r",stdin);freopen("ping.out","w",stdout); //freopen("Cola.txt","r",stdin); scanf("%d%d",&n,&m); for(int i=0;i<=(1<<n)-1;i++){ int nu=count(i); c[nu][++c[nu][0]]=i; } int x,y; for(int i=1;i<=m;i++){ scanf("%d%d",&x,&y); Insert(x,y);Insert(y,x); if(!v[x])h[++cnt]=x,v[x]=1; if(!v[y])h[++cnt]=y,v[y]=1; } dfs(1,1); scanf("%d",&k); for(int i=1;i<=k;i++) scanf("%d%d",&p[i].l,&p[i].r); int l=1,r=cnt,ans=0; while(l<=r){ int mid=(l+r)>>1; if(check(mid))ans=mid,r=mid-1; else l=mid+1; } printf("%d\n",ans); int pos=0; while(sta){ pos++; if(sta&1)printf("%d ",h[pos]); sta>>=1; } return 0; }
标签:click block can node nsf tran blog open memset
原文地址:http://www.cnblogs.com/thmyl/p/7698612.html