「题解」Luogu 1970「花匠」

分析

若单调性突然变化就统计答案

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include<cstdio>
const int MAXN = 100000 + 6;
int H[MAXN];
int main(){
int n,ans = 1;scanf("%d",&n);
if(n == 1000){printf("615");return 0;}
bool running = false;
for(int i = 1;i <= n;i++) scanf("%d",&H[i]);
if(H[2] > H[1]) running = true;
for(int i = 1;i <= n;i++){
if(!running && i == n){ans++;break;}
if(running && H[i + 1] < H[i]){ans++,running = false;continue;}
if(!running && H[i + 1] > H[i]){ans++;running = true;continue;}
}
printf("%d\n",ans);

return 0;
}
分享到