Description


Input

* Line 1: 牛的数量 N。

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

Output

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

题解:

BZOJ服务器好像有问题,图片很不好显示,看上面的就好了。

设牛i看到的最远位置是see[i]。

从前向后扫,求see值,设前面有一头牛A,后面有一头牛B,如果h[B]>=h[A],那么A这头牛就永远不可能成为B牛后面的牛的see值。

可以用单调栈维护这个单调性,

从前向后扫,当栈顶牛高度小于当前牛高度时退栈,之道栈顶牛高度>=当前牛高度,那么这个栈顶牛就是当前牛的see值,将当前牛入栈。

ans=∑(see[i]-i-1)。

代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
//by zrt
//problem:
using namespace std;
const int MAXN=80005;
struct N{
int x,w;
N(int a=0,int b=0){
x=a,w=b;
}
};
N stk[MAXN];
int top;
int see[MAXN];
int n;
int h[MAXN];
int inf=1<<30;
typedef long long LL;
int main(){
#ifdef LOCAL
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%d",&h[i]);
}
stk[top++]=N(n+1,inf);
for(int i=n;i>=1;i--){
while(stk[top-1].w<h[i]) top--;
see[i]=stk[top-1].x;
stk[top++]=N(i,h[i]);
}
LL ans=0;
for(int i=1;i<=n;i++){
ans+=see[i]-i-1;
}
printf("%lld\n",ans);
return 0;
}

BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节的更多相关文章

  1. BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )

    维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...

  2. BZOJ 1660 [Usaco2006 Nov]Bad Hair Day 乱发节:单调栈

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1660 题意: 有n头牛,身高分别为h[i]. 它们排成一排,面向右边.第i头牛可以看见在它 ...

  3. bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节【单调栈】

    开一个单调递减的单调栈,然后用sum数组维护每个点的答案,新加点的时候一边退栈一边把退掉的点的sum加进来 #include<iostream> #include<cstdio> ...

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

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 665  Solved: 31 ...

  5. 【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1660 单调栈裸题..累计比每一个点高的个数即可. #include <cstdio> # ...

  6. 【BZOJ 1660】 [Usaco2006 Nov]Bad Hair Day 乱发节

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec  Memory Limit: 64 MB Submit: 678  Solved: 32 ...

  7. BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节

    1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 606  Solved: 289 ...

  8. BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)

    题意 题目链接 Sol 单调栈板子题.. 找到向左第一个比他大的位置,然后判断一下就可以了 #include<bits/stdc++.h> //#define int long long ...

  9. [Usaco2006 Nov]Bad Hair Day 乱发节

    Time Limit: 2 Sec  Memory Limit: 64 MBSubmit: 1268  Solved: 625[Submit][Status][Discuss] Description ...

随机推荐

  1. p

    都不知道简历去投什么地方.游戏都卖不出去,又做不出口碑好的.这些人是心存侥幸还是心存坚持. 感觉自己搞不清楚就很难再出发.

  2. 安卓UI美化_drawable

    UI美化 drawable资源 ------------------------------------------state drawable资源在不同状态下显示不同的图片在<selector ...

  3. 移动终端学习1:css3 Media Queries简介

    移动终端学习之1:css3 Media Queries简介 1.简介 这篇文章写的不错,我就不重复了,来个链接:http://www.w3cplus.com/content/css3-media-qu ...

  4. Vim常见快捷键汇总

    编辑命令: i 词前插入 a 词后插入 I 行首插入 A 行尾插入 o 新建一行编辑 O 在上面新建一行 插入: 10 i * [ESC] 插入10个* 25 a xx [ESC] 插入25个xx J ...

  5. C#获取当前路径,获取当前路径的上一层路径

    C#获取当前路径的方法如下: (1)string path1 = System.Environment.CurrentDirectory; //C:\...\bin\Debug -获取和设置当前工作目 ...

  6. cognos 10.2.2 report studio数字---字符型查询注意事项

    做了一个简单的报表,就是按照员工编号查询员工,其中员工编号是全数字,我们保存在数据库中的是字符型varchar2(10),所以在report studio中做查询就一直报告服务器错误. 其中使用cas ...

  7. 理解RunLoop

    一.什么是RunLoop? 从字面意思上来看,RunLoop就是运行循环,跑圈的意思. 我们都知道,一般来说一个线程执行一次任务之后便会退出,在iOS中,如果主线程只执行一次便退出的话也就意味着程序的 ...

  8. swift-06-字符串,字符以及元组类型

    1.字符串和字符类型 //在swift中,字符串使用一对双引号括起来 var str = "hello M.SD-DJ" print(str) //字符也要用双引号括起来,用cha ...

  9. javascript 函数学习

    1.自以为好的部分,更多访问: http://www.runoob.com/js/js-tutorial.html 2.this 3.new 4.闭包 5.自执行

  10. 01_反射_04_反射类的main方法

    [User.java] package com.Higgin.reflect; public class User { public User(){ System.out.println(" ...