T1
考虑遇到一个首位为奇数的233或者或者223
会发生两者之间的相互转变
那么只需要考虑剩余的操作数的奇偶性即可
#include<cstring> #include<cstdio> #define N 1000010 int n,k; char ch[N]; int main() { freopen("trans.in","r",stdin); freopen("trans.out","w",stdout); while(scanf("%d%d",&n,&k)!=EOF) { scanf("%s",1+ch); int a=1; while(k--) { while((ch[a]!='2'||ch[a+1]!='3')&&a<=n) a++; if(a>n) break; if(a%2==1) { if(ch[a+2]=='3') k%=2; ch[a+1]='2'; } else { ch[a]='3'; a--; } } printf("%s ",1+ch); } fclose(stdin); fclose(stdout); return 0; }
T2
把豆豆和砖块丢在一起dp
f[i][j][k]:到第i行,蛇的长度还剩下j,从k位置出第i行的最大得分
g[j][l][r]:蛇的长度剩下j,从当前行l到r区间内仍为死亡的最大得分
初始化:对于每个i,g[j][k][k]=f[i-1][j-a[i][k]][k]+max(-a[i][k],0) 为初始状态
方程g[j][l][r] = max(g[j-a[i][l]][l+1][r] + max(-a[i][j],0),g[j-a[i][r]][l][r-1]+max(-a[i][k],0));
最后f[i][j][k] = max{g[j][l][r](1≤l≤k ≤r ≤5)}
#include <cstdlib> #include <cstdio> #include <cmath> #define N 205 bool fw[N][6],vis[N][15][2]; int n,m,ans,fx[5]={0,0,1,-1},fy[5]={1,-1,0,0},a[N][6]; inline int max(int a,int b) {return a>b?a:b;} void Dfs(int x,int y,int life,int point) { if(x==n+1) { ans=max(ans,point); return ; } if(life+a[x][y]<0) { ans=max(ans,point); return; } fw[x][y]=1; for(int i=0;i<4;i++) { if(fx[i]==1&&vis[x][y][1]==1) continue; if(fy[i]==-1&&vis[x][y][0]==1) continue; int tx=x+fx[i],ty=y+fy[i]; if(tx<=n+1&&tx>0&&ty<=5&&ty>0&&fw[tx][ty]==0) { if(a[x][y]>0) Dfs(tx,ty,life+a[x][y],point); else Dfs(tx,ty,life+a[x][y],point-a[x][y]); } } fw[x][y]=0; } int main(int argc,char *argv[]) { freopen("snakevsblock.in","r",stdin); freopen("snakevsblock.out","w",stdout); scanf("%d",&n); for(int i=1;i<=n;++i) for(int j=1;j<=5;++j) scanf("%d",&a[i][j]); scanf("%d",&m); for(int x,y,i=1;i<=m;++i) scanf("%d%d",&x,&y),vis[x][y][1]=1,vis[x][y+1][0]=1; Dfs(1,3,4,0); printf("%d ",ans); fclose(stdin); fclose(stdout); return 0; }
#include<iostream> #include<cstdio> #include<cstring> #define N 207 #define M 10005 using namespace std; int a[N][5],f[N][M][5],g[M][5][5]; bool flag[N][4]; int n,m,maxi,ans,val; inline int read() { int x=0,f=1;char c=getchar(); while(c>'9'||c<'0'){if(c=='-')f=-1;c=getchar();} while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();} return x*f; } int main() { freopen("snakevsblock.in","r",stdin); freopen("snakevsblock.out","w",stdout); n=read();int x,y; for(int i=1;i<=n;i++) for(int j=0;j<5;j++) a[i][j]=read(); m=read(); for(int i=1;i<=m;i++) { x=read();y=read(); flag[x][y-1]=1; } memset(f,-0x7f7f7f,sizeof f); f[0][4][2]=0;maxi=n*50; for(int i=1;i<=n;i++) { memset(g,-0x7f7f7f,sizeof g); for(int j=0;j<=maxi;j++) for(int k=0;k<5;k++) if(j-a[i][k]>=0 && j-a[i][k]<=maxi) f[i][j][k]=g[j][k][k]=f[i-1][j-a[i][k]][k]+max(-a[i][k],0); for(int l=1;l<=4;l++) for(int j=0,k=j+l;k<5;j++,k++) for(int v=0;v<=maxi;v++) { if(!flag[i][j] && (val=v-a[i][j])>=0 && val<=maxi) g[v][j][k] = g[val][j + 1][k] + max(-a[i][j], 0);//g[v][j][k]=max(g[v][j][k],g[val][j+1][k]+max(-a[i][j],0)); if(!flag[i][k-1]&& (val=v-a[i][k])>=0 && val<=maxi) g[v][j][k]=max(g[v][j][k],g[val][j][k-1]+max(-a[i][k],0)); for(int to=j;to<=k;to++) f[i][v][to]=max(f[i][v][to],g[v][j][k]); } } for(int i=0;i<=n;i++) for(int j=0;j<=maxi;j++) for(int k=0;k<5;k++) ans=max(f[i][j][k],ans); printf("%d ",ans); return 0; }
T3
任意将一个站点设置为树的根,首先求出DFS序,倒序处理,对于每个站点,如果有以以其LCA的且未被切断的路径,则将这个站点标记为损坏,用树链剖分维护即可
#include <cstdlib> #include <cstdio> #include <set> #define N 100005 using namespace std; set<int>se; struct node { int x,y; }e[N],v[N*3]; bool vis[N]; int n,m,k,limit,which[N],fa[N]; int find_(int x) {return x==fa[x]?x:fa[x]=find_(fa[x]);} bool judge() { for(int i=1;i<=n;++i) fa[i]=i; for(int i=1;i<=m;++i) { int x=e[i].x,y=e[i].y; if(se.find(x)==se.end()||se.find(y)==se.end()) continue; int fx=find_(x),fy=find_(y); if(fx!=fy) fa[fy]=fx; } int sum=0; for(int i=1;i<=k;++i) { int x=v[i].x,y=v[i].y; if(x==y) continue; int fx=find_(x),fy=find_(y); if(fx!=fy) sum++; } if(sum) return true; } void Dfs(int goal,int pos) { if(se.size()==goal) { if(judge()) { printf("%d ",goal); for(set<int> :: iterator it=se.begin();it!=se.end();++it) printf("%d ",*it); exit(0); } return ; } for(int i=pos+1;i<=limit;++i) { se.insert(which[i]); Dfs(goal,i); se.erase(which[i]); } } int main(int argc,char *argv[]) { // freopen("ping.in","r",stdin); // freopen("ping.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=m;++i) { scanf("%d%d",&e[i].x,&e[i].y); int p=e[i].x>e[i].y?e[i].x:e[i].y; } scanf("%d",&k); for(int i=1;i<=k;++i) { scanf("%d%d",&v[i].x,&v[i].y); if(!vis[v[i].x]) vis[v[i].x]=true,which[++limit]=v[i].x; if(!vis[v[i].y]) vis[v[i].y]=true,which[++limit]=v[i].y; } for(int i=1;i<=limit;++i) Dfs(i,0); fclose(stdin); fclose(stdout); return 0; }
#include <bits/stdc++.h> using namespace std; int n, m, P, u, v, to[200005], nxt[200005], p[100005], deep[100005], q[500005][2]; int son[100005], fa[100005], size[100005], top[100005], dfsx[100005], cnt, ans; int lca[500005], Q[500005], lr[100005][2]; int sta[100005]; bool flag[400005]; void dfs(int x) { size[x] = 1; for (int i = p[x]; i != -1; i = nxt[i]) if (to[i] != fa[x]) { fa[to[i]] = x; deep[to[i]] = deep[x] + 1; dfs(to[i]); if (son[x] == -1 || size[to[i]] > size[son[x]]) son[x] = to[i]; size[x] += size[to[i]]; } } void dfs1(int x) { dfsx[x] = ++cnt; if (son[x] != -1) top[son[x]] = top[x], dfs1(son[x]); for (int i = p[x]; i != -1; i = nxt[i]) if (to[i] != fa[x] && to[i] != son[x]) top[to[i]] = to[i], dfs1(to[i]); } int findlca(int x, int y) { while (1) { if (top[x] == top[y]) return deep[x] > deep[y]? y : x; if (deep[top[x]] > deep[top[y]]) x = fa[top[x]]; else y = fa[top[y]]; } } bool query(int x, int l, int r, int ll, int rr) { if (l == ll && r == rr) return flag[x]; int mid = (l + r) >> 1, L = x << 1, R = L | 1; if (rr <= mid) return query(L, l, mid, ll, rr); else if (ll > mid) return query(R, mid + 1, r, ll, rr); else return query(L, l, mid, ll, mid) | query(R, mid + 1, r, mid + 1, rr); } void modify(int x, int l, int r, int to) { flag[x] = true; if (l == r) return; int mid = (l + r) >> 1, L = x << 1, R = L | 1; if (to <= mid) modify(L, l, mid, to); else modify(R, mid + 1, r, to); } bool Query(int x, int y) { while (1) { if (top[x] == top[y]) { if (deep[x] < deep[y]) return query(1, 1, cnt, dfsx[x], dfsx[y]); else return query(1, 1, cnt, dfsx[y], dfsx[x]); } if (deep[top[x]] > deep[top[y]]) if (query(1, 1, cnt, dfsx[top[x]], dfsx[x])) return true; else x = fa[top[x]]; else { if (query(1, 1, cnt, dfsx[top[y]], dfsx[y])) return true; else y = fa[top[y]]; } } } void work(int x) { for (int i = p[x]; i != -1; i = nxt[i]) if (to[i] != fa[x]) work(to[i]); for (int i = lr[x][0]; i <= lr[x][1]; i++) if (!Query(q[Q[i]][0], q[Q[i]][1])) { modify(1, 1, cnt, dfsx[x]); sta[++ans] = x; return; } } bool cmp(int x, int y) {return lca[x] < lca[y];} int main() { freopen("ping.in","r",stdin); freopen("ping.out","w",stdout); scanf("%d%d", &n, &m); for (int i = 0; i <= n; i++) p[i] = son[i] = -1, top[i] = size[i] = fa[i] = deep[i] = 0; for (int i = 1; i <= n * 4; i++) flag[i] = false; for (int i = 1; i <= m; i++) { scanf("%d%d", &u, &v); u--, v--; to[i * 2 - 1] = v; nxt[i * 2 - 1] = p[u]; p[u] = i * 2 - 1; to[i * 2] = u; nxt[i * 2] = p[v]; p[v] = i * 2; } deep[0] = 1; dfs(0); cnt = 0; dfs1(0); scanf("%d", &P); for (int i = 1; i <= P; i++) { scanf("%d%d", &u, &v); u--, v--; q[i][0] = u, q[i][1] = v; lca[i] = findlca(u, v); Q[i] = i; } sort(Q + 1, Q + P + 1, cmp); for (int i = 0; i <= n; i++) lr[i][0] = P + 1, lr[i][1] = 0; for (int i = 1; i <= P; i++) { lr[lca[Q[i]]][0] = min(lr[lca[Q[i]]][0], i); lr[lca[Q[i]]][1] = max(lr[lca[Q[i]]][1], i); } ans = 0; work(0); printf("%d ", ans); for (int i = 1; i <= ans; i++) printf("%d ", sta[i] + 1); return 0; }