The Best Peak Shape

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) such that D1​​<<Di1​​<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). Then N integers are given in the next line, separated by spaces. All the integers are in [.

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 Input1:

20
1 3 0 8 5 -2 29 20 20 4 10 4 7 25 18 6 17 16 2 -1
 

Sample Output1:

10 14 25
 

Sample Input2:

5
-1 3 8 10 20
 

Sample Output2:

No peak shape
 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 int main()
 4 {
 5 //    freopen("data.txt","r",stdin);
 6     int n,k,x;
 7     int msum=0,mmin=99999;
 8     scanf("%d",&n);
 9     vector<pair<int,int> > v(n,make_pair(0,0));
10     vector<int> vm;
11     for(auto& i:v)
12     {
13         scanf("%d",&x);
14         i.first=x;
15         if(vm.empty())
16         vm.emplace_back(x);
17         else
18         {
19             if(x>vm.back())
20             {
21                 vm.emplace_back(x);
22                 i.second=vm.size()-1;
23             }
24             else
25             for(int j=vm.size()-2;;--j)
26             {
27                 if(x>vm[j])
28                 {
29                     i.second=j+1;
30                     if(x<vm[j+1])
31                     vm[j+1]=x;
32                     break;
33                 }
34                 if(j<0)
35                 {
36                     i.second=0;
37                     if(x<vm[0])
38                     vm[0]=x;
39                     break;
40                 }
41             }
42         }
43     }
44     vm.clear();
45     x=0;
46     for(int i=v.size()-1;i>-1;--i)
47     {
48         int p;
49         if(vm.empty())
50         {
51             vm.emplace_back(v[i].first);
52             p=0;
53         }
54         else
55         {
56             if(v[i].first>vm.back())
57             {
58                 vm.emplace_back(v[i].first);
59                 p=vm.size()-1;
60             }
61             else
62             for(int j=vm.size()-2;;--j)
63             {
64                 if(v[i].first>vm[j])
65                 {
66                     p=j+1;
67                     if(v[i].first<vm[j+1])
68                     vm[j+1]=v[i].first;
69                     break;
70                 }
71                 if(j<0)
72                 {
73                     p=0;
74                     if(v[i].first<vm[0])
75                     vm[0]=v[i].first;
76                     break;
77                 }
78             }
79         }
80         if(p+v[i].second>msum)
81         {
82             msum=p+v[i].second;
83             mmin=abs(p-v[i].second);
84             x=i;
85         }
86         else if(p+v[i].second==msum)
87         {
88             if(mmin>abs(p-v[i].second))
89             {
90                 mmin=abs(p-v[i].second);
91                 x=i;
92             }
93         }
94     }
95     if(x==v.size()-1||x==0)
96     printf("No peak shape");
97     else
98     printf("%d %d %d",msum+1,x+1,v[x].first);
99 }

 

posted @ 2020-02-08 23:52  一斜星辰酱  阅读(515)  评论(0编辑  收藏  举报