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;}
- 1017. The Best Peak Shape (35)
- 1017. The Best Peak Shape (35)
- pat-top 1017. The Best Peak Shape (35)
- 1017. The Best Peak Shape (35)解题报告
- 【PAT】1017. The Best Peak Shape
- PAT (Top Level) Practise 1017 The Best Peak Shape (35)
- the peak with the mountain
- LeetCode find the peak element
- UVA11800 Determine the Shape
- UVA11800 - Determine the Shape
- Simple Is The Best
- PK王纪敏佳,the best!
- The Best Songs!!
- The best tester is...
- The best art critic
- The best tester
- To be the best
- the best toner cartridge
- 【二分图】poj 3020 Antenna Placement
- (85)字节流读写、缓冲区
- 洛谷P1028 数的计算
- 本地如何搭建FPT服务
- 版权声明
- 1017. The Best Peak Shape (35)
- vector
- HTML块级标签的补充
- 关于技术博客文章目录
- 链表的一些操作
- 相机标定 calib3d 学习笔记
- C++继承与多态(一)
- R实战:【绘图】R中的颜色Chart of R Colors
- 【Docker】win10环境下安装Docker