PAT (Top Level) Practice 1017 The Best Peak Shape (35 分) 題解

----
源代碼位於:https://github.com/yunwei37/myClassNotes
還有不少數據結構和算法相關的筆記以及pta題解哦x
----

PTA top level 練習裏面的一道題目...挺簡單的,就是讀懂題目然後注意一下邊界條件。

是浙江大學高級數據結構與算法課程的一個小作業啦

# 思路

就是兩個最大上升子序列,從正反兩個方向進行,然後把結果疊加到一起就好啦。不算註釋的話30行代碼...可以說非常少了x

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 { D​1​​, ⋯, D​L​​ } satisfying that there exists an index i (1<i<L) such that D​1​​<⋯<D​i−1​​<D​i​​>D​i+1​​>⋯>D​L​​.

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≤10​4​​). 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 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

代碼:

 

/*
	Name: The Best Peak Shape
	Author: zheng yusheng
	Date: 21/04/19 14:27
	Description: the solve of project "The Best Peak Shape" using dp algorithm
*/

#include <stdio.h>
#include <math.h>
#define max(a,b) (a>b?(a):(b))
/*forward dp, find the length of the increasing sequence end with the ith number*/
int up[10010];
/*backward dp, find the length of the decreasing sequence end with the ith number*/
int down[10010];
//the number sequence is stored here, begin at num[1];
int num[10010];

int main() {
	int n,max,j;
	scanf("%d",&n);//get the integer N 
	int i;
	for(i=0; i<n; ++i)
		scanf("%d",num+i+1);//get N numbers;
	//forward dp: up[i] = the max length of the increasing sequence end with the number before num[i] and smaller than num[i]
	for(int i=1; i<=n; i++)
		for(int j=1; j<=i-1; j++)
			if (num[j] < num[i]) up[i] = max(up[i], up[j] + 1);
	//backward dp: up[i] = the max length of the decreasing sequence end with the number after num[i] and smaller than num[i]
	for(int i=n; i>=1; i--) 
		for(int j=n; j>=i+1; j--)
			if (num[j] < num[i]) down[i] = max(down[i], down[j] + 1);
	//add the results of the forward dp and backward together, and find the max one;
	max=0;//the peak position of the max length peak shape(guard:the length is zero)
	for(i=1; i<=n; ++i) {
		if(up[i]==0||down[i]==0) continue;//skip results that cannot form a peak shape;
		else if(up[i]+down[i]>up[max]+down[max])//if the lenth is larger, update the position of the num; 
			max=i;
		else if(up[i]+down[i]==up[max]+down[max]&&abs(up[i]-down[i])<abs(up[max]-down[max]))
			max=i;//minimize the difference of the lengths of the increasing and the decreasing sub-sequences
	}
	if (up[max]+down[max] > 0)//if there exist a solution, output the result
		printf("%d %d %d",up[max]+down[max]+1,max,num[max]);
	else printf("No peak shape\n");//no solution
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章