【NOIP2013提高组】花匠
2016-09-03 10:46
393 查看
题目很明显是要找一个锯齿形的序列。对于连续下降或上升的子序列,只能留下其中任意一个才能形成,于是可以直接找拐点,拐点个数+1就是最长锯齿序列的长度。
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> #include<algorithm> #include<cctype> #include<vector> using namespace std; int n,a[100005]={0}; void read(int &x) { x=0; bool ok=0; char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') { ch=getchar(); } while((ch>='0'&&ch<='9')||ch=='-') { if(ch=='-') ok=1; else x=x*10+ch-'0'; ch=getchar(); } if(ok) x=-x; } void in() { read(n); for(int i=1;i<=n;i++) read(a[i]); } void task() { int ans1=1,ans2=1; bool ok=0; for(int i=2;i<=n;i++) { if(!ok&&a[i]>a[i-1]) { ans1++; ok=1; } if(ok&&a[i]<a[i-1]) { ans1++; ok=0; } } ok=1; for(int i=2;i<=n;i++) { if(!ok&&a[i]>a[i-1]) { ans2++; ok=1; } if(ok&&a[i]<a[i-1]) { ans2++; ok=0; } } printf("%d",max(ans1,ans2)); } int main() { freopen("in.txt","r",stdin); in(); task(); return 0; }
相关文章推荐
- NOIP 2013 提高组 花匠
- 2013 NOIP提高组 花匠
- Noip2013提高组day2 花匠
- luogu1970 花匠(NOIP2013提高组第5题)
- 【NOIP2013提高组T5】花匠-O(n)横扫做法
- [NOIP2013] 提高组 洛谷P1970 花匠
- noip2013提高组 花匠
- 【NOIP】2013提高组 花匠(摆花)
- Luogu P1970 [NOIp提高组2013]花匠
- 【NOIP2013提高组】花匠
- 洛谷P1970 [NOIP2013提高组Day2T2] 花匠
- NOIP2013复赛提高组day2(A:积木大赛 B:花匠 C:华容道)
- 【NOIP2013提高组】花匠
- noip2013 花匠 (动态规划求最长抖动序列)
- 刷过一题之NOIP2013花匠
- NOIP 2013 提高组 Day1 T2 火柴排队【逆序对】
- 【华容道】题解(NOIP2013提高组day2)
- NOIP 2013 提高组复赛 day1 试题 Vigenere密码
- [题解+总结]NOIP2013-2014提高组题目浅析
- [noip2013]花匠 题解