ural 1346. Intervals of Monotonicity
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
Tags: dynamic programming
Difficulty: 358
题意:问一个下标从a到b的数组,它分成若干个不降序列,不升序列的最小划分数
分析:
DP啊。。。有什么特别的吗》
Up[i]表示到i结尾的不降序列
Down[i]表示到i的不升序列
转移就显然了
1 /** 2 Create By yzx - stupidboy 3 */ 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <cmath> 8 #include <deque> 9 #include <vector> 10 #include <queue> 11 #include <iostream> 12 #include <algorithm> 13 #include <map> 14 #include <set> 15 #include <ctime> 16 #include <iomanip> 17 using namespace std; 18 typedef long long LL; 19 typedef double DB; 20 #define For(i, s, t) for(int i = (s); i <= (t); i++) 21 #define Ford(i, s, t) for(int i = (s); i >= (t); i--) 22 #define Rep(i, t) for(int i = (0); i < (t); i++) 23 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--) 24 #define rep(i, x, t) for(int i = (x); i < (t); i++) 25 #define MIT (2147483647) 26 #define INF (1000000001) 27 #define MLL (1000000000000000001LL) 28 #define sz(x) ((int) (x).size()) 29 #define clr(x, y) memset(x, y, sizeof(x)) 30 #define puf push_front 31 #define pub push_back 32 #define pof pop_front 33 #define pob pop_back 34 #define ft first 35 #define sd second 36 #define mk make_pair 37 inline void SetIO(string Name) 38 { 39 string Input = Name+".in", 40 Output = Name+".out"; 41 freopen(Input.c_str(), "r", stdin), 42 freopen(Output.c_str(), "w", stdout); 43 } 44 45 46 inline int Getint() 47 { 48 int Ret = 0; 49 char Ch = ' '; 50 bool Flag = 0; 51 while(!(Ch >= '0' && Ch <= '9')) 52 { 53 if(Ch == '-') Flag ^= 1; 54 Ch = getchar(); 55 } 56 while(Ch >= '0' && Ch <= '9') 57 { 58 Ret = Ret * 10 + Ch - '0'; 59 Ch = getchar(); 60 } 61 return Flag ? -Ret : Ret; 62 } 63 64 const int N = 100010; 65 int a, b, Arr[N]; 66 int Up[N], Down[N]; 67 68 inline void Input() 69 { 70 scanf("%d%d", &a, &b); 71 For(i, a, b) scanf("%d", Arr + i); 72 } 73 74 inline void Solve() 75 { 76 Up[a] = Down[a] = 1; 77 For(i, a + 1, b) 78 { 79 if(Arr[i] > Arr[i - 1]) 80 { 81 Up[i] = min(Up[i - 1], Down[i - 1] + 1); 82 Down[i] = min(Up[i - 1] + 1, Down[i - 1] + 1); 83 } 84 else if(Arr[i] < Arr[i - 1]) 85 { 86 Down[i] = min(Down[i - 1], Up[i - 1] + 1); 87 Up[i] = min(Up[i - 1] + 1, Down[i - 1] + 1); 88 } 89 else 90 { 91 Up[i] = min(Up[i - 1], Down[i - 1] + 1); 92 Down[i] = min(Down[i - 1], Up[i - 1] + 1); 93 } 94 } 95 96 int Ans = min(Up[b], Down[b]); 97 printf("%d\n", Ans); 98 } 99 100 int main() 101 { 102 #ifndef ONLINE_JUDGE 103 SetIO("I"); 104 #endif 105 Input(); 106 Solve(); 107 return 0; 108 }

浙公网安备 33010602011771号