Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

两个单调队列+哈希,1750MS

Posted by SG_SiQing at 2013-10-08 15:45:30 on Problem 2452
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator