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 |
彻底崩溃。。。。。TLE, 二分加递归到底怎么写才能过啊?求救!#include <iostream> using namespace std; int a[50001]; int max, n; int solve(int beg, int end) { if (beg >= end) return 0; if (end-beg == 1) { if (a[end] > a[beg]) { if (max < 1) max = 1; } return 0; } solve(beg, (beg+end)/2);//左边区间 solve((beg+end)/2, end);//右边区间 //以下是包括mid点的区间 int mid = (beg+end)/2; int p = mid-1; int q = mid+1; if (p == 0 || q == n+1) return 0; int i, j, pp, qq, tmp1, tmp2, tmp3; bool flag1 = false, flag2 = false; tmp1 = a[mid]; tmp2 = a[mid]; tmp3; for (i=p; i>=beg; i--) { if (a[i] > tmp2) tmp2 = a[i]; if (a[i] < tmp1) { tmp1 = a[i]; tmp3 = tmp2; for (j=q; j<=end; j++) { if (a[j] > tmp2) { tmp3 = a[j]; if (max < j-i) max = j-i; } } } } tmp2 = a[mid]; for (i=q; i<=end; i++) { if (a[i] < a[mid]) break; if (a[i] > tmp2) { tmp2 = a[i]; if (max < i-mid) max = i-mid; } } tmp1 = a[mid]; for (i=p; i>=beg; i--) { if (a[i] > a[mid]) break; if (a[i] < tmp1) { tmp1 = a[i]; if (max < mid-i) max = mid-i; } } return 0; } int main() { int i; while (scanf("%d", &n) != EOF) { for (i=1; i<=n; i++) scanf("%d", &a[i]); max = -1; solve(1, n); printf("%d\n", max); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator