1017. The Best Peak Shape (35)解题报告
来源:互联网 发布:淘宝客服绩效考核工具 编辑:程序博客网 时间:2024/06/12 18:06
原题链接:
1017. The Best Peak Shape (35)
解题思路:
- 对于每个元素,求以其结尾的最长严格递增子序列。Ii是以下标为i元素结尾的最长严格递增子序列。
- 对于每个元素,求以其开头的最长严格递减子序列。Di是以下标为i元素开头的最长严格递减子序列。
- 遍历每个元素,求Ii+Di-1最大者。
通过画面:
代码:
#define _CRT_SECURE_NO_WARNINGS#include <cstdio>#include <cstdlib>#include <cassert>#include <cmath>#include <algorithm>#include <vector>using namespace std;int main(){int n = 0;assert(1 == scanf("%d", &n));vector<int> input(n + 1, 0), forward(n + 1, 0), backward(n + 1, 0);for (int i = 1; i <= n; i++) {int tmp = 0;assert(1 == scanf("%d", &tmp));input[i] = tmp;}for (int i = 2; i <= n; i++) {for (int j = i - 1; j >= 1; j--) {if (input[i] > input[j]) {forward[i] = max(forward[i], forward[j] + 1);}}}for (int i = n - 1; i >= 1; i--) {for (int j = i + 1; j <= n; j++) {if (input[i] > input[j]) {backward[i] = max(backward[i], backward[j] + 1);}}}int maxLen = 0, index = 0, value = 0, diff = 100000;for (int i = 2; i < n; i++) {if (forward[i] && backward[i]) {if (forward[i] + backward[i] + 1 > maxLen) {maxLen = forward[i] + backward[i] + 1;index = i;value = input[i];diff = abs(forward[i] - backward[i]);}else if (forward[i] + backward[i] + 1 == maxLen && diff > abs(forward[i] - backward[i])) {index = i;value = input[i];diff = abs(forward[i] - backward[i]);}}}if (maxLen) {printf("%d %d %d\n", maxLen, index, value);}else {puts("No peak shape");}return 0;}
阅读全文
0 0
- 1017. The Best Peak Shape (35)解题报告
- 1017. The Best Peak Shape (35)
- 1017. The Best Peak Shape (35)
- pat-top 1017. The Best Peak Shape (35)
- 【PAT】1017. The Best Peak Shape
- PAT (Top Level) Practise 1017 The Best Peak Shape (35)
- Find Peak Element解题报告
- HDU 2680 Choose the best route 解题报告
- HDU2680 Choose the best route 解题报告--Dijkstra
- HDOJ-2680-Choose the best route 解题报告
- hdu 2680 Choose the best route 最短路 解题报告
- hdu 3565 Bi-peak Number解题报告
- [leetcode] 162. Find Peak Element 解题报告
- [Leetcode] 162. Find Peak Element 解题报告
- [leetcode] 296. Best Meeting Point 解题报告
- [Leetcode] 296. Best Meeting Point 解题报告
- USACO :The Castle解题报告
- HDOJ_1709 The Balance 解题报告
- vue入门test
- 计算给定数组 arr 中所有元素的总和
- [Hdu 4280]Island Transport
- github中git push origin master出错:error: failed to push some refs to
- javascript 高级程序设计 笔记 (2)
- 1017. The Best Peak Shape (35)解题报告
- jquery阻止冒泡小例子
- 看似很简单的一道题 (openjudge 1.4 16题)
- KindEditor中上传图片不回显的问题
- 股票大数据分析软件V2.7
- Server Tomcat v7.0 Server at localhost failed to start.解决方案
- think-queue消息队列初体验
- 采用jquery实现简单的计算器
- EasyRMS 部署使用文档