D e s c r i p t i o n Description Description
给定一个长度为 n n n的数列A ,要求划分最少的段数,使得每一段要么单调不降,要么单调不升。
I n p u t Input Input
第一行一个整数
n
n
n。
接下来
n
n
n个数表示数列A。
O u t p u t Output Output
输出最少的划分数。
S a m p l e Sample Sample $Input$1
6
1 2 3 2 2 1
S a m p l e Sample Sample O u t p u t Output Output#1
2
S a m p l e Sample Sample I n p u t Input Input#2
9
1 2 1 2 1 2 1 2 1
S a m p l e Sample Sample O u t p u t Output Output#2
5
S a m p l e Sample Sample I n p u t Input Input#3
7
1 2 3 2 1 999999999 1000000000
S a m p l e Sample Sample O u t p u t Output Output#3
3
H i n t Hint Hint
对于
30
%
30\%
30%的数据
1
≤
n
≤
10
1 \leq n \leq 10
1≤n≤10;
对于
60
%
60\%
60%的数据
1
≤
n
≤
1000
1 \leq n \leq 1000
1≤n≤1000;
对于
80
%
80\%
80%的数据,保证数据随机生成;
对于
100
%
100\%
100%的数据
1
≤
n
≤
100000
1 \leq n \leq 100000
1≤n≤100000, 在
i
n
t
int
int范围内。
T r a i n Train Train o f of of T h o u g h t Thought Thought
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#define ll long long
using namespace std;
ll A[100250];
ll n;
int main()
{
scanf("%lld", &n);
for(int i = 1; i <= n; ++i)
scanf("%lld", &A[i]);
int t = 2, l = 1, Ans = 0;
if(A[1] > A[2])l = -1;
while(++t <= n)
{
if(l != 1 && A[t] >= A[t - 1]
|| l == 1 && A[t] <= A[t - 1])
{
Ans++;
while(A[t + 1] == A[t])t++;
if(A[t + 1] > A[t])l = 1;
else l = -1;
}
}
printf("%lld", Ans + 1);
return 0;
}