Run ID | 作者 | 问题 | 语言 | 测评结果 | 分数 | 时间 | 内存 | 代码长度 | 提交时间 |
---|---|---|---|---|---|---|---|---|---|
1885 | 18级尹玉文东 | 【S】T1最大后缀值个数 | C++ | 运行超时 | 70 | 2066 MS | 76464 KB | 2467 | 2020-11-16 17:17:10 |
#include <bits/stdc++.h> #define MAXN 1000008 using namespace std; inline int read() { int ret = 0; char ch = getchar(); while(ch < '0' || ch > '9') ch = getchar(); while(ch <= '9' && ch >= '0') { ret = ret * 10 + ch - '0'; ch = getchar(); } return ret; } void out(int x) { if (x >= 10) out(x / 10); putchar(x % 10 + '0'); } int n; int f[MAXN], val[MAXN]; struct Edge{int to, nxt;} edge[2 * MAXN]; int head[MAXN], cnt = 0; void add(int x, int y) { edge[++cnt].to = y; edge[cnt].nxt = head[x]; head[x] = cnt; } int st[MAXN], top = 0;//单调栈 int ans[MAXN]; void dfs(int now, int fa) { int chi, chv, cht = top;//change_id, change_val, change_top if(top == 0) { st[++top] = val[now]; chi = 1; chv = 0; } else { while(st[top] < val[now] && top > 0) top--; chi = top + 1; chv = st[top + 1]; st[++top] = val[now]; } ans[now] = top; // cout << "now: " << now << endl; // for(int i = 1; i <= top; i++) { // cout << st[i] << " "; // } // cout << endl; for(int i = head[now]; i; i = edge[i].nxt) { int ne = edge[i].to; if(ne == fa) continue; dfs(ne, now); } st[chi] = chv; top = cht; } bool subtask = true;//链 int main() { n = read(); for(int i = 2; i <= n; i++) { f[i] = read(); if(f[i] != i - 1) subtask = false; add(f[i], i); add(i, f[i]); } for(int i = 1; i <= n; i++) { val[i] = read(); } if(n <= 10) { int mmax, cnt; for(int i = 1; i <= n; i++) { mmax = val[i], cnt = 0; for(int j = i; j; j = f[j]) { if(val[j] >= mmax) { mmax = val[j]; cnt++; } } out(cnt); putchar(' '); } return 0; } else if(subtask) { for(int i = 1; i <= n; i++) { if(top == 0) { st[++top] = val[i]; } else { while(st[top] < val[i] && top > 0) top--; st[++top] = val[i]; } out(top); putchar(' '); } return 0; } else { dfs(1, 0); for(int i = 1; i <= n; i++) { out(ans[i]); putchar(' '); } return 0; } return 0; }