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

MY TLE的DP

Posted by lcjh at 2010-02-05 21:35:55 on Problem 3298
#include <iostream>

using namespace std;

int N;
int num[30001];
int dp[30001][2];

int main()
{
    int T;
    scanf("%d" , &T);

    while(T --)
    {
        int i , j;
        scanf("%d" , &N);
        for(i = 1 ; i <= N ; i ++)
            scanf("%d" , &num[i]);

        dp[1][0] = dp[1][1] = num[1];
        int l1 = 1 , l2 = 1;
        for(i = 2 ; i <= N ; i ++)
        {
            if(dp[1][1] < num[i])
                dp[1][1] = num[i];
            for(j = 2 ; j < l2 ; j ++)
            {
                if(dp[j][0] < num[i] && num[i] > dp[j+1][1])
                    dp[j+1][1] = num[i];
            }
            for(j = 1 ; j < l1 ; j ++)
            {
                if(dp[j][1] > num[i] && num[i] < dp[j+1][0])
                    dp[j+1][0] = num[i];
            }
            if(dp[l1][0] < num[i] && l1 != 1)
            {
                if(l2 <= l1)
                {
                    l2 = l1 + 1;
                    dp[l2][1] = num[i];
                }
                else if(l1 == l2 + 1 && num[i] > dp[l2][1])
                {
                    dp[l2][1] = num[i];
                }
            }
            if(dp[l2][1] > num[i])
            {
                if(l1 <= l2)
                {
                    l1 = l2 + 1;
                    dp[l1][0] = num[i];
                }
                else if(l1 == l2 - 1 && num[i] < dp[l1][0])
                    dp[l1][0] = num[i];
            }
        }

        if(l1 < l2)
            l1 = l2;
        printf("%d\n" , l1);
    }

    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