zoukankan      html  css  js  c++  java
  • [BZOJ1660][Usaco2006 Nov]Bad Hair Day 乱发节

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 1204  Solved: 589 [Submit][Status][Discuss]

    Description

    Input

    * Line 1: 牛的数量 N。

     * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。

    Output

    * Line 1: 一个整数表示c[1] 至 c[N]的和。

    Sample Input

    6
    10
    3
    7
    4
    12
    2


    输入解释:

    六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。

    Sample Output

    5

    3+0+1+0+1=5
     
    单调栈维护一个身高递减序列,注意是从n到1
    #include <cstdio>
    const int maxn = 80000 + 10;
    int h[maxn], sta[maxn], top = 0;
    int main(){
        int n;
        scanf("%d", &n);
        for(int i = 1; i <= n; i++) scanf("%d", h + i);
        h[n + 1] = 1 << 30;
        sta[top = 1] = n + 1;
        long long ans = 0;
        for(int i = n; i; i--){
            while(top && h[i] > h[sta[top]]) top--;
            ans += sta[top] - i - 1;
            sta[++top] = i;
        }
        printf("%lld
    ", ans);
        return 0;
    }
  • 相关阅读:
    MVC基础
    图片水印和图片验证码
    Jquery弹窗
    AJAX基础
    Jquery--动画
    Jquery--动画
    JQuery
    LinkQ 组合查询与分页
    LinQ的简单使用
    JavaScript复习
  • 原文地址:https://www.cnblogs.com/ruoruoruo/p/7491475.html
Copyright © 2011-2022 走看看