码迷,mamicode.com
首页 > 其他好文 > 详细

[hdu4513]常规dp

时间:2015-07-28 07:56:45      阅读:109      评论:0      收藏:0      [点我收藏+]

标签:

题意:给一个长度为m的序列,从里面选出一些数,相对位置不发生变化,并满足a[i]=a[n-i],a[1]<a[2]<...<a[(n+1)/2],n是数的个数,求最大的n

思路:dp[i][j]表示0~i,j~m的答案,则dp[i][j]=dp[l][r]+1+(i<j),其中a[i]=a[j]>a[l]=a[r]&&l<i<=j<r。枚举3个变量i,j,r,维护一个l就行了,o(m^3)。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* ******************************************************************************** */
#include <iostream>                                                                 //
#include <cstdio>                                                                   //
#include <cmath>                                                                    //
#include <cstdlib>                                                                  //
#include <cstring>                                                                  //
#include <vector>                                                                   //
#include <ctime>                                                                    //
#include <deque>                                                                    //
#include <queue>                                                                    //
#include <algorithm>                                                                //
using namespace std;                                                                //
                                                                                    //
#define pb push_back                                                                //
#define mp make_pair                                                                //
#define X first                                                                     //
#define Y second                                                                    //
#define all(a) (a).begin(), (a).end()                                               //
#define foreach(i, a) for (typeof(a.begin()) it = a.begin(); it != a.end(); it ++)  //
                                                                                    //
void RI(vector<int>&a,int n){a.resize(n);for(int i=0;i<n;i++)scanf("%d",&a[i]);}    //
void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>                    //
void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?1:-1;          //
while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>      //
void print(const T t){cout<<t<<endl;}template<typename F,typename...R>              //
void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>   //
void print(T*p, T*q){int d=p<q?1:-1;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}   //
                                                                                    //
typedef pair<intint> pii;                                                         //
typedef long long ll;                                                               //
typedef unsigned long long ull;                                                     //
                                                                                    //
/* -------------------------------------------------------------------------------- */
                                                                                    //
template<typename T>bool umax(T &a, const T &b) {
    return a >= b? false : (a = b, true);
}
 
int a[300], dp[300][300];
 
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt""r", stdin);
#endif // ONLINE_JUDGE
    int T;
    RI(T);
    while (T --) {
        int n;
        RI(n);
        RI(a + 1, a + 1 + n);
        int p[300] = {};
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for (int i = 1; i <= n; i ++) {
            for (int j = n; j >= i; j --) {
                if (a[i] != a[j]) continue;
                dp[i][j] = 1 + (i < j);
                for (int k = j + 1; k <= n; k ++) {
                    if (a[k] < a[i]) {
                        if (p[a[k]] && p[a[k]] < i)
                            umax(dp[i][j], dp[p[a[k]]][k] + 1 + (i < j));
                    }
                }
                umax(ans, dp[i][j]);
            }
            p[a[i]] = i;
        }
        cout << ans << endl;
    }
    return 0;                                                                       //
}                                                                                   //
                                                                                    //
                                                                                    //
                                                                                    //
/* ******************************************************************************** */

 

[hdu4513]常规dp

标签:

原文地址:http://www.cnblogs.com/jklongint/p/4681719.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!