1017 The Best Peak Shape (35 分)(最佳峰形)(思路+详解+翻译+题意分析)Come brather!!!!!!!!!
一:題目:
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
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:
結尾無空行
Sample Input2:
Sample Output2:
No peak shape二:翻譯:
在許多研究領域中,分析數據的一個重要目標就是從大量充滿噪聲的原始數據中找到最佳的“峰形”。長度為L的“峰形”是L數{D的有序序列
現在給定N個輸入數字,按照它們的索引排序,你可以刪除其中的一些數字,以保持其余數字的峰值形狀。最佳峰形是形成峰形的最長子序列。如果有一個平局,那么將選擇最對稱的(即增加子序列和減少子序列的長度之差是最小的)一個。
輸入規格:
每個輸入文件包含一個測試用例。對于每種情況,第一行給出整數N(3≤N≤10)
4
). 然后在下一行給出N個整數,用空格隔開。所有整數的取值范圍為[?10000,10000]。
輸出規范:
對于每種情況,在一行中打印最佳峰值形狀的長度、索引(從1開始)和峰值數的值。如果不存在解決方案,只需在一行中打印“無峰形”。法官的輸入保證了輸出的唯一性。
示例Input1:
20.1 3 0 8 5 -2 29 20 20 4 10 4 7 25 18 6 17 16 2 -1結尾無空行
示例Output1:
10 14 25結尾無空行
示例Input2:
51 3 8 10 20示例Output2:
No peak shape三:分析題意+思路
分析題意:1.想要求一個帶有峰值的 最長峰形,即峰值前面的數小于峰值,
峰值后面的數小于峰值
2.如果出現長度一樣的峰型,則考慮比較對稱的峰形,
即峰值左右數的個數相差比較少的
思路:我們要求的結果的過程是動態的也就是在變化,所以這類題是動態規劃
統計每個數值的最佳峰形長度,最后求出最大值
那么在統計每個數值的峰形長度 = 數值前面比起小的個數 + 本身(1)+ 后面比起大的數
下標為 i 的數值 前面比起小的數
m_first[i] = max(m_first[i],m_first[前面的數] + 1),這里加一是表示統計第一次前面有數比起小的時候為1
下標為 i 的數值 后面比起小的數
m_last[i] = max(m_last[i],m_last[后面的數] + 1)
四:上碼
/**分析題意:1.想要求一個帶有峰值的 最長峰形,即峰值前面的數小于峰值,峰值后面的數小于峰值 2.如果出現長度一樣的峰型,則考慮比較對稱的峰形,即峰值左右數的個數相差比較少的思路:我們要求的結果的過程是動態的也就是在變化,所以這類題是動態規劃 統計每個數值的最佳峰形長度,最后求出最大值那么在統計每個數值的峰形長度 = 數值前面比起小的個數 + 本身(1)+ 后面比起大的數下標為 i 的數值 前面比起小的數 m_first[i] = max(m_first[i],m_first[前面的數] + 1),這里加一是表示統計第一次前面有數比起小的時候為1下標為 i 的數值 后面比起小的數m_last[i] = max(m_last[i],m_last[后面的數] + 1) */ #include<bits/stdc++.h> using namespace std;int main(){int m_first[10010];//存的是m[i] 前面比起小的個數 int m_last[10010]; //存的是m[i] 后面比起小的個數 int m[10010];// 存放的輸入的數值memset(m_first,0,sizeof(m_first));memset(m_last,0,sizeof(m_last));memset(m,0,sizeof(m));int N;cin >> N;for(int i = 1; i <= N; i++){cin >> m[i];}//統計 峰值 前面的遞增的序列當中的數個數for(int i = 1; i <= N; i++){for(int j = 1; j <= i - 1; j++){if(m[j] < m[i])m_first[i] = max(m_first[i],m_first[j] + 1);//這里的每次更新,會將有的數值均前面比起大的 也就是m_first[i] = 0,那么后面的數統計到這時候會均比0大}} //從后往前 也就是峰值后面的遞減序列for(int i = N; i >= 1; i--){for(int j = N; j >= i + 1; j--){if(m[j] < m[i]){m_last[i] = max(m_last[i],m_last[j] + 1); }}} //開始統計不同下標下的個數 int max = 0;for(int i = 1; i <= N; i++){int temp = m_first[i] + m_last[i];int maxx = m_first[max] + m_last[max];if(m_first[i] == 0 || m_last[i] == 0)//如果出現遞增或遞減序列 那么其m_last[i] == 0, m_first[i] == 0;continue;else if(temp > maxx)max = i;//這種情況就是 長度一致,我們要選擇更加對稱的 else if(temp == maxx && (abs(m_first[i] - m_last[i])) < (abs(m_first[max] - m_last[max])) )max = i; } //如果出現遞增或是遞減的序列則 m_first[max] + m_last[max] == 0; if(m_first[max] + m_last[max] > 0){ cout << m_first[max] + 1 + m_last[max] << ' ' << max << ' ' << m[max]; }else{cout << "No peak shape";} }加油陌生人,我們共勉!如有疑問歡迎留言!!!!!!!
總結
以上是生活随笔為你收集整理的1017 The Best Peak Shape (35 分)(最佳峰形)(思路+详解+翻译+题意分析)Come brather!!!!!!!!!的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 全新C4D必备插件合集他来啦傻瓜式一键安
- 下一篇: 455. 分发饼干001(贪心算法+详解