问题描述:
1017. The Best Peak Shape (35)
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 -1Sample Output 1:
10 14 25Sample Input 2:
5 -1 3 8 10 20Sample Output 2:
No peak shape
这一题其实是最小不下降子列问题的变形,用相同的方法就能AC;
AC代码(因为发现PAT其实是支持C++11的,于是代码风格变成C++11的了。。。):
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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
#include<bits/stdc++.h> using namespace std; int main() { // freopen("data.txt","r",stdin); int n,k,x; int msum=0,mmin=99999; scanf("%d",&n); vector<pair<int,int> > v(n,make_pair(0,0)); vector<int> vm; for(auto& i:v) { scanf("%d",&x); i.first=x; if(vm.empty()) vm.emplace_back(x); else { if(x>vm.back()) { vm.emplace_back(x); i.second=vm.size()-1; } else for(int j=vm.size()-2;;--j) { if(x>vm[j]) { i.second=j+1; if(x<vm[j+1]) vm[j+1]=x; break; } if(j<0) { i.second=0; if(x<vm[0]) vm[0]=x; break; } } } } vm.clear(); x=0; for(int i=v.size()-1;i>-1;--i) { int p; if(vm.empty()) { vm.emplace_back(v[i].first); p=0; } else { if(v[i].first>vm.back()) { vm.emplace_back(v[i].first); p=vm.size()-1; } else for(int j=vm.size()-2;;--j) { if(v[i].first>vm[j]) { p=j+1; if(v[i].first<vm[j+1]) vm[j+1]=v[i].first; break; } if(j<0) { p=0; if(v[i].first<vm[0]) vm[0]=v[i].first; break; } } } if(p+v[i].second>msum) { msum=p+v[i].second; mmin=abs(p-v[i].second); x=i; } else if(p+v[i].second==msum) { if(mmin>abs(p-v[i].second)) { mmin=abs(p-v[i].second); x=i; } } } if(x==v.size()-1||x==0) printf("No peak shape"); else printf("%d %d %d",msum+1,x+1,v[x].first); } |