MUTC2013 E-Deque-hdu 4604

Deque

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 786    Accepted Submission(s): 253


Problem Description
Today, the teacher gave Alice extra homework for the girl weren't attentive in his class. It's hard, and Alice is going to turn to you for help.
The teacher gave Alice a sequence of number(named A) and a deque. The sequence exactly contains N integers. A deque is such a queue, that one is able to push or pop the element at its front end or rear end. Alice was asked to take out the elements from the sequence in order(from A_1 to A_N), and decide to push it to the front or rear of the deque, or drop it directly. At any moment, Alice is allowed to pop the elements on the both ends of the deque. The only limit is, that the elements in the deque should be non-decreasing.
Alice's task is to find a way to push as many elements as possible into the deque. You, the greatest programmer, are required to reclaim the little girl from despair.
 

Input
The first line is an integer T(1≤T≤10) indicating the number of test cases.
For each case, the first line is the length of sequence N(1≤N≤100000).
The following line contains N integers A1,A2,…,AN.
 

Output
For each test case, output one integer indicating the maximum length of the deque.
 

Sample Input
3 7 1 2 3 4 5 6 7 5 4 3 2 1 5 5 5 4 1 2 3
 

Sample Output
7 5 3
 

Source
 

Recommend
liuyiding
 
------------
考虑题目的一个简化版本:使双端队列单调上升。对于序列A 和队列Q,找到队列中最早出
现的数字Ax,则Ax将 Q 分成的两个部分分别是原序列中以Ax开始的最长上升和最长下降序
列,答案即为这两者之和的最大值。而对于本题,由于存在相同元素,所以只要找到以Ax 
为起点的最长不下降序列和最长不上升序列的和,然后减去两个里面出现Ax次数的最小值
即可。 
-------------
题解要按字面意思理解。。。。
里面有深刻的内涵。。。
--------
/** head-file **/

#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <list>
#include <set>
#include <map>
#include <algorithm>

/** define-for **/

#define REP(i, n) for (int i=0;i<int(n);++i)
#define FOR(i, a, b) for (int i=int(a);i<int(b);++i)
#define DWN(i, b, a) for (int i=int(b-1);i>=int(a);--i)
#define REP_1(i, n) for (int i=1;i<=int(n);++i)
#define FOR_1(i, a, b) for (int i=int(a);i<=int(b);++i)
#define DWN_1(i, b, a) for (int i=int(b);i>=int(a);--i)
#define REP_N(i, n) for (i=0;i<int(n);++i)
#define FOR_N(i, a, b) for (i=int(a);i<int(b);++i)
#define DWN_N(i, b, a) for (i=int(b-1);i>=int(a);--i)
#define REP_1_N(i, n) for (i=1;i<=int(n);++i)
#define FOR_1_N(i, a, b) for (i=int(a);i<=int(b);++i)
#define DWN_1_N(i, b, a) for (i=int(b);i>=int(a);--i)

/** define-useful **/

#define clr(x,a) memset(x,a,sizeof(x))
#define sz(x) int(x.size())
#define see(x) cerr<<#x<<" "<<x<<endl
#define se(x) cerr<<" "<<x
#define pb push_back
#define mp make_pair

/** test **/

#define Display(A, n, m) {                      
    REP(i, n){                                  
        REP(j, m) cout << A[i][j] << " ";       
        cout << endl;                           
    }                                           
}

#define Display_1(A, n, m) {                    
    REP_1(i, n){                                
        REP_1(j, m) cout << A[i][j] << " ";     
        cout << endl;                           
    }                                           
}

using namespace std;

/** typedef **/

typedef long long LL;

/** Add - On **/

const int direct4[4][2]={ {0,1},{1,0},{0,-1},{-1,0} };
const int direct8[8][2]={ {0,1},{1,0},{0,-1},{-1,0},{1,1},{1,-1},{-1,1},{-1,-1} };
const int direct3[6][3]={ {1,0,0},{0,1,0},{0,0,1},{-1,0,0},{0,-1,0},{0,0,-1} };

const int MOD = 1000000007;
const int INF = 0x3f3f3f3f;
const long long INFF = 1LL << 60;
const double EPS = 1e-9;
const double OO = 1e15;
const double PI = acos(-1.0); //M_PI;
const int maxn=111111;

int finc[maxn],fdec[maxn],a[maxn],same[maxn];
int n;

void LXS(int* a,int* f,int* same)
{
    vector<int>d;
    int l,r;
    REP(i,n)
    {
        l=lower_bound(d.begin(),d.end(),a[i])-d.begin();
        r=upper_bound(d.begin(),d.end(),a[i])-d.begin();
        if (r==sz(d)) d.push_back(a[i]);
        else d[r]=a[i];
        f[i]=r+1;
        same[i]=min(same[i],r-l+1);
    }
}

int main()
{
    int T;
    cin>>T;
    while(T--)
    {
        cin>>n;
        REP(i,n) cin>>a[i];
        reverse(a,a+n);
        clr(same,INF);
        LXS(a,fdec,same);
        REP(i,n) a[i]=-a[i];
        LXS(a,finc,same);
        //REP(i,n) se(finc[i]); cout<<endl;
        //REP(i,n) se(fdec[i]); cout<<endl;
        int ans=0;
        REP(i,n) ans=max(ans,fdec[i]+finc[i]-same[i]);
        cout<<ans<<endl;
    }
    return 0;
}




原文地址:https://www.cnblogs.com/cyendra/p/3226274.html