timus 1346. Intervals of Monotonicity URAL 解题报告
坑死人的小破题,给一个数列,然后判断这个数列的
complexity 是多少,所谓的complexity 就是最少数目的partition ,一个partition是这样定义的: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! 狗日的题意,就是说看最少有几个片段是单调的,如果不增不减的就不算……
例如:
1 5
1 1 1 1 1
答案是1,而不是5,不是0!
明白了这点之后O(n)的做法就出来了
对于每一个起点,往后找他的转折点! 中间相等的不要管他!
或者来由的做法,标记法,交替寻找上升或者下降!
#include<iostream>
#include<cstdio>
#include<cstring>
#define pd(x) printf("%d",(x))
#define ed printf("\n")
#define op(x) cout<<#x<<"="<<(x)<<" "
using namespace std;
const int N=100100;
int a[N];
int A,B;
int ans,tmp1,tmp2;
int n;
int bo;
int main()
{
cin>>A>>B;
n=B-A+1;bo=0; ans=0;
for(int i=1;i<=n;++i)
{
cin>>a[i];
}if(n<=1){cout<<1<<endl;return 0;}
for(int i=1;i<=n;++i)
{///枚举当前节点,看从这点出发到哪里才是结束,
if(i==n)ans++;
//else if(a[i]==a[i+1])ans++;
else if(a[i]<a[i+1])
{
while(1)
{
i++; if(i+1>n){ ans++;break;}
if(a[i]>a[i+1]){ans++; break;}
// if(a[i]==a[i+1]){ans++; break;}
}
}
else if(a[i]>a[i+1])
{
while(1)
{
i++; if(i+1>n){ ans++;break;}
if(a[i]<a[i+1]){ans++; break;}
// if(a[i]==a[i+1]){ans++; break;}
}
}
}
cout<<ans<<endl;
return 0;
}