1017. The Best Peak Shape (35)

来源:互联网 发布:网络枪支案件判刑 编辑:程序博客网 时间:2024/06/06 02:01

基于dp 01背包的思想,要对背包压缩一下,不然会超内存
In many research areas, one important target of analyzing data is to find the best “peak shape” out of a huge amount of raw data full of noises. A “peak shape” of length L is an ordered sequence of L numbers { D1, …, DL } satisfying that there exists an index i (1 < i < L) such that D1 < … < Di-1 < Di > Di+1 > … > DL.

Now given N input numbers ordered by their indices, you may remove some of them to keep the rest of the numbers in a peak shape. The best peak shape is the longest sub-sequence that forms a peak shape. If there is a tie, then the most symmetric (meaning that the difference of the lengths of the increasing and the decreasing sub-sequences is minimized) one will be chosen.

Input Specification:

Each input file contains one test case. For each case, the first line gives an integer N (3 <= N <= 104). Then N integers are given in the next line, separated by spaces. All the integers are in [-10000, 10000].

Output Specification:

For each case, print in a line the length of the best peak shape, the index (starts from 1) and the value of the peak number. If the solution does not exist, simply print “No peak shape” in a line. The judge’s input guarantees the uniqueness of the output.

Sample Input 1:
20
1 3 0 8 5 -2 29 20 20 4 10 4 7 25 18 6 17 16 2 -1
Sample Output 1:
10 14 25
Sample Input 2:
5
-1 3 8 10 20
Sample Output 2:
No peak shape
提交代码

#include<iostream>#include<algorithm>using namespace std;#define XX 10001#define MM 20004int dp1[20005],dp2[20005];int all[10005];int dpp[10005],dpl[10005];int main(){    int N;    cin >> N;    for (int i = 1; i <= N; ++i) {        cin >> all[i];        dpp[i] = dp1[all[i] + XX];        int tep = dp1[all[i] + XX ] + 1;        for (int j = all[i] + XX+1; j < MM; ++j)            dp1[j] = max(dp1[j], tep);    }    for (int i = N; i > 0; --i) {        int tep = dp2[all[i] + XX ] + 1;        dpl[i] = dp2[all[i] + XX];        for (int j = all[i] + XX+1; j < MM; ++j)            dp2[j] = max(dp2[j], tep);    }    int p_min = 0, s_max = 0, re_n;    for (int i = 1; i <= N; ++i) {        if (dpp[i] + dpl[i] + 1 > s_max ||            (dpp[i] + dpl[i] + 1 == s_max && abs(dpp[i] - dpl[i]) < p_min))        {            if (!dpp[i] || !dpl[i]) continue;            p_min = abs(dpp[i] - dpl[i]);            re_n = i;            s_max = dpp[i] + dpl[i] + 1;        }    }    if (s_max)        cout << s_max << " " << re_n << " " << all[re_n] << endl;    else        cout << "No peak shape" << endl;}
原创粉丝点击