template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
template <typename T> inline T Max(T a, T b){return a > b ? a : b;}
int n, lmt, a[N], mx[20][N], sec[20][N], vis[20][N], ans[N];
intmain(){ read(n); lmt = 1 << n; for (int i = 1; i <= lmt; ++ i) read(a[i]); for (int i = 1; i <= lmt; ++ i) mx[0][i] = a[i], sec[0][i] = -INF; for (int i = 1; i <= n; ++ i) for (int j = 1; j + (1 << i) - 1 <= lmt; j += (1 << i)) { if (mx[i - 1][j] < mx[i - 1][j + (1 << i - 1)]) { mx[i][j] = mx[i - 1][j + (1 << i - 1)]; sec[i][j] = Max(sec[i - 1][j + (1 << i - 1)], mx[i - 1][j]); } else { mx[i][j] = mx[i - 1][j]; sec[i][j] = Max(sec[i - 1][j], mx[i - 1][j + (1 << i - 1)]); } vis[i][sec[i][j]] |= 1; } for (int i = 1; i <= n; ++ i) for (int j = 1; j <= lmt; ++ j) vis[i][j] |= vis[i][j - 1]; for (int i = 1; i <= lmt; ++ i) for (int j = 1; j <= n; ++ j) if (vis[j][a[i] - 1]) ans[a[i]] = j; for (int i = 1; i <= lmt; ++ i) printf("%d ", ans[i]); return0; }
constint N = 500010; const ll MOD = 1e9 + 7; constint INF = 0x3fffffff;
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
structEdge {int u, v, nxt;} e[N << 2];
int n, head[N], ecnt(1), K;
inlinevoidadd_edge(int u, int v){ e[ecnt].u = u, e[ecnt].v = v; e[ecnt].nxt = head[u], head[u] = ecnt ++; }
int son[N], mxd[N], *dp[N], buf[N << 3];
voiddfs(int x, int fa){ for (int i = head[x]; i; i = e[i].nxt) { if (e[i].v == fa) continue; dfs(e[i].v, x); if (mxd[e[i].v] > mxd[son[x]]) son[x] = e[i].v; } mxd[x] = mxd[son[x]] + 1; }
int *p = buf; ll cnt = 0;
voidDP(int x, int fa){ dp[x] = p++; dp[x][0] = 1; if (son[x]) DP(son[x], x); if (mxd[x] > K) (cnt += dp[x][K]) %= MOD; for (int i = head[x]; i; i = e[i].nxt) { if (e[i].v == fa || e[i].v == son[x]) continue; DP(e[i].v, x); for (int j = max(0, K - mxd[x]); j < mxd[e[i].v] && j < K; ++j) (cnt += 1ll * dp[e[i].v][j] * dp[x][K - j - 1]) %= MOD; for (int j = 1; j <= mxd[e[i].v]; ++j) dp[x][j] = (dp[x][j] + dp[e[i].v][j - 1]) % MOD; } } intmain(){ read(n), read(K); for (int i = 1; i < n; ++ i) { int u, v; read(u), read(v); add_edge(u, v), add_edge(v, u); } dfs(1, 0), DP(1, 0); printf("%lld", cnt * (1ll * (K + 1) * K / 2 % MOD) % MOD); return0; }
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
template <typename T> inline T Max(T a, T b){return a > b ? a : b;}
structEdge {int u, v, nxt;} e[N], te[N];
int n, head[N], ecnt(2), vis[N], endpos, pre[N], spc[N], nxt[N]; int ring[N], rcnt, ve[N], f[N], g[N], ans, pos[N];
inlinevoidadd_edge(int u, int v){ e[ecnt].u = u, e[ecnt].v = v; e[ecnt].nxt = head[u], head[u] = ecnt ++; }
boolget_ring(int x, int fa){ vis[x] = 1; for (int i = head[x]; i; i = e[i].nxt) { if (e[i].v == fa) continue; pre[e[i].v] = i; if (vis[e[i].v]) {endpos = e[i].v; returntrue;} if (get_ring(e[i].v, x)) returntrue; } returnfalse; }
voidget_ring_list(){ int now = endpos; do { ring[++ rcnt] = now; ve[pre[now]] = ve[pre[now] ^ 1] = 1; nxt[e[pre[now]].u] = pre[now] / 2; now = e[pre[now]].u; } while (now != endpos); for (int i = 1; i <= rcnt; ++ i) ring[i + rcnt] = ring[i]; }
voiddp_on_tree(int x, int fa){ g[x] = 0, f[x] = 0; for (int i = head[x]; i; i = e[i].nxt) { if (e[i].v == fa || ve[i] || ve[i ^ 1]) continue; dp_on_tree(e[i].v, x); f[x] = Max(f[x], f[e[i].v]); f[x] = Max(f[x], g[x] + g[e[i].v] + 1); g[x] = Max(g[x], g[e[i].v] + 1); } }
intmain(){ read(n); for (int i = 1; i <= n; ++ i) { read(te[i].u), read(te[i].v); add_edge(te[i].u, te[i].v); add_edge(te[i].v, te[i].u); } get_ring(1, 0); get_ring_list(); reverse(ring + 1, ring + rcnt * 2 + 1); for (int i = 1; i <= rcnt; ++ i) dp_on_tree(ring[i], 0), ans = Max(ans, f[ring[i]]); for (int i = 1; i <= rcnt; ++ i) spc[nxt[ring[i]]] = ans; for (int i = 1; i <= rcnt; ++ i) { while (frt1 <= tal1 && g[ring[q1[tal1]]] - q1[tal1] < g[ring[i]] - i) -- tal1; while (frt2 <= tal2 && g[ring[q2[tal2]]] + q2[tal2] < g[ring[i]] + i) -- tal2; q1[++ tal1] = i, q2[++ tal2] = i; } for (int i = rcnt + 1; i <= rcnt << 1; ++ i) { while (frt1 <= tal1 && q1[frt1] <= i - rcnt) ++ frt1; while (frt2 <= tal2 && q2[frt2] <= q1[frt1]) ++ frt2; while (frt2 <= tal2 && g[ring[q2[tal2]]] + q2[tal2] < g[ring[i]] + i) -- tal2; q2[++ tal2] = i; spc[nxt[ring[i]]] = Max(spc[nxt[ring[i]]], g[ring[q1[frt1]]] - q1[frt1] + g[ring[q2[frt2]]] + q2[frt2]); while (frt1 <= tal1 && g[ring[q1[tal1]]] - q1[tal1] < g[ring[i]] - i) -- tal1; q1[++ tal1] = i; } for (int i = 1; i <= n; ++ i) if (ve[i << 1]) printf("%d\n", spc[i]); elseprintf("-1\n"); return0; }
#T4 turing machine
Time Limit: 1s Memory Limit: 512MiB
#题意简述
给定程序
1 2 3 4 5 6 7 8 9 10 11 12 13 14
read(n);read(T); for i = 1to n do read(a[i]);//a[i] must be 0 or 1 for t = 1to T do begin for i = 2to n do do_swap[i]=(a[i-1]==0) and (a[i]==1) for i = 2to n do if (do_swap[i]) then swap(a[i-1],a[i]); end for i = 1to n do write(a[i],' ');
template <typename T> inlinevoidread(T &x){ x = 0; int f = 1; char c = getchar(); for (; !isdigit(c); c = getchar()) if (c == '-') f = -f; for (; isdigit(c); c = getchar()) x = x * 10 + c - '0'; x *= f; }
int n, m, a[N], q[N], frt, tal = -1, tot;
intmain(){ read(n), read(m); for (int i = m - 1; ~i; -- i) q[++ tal] = i; for (int i = 1, lst = 0; i <= n; ++ i) { int x; read(x); if (!x) continue; int len = i - lst - 1; ++ tot; q[++ tal] = -tot; tal = max(frt - 1, tal - len); while (frt <= tal && q[frt] + tot >= m) ++ frt; a[i - m + tal - frt + 1] = 1, lst = i; } for (int i = 1; i <= n; ++ i) printf("%d ", a[i]); return0; }