Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
两个单调队列+哈希,1750MS#include <stdio.h> #define maxn 50005 int a[maxn]; int s[maxn]; int l[maxn]; int q[maxn], t; inline int max(int a, int b) { return a > b ? a : b; } int main() { int n, res; a[0] = 100005; while (scanf("%d", &n) != EOF) { for (int i = 1; i <= n; i++) scanf("%d", &a[i]); a[++n] = res = 0; for (int i = 1, t = 0; i <= n; i++) { while (t && a[i] < a[q[t]]) { s[q[t]] = i - 1; t--; } q[++t] = i; } for (int i = n - 1, t = 0; i >= 0; i--) { while (t && a[i] > a[q[t]]) { l[q[t]] = i + 1; t--; } q[++t] = i; } for (int i = 1; i < n; i++) q[i] = i; for (int i = 1; i < n; i++) { while (s[l[i]] < i) l[i] = s[l[i]] + 1; q[l[i]] = i; } for (int i = 1; i < n; i++) res = max(res, q[i] - i); if (res == 0) res = -1; printf("%d\n", res); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator