• COdevs 1531 山峰


    1531 山峰

     时间限制: 1 s    空间限制: 128000 KB    题目等级 : 钻石 Diamond
    题目描述 Description

    Rocky山脉有n个山峰,一字排开,从西向东依次编号为1, 2, 3, ……, n。每个山峰的高度都是不一样的。编号为i的山峰高度为hi。

    小修从西往东登山。每到一座山峰,她就回头观望自己走过的艰辛历程。在第i座山峰,她记录下自己回头能看到的山峰数si。

    何谓“能看到”?如果在第i座山峰,存在j<k<i,hj<hk,那么第j座山峰就是不可见的。除了不可见的山峰,其余的山峰都是可见的。

    回家之后,小修把所有的si加起来得到S作为她此次旅行快乐值。现在n座山峰的高度都提供给你了,你能计算出小修的快乐值吗?

    输入描述 Input Description

    第一行一个整数n(n<=15000)。

    第i+1(1<=i<=n)行是一个整数hi(hi<=109)。

    输出描述 Output Description

    仅一行:快乐值。

    样例输入 Sample Input

    5

    2

    1

    3

    5

    9

    样例输出 Sample Output

    5

    数据范围及提示 Data Size & Hint

    说明:s1=0, s2=1, s3=2, s4=1, s5=1。

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<stack>
     5 using namespace std;
     6 int n,a[15050],ans,s[15009],top=1;
     7 int main()
     8 {
     9     scanf("%d",&n);
    10     for(int i=1;i<=n;i++)
    11         scanf("%d",&a[i]);
    12     s[1]=a[1];
    13     for(int i=2;i<=n;i++)
    14     {
    15         ans+=top;
    16         if(s[top]>a[i]) s[++top]=a[i];
    17         else {
    18             while(s[top]<=a[i]&&top>=1) 
    19             {
    20                 top--;
    21             }
    22             s[++top]=a[i];
    23         }
    24     }
    25     printf("%d",ans);
    26     return 0;
    27 }

    本来用STL写的,退栈的时候老出错,手敲单调栈~~~

  • 相关阅读:
    saltstack学习
    linux命令学习(2):wc 命令
    linux命令学习(1):grep 命令
    Docker Compose—简化复杂容器应用的利器
    Docker-compose命令详解
    Python json数据写入csv json excel文件
    Centos 不重启 修改ulimit参数
    pip包管理工具 基本使用
    python 使用xpath解析含有命名空间(xmlns)的xml
    pyqt5 + pyinstaller 制作爬虫小程序
  • 原文地址:https://www.cnblogs.com/suishiguang/p/6216847.html
Copyright © 2020-2023  润新知