Timus 1346
简单贪心,贪心原则是将每一个单调序列尽可能的大。
#include<iostream> #include<cstdio> #include<cstring> using namespace std; int data[100001]; int work(int m,int n) { int head=m+1,count=0; if(n-m<=1) return 1; while(head<=n) { count++; while(data[head]==data[head-1]) head++; if(data[head]>data[head-1]) { while(head <=n && data[head] >= data[head-1]) head++; if(head==n) return count+1; head++; continue; } else { while(head <=n && data[head] <= data[head-1]) head++; if(head==n) return count+1; head++; continue; } } return count; } int main() { int i,m,n; while(scanf("%d %d",&m,&n)!=EOF) { for(i=m;i<=n;i++) scanf("%d",&data[i]); printf("%d\n",work(m,n)); } return 0; }