【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
http://www.lydsy.com/JudgeOnline/problem.php?id=1660
单调栈裸题。。累计比每一个点高的个数即可。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
#define rep(i, n) for(int i=0; i<(n); ++i)
#define for1(i,a,n) for(int i=(a);i<=(n);++i)
#define for2(i,a,n) for(int i=(a);i<(n);++i)
#define for3(i,a,n) for(int i=(a);i>=(n);--i)
#define for4(i,a,n) for(int i=(a);i>(n);--i)
#define CC(i,a) memset(i,a,sizeof(i))
#define read(a) a=getint()
#define print(a) printf("%d", a)
#define dbg(x) cout << #x << " = " << x << endl
#define printarr(a, n, m) rep(aaa, n) { rep(bbb, m) cout << a[aaa][bbb]; cout << endl; }
inline const int getint() { int r=0, k=1; char c=getchar(); for(; c<'0'||c>'9'; c=getchar()) if(c=='-') k=-1; for(; c>='0'&&c<='9'; c=getchar()) r=r*10+c-'0'; return k*r; }
inline const int max(const int &a, const int &b) { return a>b?a:b; }
inline const int min(const int &a, const int &b) { return a<b?a:b; }
const bool cmp(const int &a, const int &b) { return a>=b; }
const int N=80006;
int s[N], top, h[N], n;
long long ans; void update(int x) {
while(top && h[x]>=s[top]) --top;
ans+=top;
s[++top]=h[x];
} int main() {
read(n);
for1(i, 1, n) read(h[i]);
for1(i, 1, n) update(i);
printf("%lld", ans);
return 0;
}
Description

Input
* Line 1: 牛的数量 N。
* Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度。
Output
* Line 1: 一个整数表示c[1] 至 c[N]的和。
Sample Input
10
3
7
4
12
2
输入解释:
六头牛排成一排,高度依次是 10, 3, 7, 4, 12, 2。
Sample Output
3+0+1+0+1=5
HINT
Source
【BZOJ】1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)的更多相关文章
- BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节( 单调栈 )
维护一个h严格递减的栈 , 出栈时计算一下就好了.. ------------------------------------------------------------------------- ...
- BZOJ 1660: [Usaco2006 Nov]Bad Hair Day 乱发节
Description Input * Line 1: 牛的数量 N. * Lines 2..N+1: 第 i+1 是一个整数,表示第i头牛的高度. Output * Line 1: 一个整数表示c[ ...
- BZOJ 1660 [Usaco2006 Nov]Bad Hair Day 乱发节:单调栈
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1660 题意: 有n头牛,身高分别为h[i]. 它们排成一排,面向右边.第i头牛可以看见在它 ...
- bzoj 1660: [Usaco2006 Nov]Bad Hair Day 乱发节【单调栈】
开一个单调递减的单调栈,然后用sum数组维护每个点的答案,新加点的时候一边退栈一边把退掉的点的sum加进来 #include<iostream> #include<cstdio> ...
- BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节(单调栈)
题意 题目链接 Sol 单调栈板子题.. 找到向左第一个比他大的位置,然后判断一下就可以了 #include<bits/stdc++.h> //#define int long long ...
- 1660: [Usaco2006 Nov]Bad Hair Day 乱发节
1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec Memory Limit: 64 MB Submit: 665 Solved: 31 ...
- 【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 ...
- BZOJ1660: [Usaco2006 Nov]Bad Hair Day 乱发节
1660: [Usaco2006 Nov]Bad Hair Day 乱发节 Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 606 Solved: 289 ...
- [Usaco2006 Nov]Bad Hair Day 乱发节
Time Limit: 2 Sec Memory Limit: 64 MBSubmit: 1268 Solved: 625[Submit][Status][Discuss] Description ...
随机推荐
- PHP-流的概念与详细用法
Stream是PHP开发里最容易被忽视的函数系列(SPL系列,Stream系列,pack函数,封装协议)之一,但其是个很有用也很重要的函数.Stream可以翻译为“流”,在Java里,流是一个很重要的 ...
- 利用ThreadLocal建立高质量事务处理
ThreadLocal此类是一个以当前线程为key的map对象的构想. 当我们在web开发中,多个浏览器访问的时候,servlet为它们各开线程执行相应代码,而事务的执行依赖于特定的一个Connect ...
- Zookeeper研究和应用
http://www.searchtb.com/2011/01/zookeeper-research.html zookeeper简介 zookeeper是一个开源分布式的服务,它提供了分布式协作,分 ...
- PHP安全细节(转)
一个常见的PHP安全细则 发布时间:2012-01-09 10:18:50 来源:51cto 评论:0 点击:9 次 [字号:大 中 小] PHP本身再老版本有一些问题,比如在 php4.3. ...
- jasmine-行为驱动测试
http://jasmine.github.io/1.3/introduction.html,先保留着,好好研究.
- atitit.资源释放机制--attilax总结
atitit.资源释放机制--attilax总结 1. .全手工, 1 2. 引用计数, 1 2.1. 成本也显而易见. 1 2.2. 循环引用的问题, 2 2.3. 引用计数方式事实上也有经典的卡顿 ...
- Leetcode Copy List with Random Pointer(面试题推荐)
给大家推荐一道leetcode上的面试题,这道题的详细解说在<剑指offer>的P149页有思路解说.假设你手头有这本书.建议翻阅. 题目链接 here A linked list is ...
- HDU - 3836 Equivalent Sets (强连通分量+DAG)
题目大意:给出N个点,M条边.要求你加入最少的边,使得这个图变成强连通分量 解题思路:先找出全部的强连通分量和桥,将强连通分量缩点.桥作为连线,就形成了DAG了 这题被坑了.用了G++交的,结果一直R ...
- [k8s]helm原理&私有库搭建&monocularui和kubeapp探究
运行最简单的charts示例 helm的2个chart例子: https://github.com/kubernetes/helm/tree/master/docs/examples/nginx he ...
- CYQ多数据库链接
枚举名XXXEnum 对应XXXConn的配置数据库链接项 ,不多说,一看就明白!名字空间得要带上数据库名!