/* THE PROGRAM IS MADE BY PYY */ /*----------------------------------------------------------------------------// Copyright (c) 2012 panyanyany All rights reserved. URL : http://ac.jobdu.com/problem.php?pid=1131 Name : 题目1131:合唱队形 Date : Sunday, July 8, 2012 Time Stage : 3 hours Result: 277086 panyanyany 1131 Accepted 1516 kb 840 ms C++ / Edit 11:06:23 Test Data : Review : 呜呜呜呜呜~~又做了三四个小时。改来改去都不成功啊。刚想的时候以为可以打乱次序来排, 结果一对比数据发现是不行的。于是果断思考DP的思路。后来用了一种很麻烦的方法,又过不了。 于是果断找题解。其实看题解的话真是好简单的样子。个人感觉小媛姐的比较清晰点。这题说白了 就是以某个人为最高点,进行向左向右的两次 “最长降序子序列算法”。 -----欢迎进入传送门------- http://blog.csdn.net/zxy_snow/article/details/6064297 http://xieyan87.com/?p=195 我写了一个函数: int LDesS(int dp[], int a[], int n, int dir = RIGHT); 主要是求最长下降子序列的,不过可以设定方向(dir)。如果dir == RIGHT,就表示向右递减,也就是正常的 dp,dir == LEFT 表示向左递减,就是反方向的dp了。这个函数还真是有点纠结……最后,因为求LDS的时候同一个位置的元素被加了两次,所以答案要减1. //----------------------------------------------------------------------------*/ #include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> #include <vector> #include <algorithm> #include <iostream> #include <queue> #include <set> #include <string> using namespace std ; #define MEM(a, v) memset (a, v, sizeof (a)) // a for address, v for value #define max(x, y) ((x) > (y) ? (x) : (y)) #define min(x, y) ((x) < (y) ? (x) : (y)) #define INF (0x3f3f3f3f) #define MAXN 309 #define L(x) ((x)<<1) #define R(x) (((x)<<1)|1) #define M(x, y) (((x)+(y)) >> 1) #define DB // int DesLeft[MAXN], DesRight[MAXN], a[MAXN], cnt[MAXN]; enum { LEFT, RIGHT }; int LDesS(int dp[], int a[], int n, int dir = RIGHT) { int i, j, k, beg, end, step; if (RIGHT == dir) { beg = 0; end = n; step = 1; } else { beg = n - 1; end = -1; step = -1; } for (i = beg; i != end; i += step) { dp[i] = 1; for (j = beg; j != i; j += step) { if (a[i] > a[j]) dp[i] = max(dp[i], dp[j] + 1); } } // printf ("LDecs: pos:%d, dir:%d, return:%d\n", pos, dir, dp[pos]); return dir; } int DblDirectSeq(int a[], int n) { int i, k; LDesS(DesRight, a, n, RIGHT); LDesS(DesLeft, a, n, LEFT); k = 0; for (i = 0; i < n; ++i) DesRight[i] += DesLeft[i] - 1; for (i = 0; i < n; ++i) k = max(k, DesRight[i]); return n - k; } int main() { int i, n; while (scanf("%d", &n) != EOF) { for(i = 0; i < n; ++i) scanf("%d", a+i); printf("%d\n", DblDirectSeq(a, n)); } return 0; }