码迷,mamicode.com
首页 > 其他好文 > 详细

luogu P1317 低洼地

时间:2017-10-20 21:44:44      阅读:374      评论:0      收藏:0      [点我收藏+]

标签:mes   tick   art   blog   技术   code   变化   直线   clu   

 

题目描述

一组数,分别表示地平线的高度变化。高度值为整数,相邻高度用直线连接。找出并统计有多少个可能积水的低洼地?

如图:地高变化为 0 1 0 2 1 2 0 0 2 0

技术分享

输入输出格式

输入格式:

 

两行,第一行n,表示有n个数。第2行连续n个数表示地平线高度变化的数据,保证首尾为0。(3<=n<=10000,0<=高度<=1000)

 

输出格式:

 

一个数,可能积水低洼地的数目。

 

输入输出样例

输入样例#1:
10
0 1 0 2 1 2 0 0 2 0
输出样例#1:
3
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>

using namespace std;
const int N = 1e4 + 10;

int n, a[N];

inline int read()
{
    int x = 0, f = 1; char c = getchar();
    while(c < 0 || c > 9){if(c == -)f = -1;c = getchar();} 
    while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar();
    return x * f;
}

int main()
{
    memset(a, -1, sizeof(a));
    n = read();
    for(int i = 1; i <= n; i ++)
        a[i] = read();
    int answer = 0;
    for(int i = 1; i <= n - 2; i ++)
    {
        if(a[i] > a[i + 1] && a[i + 2] > a[i + 1]) answer ++; 
        if(a[i + 1] == a[i + 2] && a[i + 1] < a[i] && i <= n - 2)
        {
            int j = i + 2;
            while(a[j] == a[j - 1]) j ++;
            if(a[j] > a[j - 1]) answer ++;
            i = j - 2;
        }
    }
    printf("%d", answer);
    return 0;
}

/*
11
10 9 8 8 8 7 8 2 1 1 2
*/

 

luogu P1317 低洼地

标签:mes   tick   art   blog   技术   code   变化   直线   clu   

原文地址:http://www.cnblogs.com/lyqlyq/p/7701214.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!