AtCoder Grand Contest 013 A-Sorted Arrays ( 贪心
来源:互联网 发布:淘宝店铺上货采集软件 编辑:程序博客网 时间:2024/06/07 06:49
Description
You are given an array A of length N. Your task is to divide it into several contiguous subarrays. Here, all subarrays obtained must be sorted in either non-decreasing or non-increasing order. At least how many subarrays do you need to divide A into?
Input
Input is given from Standard Input in the following format:
NA1 A2 … AN
Output
Print the minimum possible number of subarrays after division of A.
Sample Input
61 2 3 2 2 1
Sample Output
2
One optimal solution is to divide the array into [1,2,3] and [2,2,1].
Hint
1≤N≤105
1≤Ai≤109
Each Ai is an integer.
题意
找出一个数组内 非递减数列,非递增数列 一个有几个;
题解:
明显的贪心思想
蒟蒻在多开个数组进行标记
AC代码
#include <bits/stdc++.h>using namespace std;#define INF 0x3f3f3f3fint n, arr[N], dp[N];int main(){ int n; scanf("%d",&n); for(int i = 0;i < n; i++) cin>>arr[i]; int ans, res; for(int i = 0;i < n; i++) { dp[i+1] = INF; if(i>=1 && arr[i-1]>arr[i]) ans = i; else dp[i+1] = dp[ans]+1; if(i>=1 && arr[i-1]<arr[i]) res = i; else dp[i+1] = min(dp[i+1],dp[res]+1); } cout<<dp[n]<<endl; return 0;}
阅读全文
1 0
- AtCoder Grand Contest 013 A-Sorted Arrays ( 贪心
- AtCoder Grand Contest 011 A\B 贪心、二分
- AtCoder Grand Contest 012 A
- AtCoder Grand Contest 017-A
- AtCoder Grand Contest 018 A
- atcoder grand Contest 018 A
- AtCoder Grand Contest 018 A
- Atcoder Grand Contest 013C
- Atcoder Grand Contest 013D
- Atcoder Grand Contest 013E
- AtCoder Grand Contest 013 题解
- AtCoder Grand Contest 017-A Biscuits
- Atcoder Grand Contest 013D piling Up
- AtCoder Grand Contest 010
- AtCoder Grand Contest 011
- AtCoder Grand Contest 018
- AtCoder Grand Contest 018
- Atcoder Grand Contest 019
- [Python]使用Numpy对矩阵进行转置
- Android就业面试技巧系列-技术篇5 (WebView和JS交互)
- 关于相机标定的一些资料
- 限制input输入类型
- Android线程—火车售票Demo
- AtCoder Grand Contest 013 A-Sorted Arrays ( 贪心
- 三大文本处理命令之一SED
- Spark Streaming场景应用-Kafka数据读取方式
- Java 多线程断点下载实现思路
- 多线程查询
- 【Shader实战篇】四大渲染模式之镂空实现格子窗户
- 微信小程序 自动换行
- HDU4850 Wow! Such String! —— 字符串构造
- Android就业面试技巧系列-技术篇6 (Handler机制)