1346. Intervals of Monotonicity
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
It’s well known that a domain of any continuous function may be divided into intervals where the function would increase monotonically or decrease monotonically. A number of intervals of such a partition we will call a
complexity of the partition. A
complexity of a continuous function is the minimal possible complexity of partition in the domain into the monotonicity intervals.
The notion of complexity may be defined not only for continuous functions. In particular, it is applicable to the functions specified on a grid.
Input
The input contains a description of a function F, specified on a grid. The first line contains two numbers A and B — the first and the last point of the integer grid with step 1 (0 ≤ A < B ≤ 100 000). The second line contains the values table of the function F. The table consists of the integers F(A), F(A+1), …, F(B) separated with a space and/or linefeeds. All the values of the function F are in diapason from –100 000 to 100 000.
Output
Output the only number — the complexity of the function F.
Sample
input | output |
---|---|
1 10 1 2 3 4 2 1 -1 3 6 7 | 3 |
Problem Author: Alexander Klepinin
Problem Source: USU Championship 2004
Problem Source: USU Championship 2004
Difficulty: 320
Printable version
Submit solution
Discussion (26)
All submissions (7316) All accepted submissions (1768) Solutions rating (1372)
题目给定一个数组,作为函数值,求这个离散的函数的单调区间,这里注意严格单调,把相等的要去掉。。。
递推公式就很好理解啦。。。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn = 1e5+7;
int a, b, n, tot = 0, f[maxn], dp[maxn];
int main()
{
//freopen("in.txt", "r", stdin);
while(~scanf("%d%d", &a, &b))
{
tot = 0;
n = b - a + 1;
f[0] = -1000002;
for(int i = 1; i <= n; i++)
{
int x;
scanf("%d", &x);
if(x != f[tot]) f[++tot] = x;
}
memset(dp, 0, sizeof(dp));
dp[1] = dp[2] = 1;
for(int i = 3; i <= tot;)
{
if((f[i]>=f[i - 1]&&f[i - 1]>=f[i - 2])||(f[i]<=f[i - 1]&&f[i - 1] <= f[i - 2]))
{
dp[i] = dp[i - 1];
i++;
} else {
dp[i] = dp[i - 1] + 1;
dp[i+1] = dp[i];
i+=2;
}
}
printf("%d\n", dp[tot]);
}
return 0;
}