gdutProblem E: 积木积水
2016-04-10 22:07
274 查看
链接:http://gdutcode.sinaapp.com/problem.php?cid=1039&pid=4
题意:中文题。
分析:很显然想要积水,要组成一个凹形。那么我们只要维护一个单调递减的栈,然后每来一个右边界我们就去夹出一个凹形。
代码:
#include<map> #include<set> #include<cmath> #include<queue> #include<bitset> #include<math.h> #include<cstdio> #include<vector> #include<string> #include<cstring> #include<iostream> #include<algorithm> #pragma comment(linker, "/STACK:102400000,102400000") using namespace std; const int N=1000100; const int MAX=100000000; const int mod=100000000; const int MOD1=1000000007; const int MOD2=1000000009; const double EPS=0.00000001; typedef long long ll; const ll MOD=998244353; const ll INF=10000000010; typedef double db; typedef unsigned long long ull; int a ,b ; int main() { int i,n,t,k,x; ll ans; scanf("%d", &t); while (t--) { scanf("%d", &n); ans=k=a[0]=b[0]=0; for (i=1;i<=n;i++) { scanf("%d", &x); if (a[k]==x) { k++;a[k]=x;b[k]=i;continue ; } if (x>a[k]) { while (k>1&&x>a[k]) { ans+=(ll)(min(x,a[k-1])-a[k])*(i-b[k-1]-1); k--; } if (k>0&&x>a[k]) k--; ++k;a[k]=x;b[k]=i; } else { k++;a[k]=x;b[k]=i; } } printf("%lld\n", ans); } return 0; }
相关文章推荐
- php 全局变量
- 接触php遇到的小问题
- phpMyAdmin-Error:The mbstring extension is missing. Please check your PHP configuration.
- php优势和技术应用
- PHP的特性包括 PHP:
- php中页面间传带+的数据出现空格问题
- Atitit.跨语言异常转换机制 java c# php到js的异常转换
- Atitit.跨语言异常转换机制 java c# php到js的异常转换
- Atitit.跨语言异常转换机制 java c# php到js的异常转换
- php mysql ajax 单表多字段多关键词查询
- 020.php
- 019.php函数变量
- yii 查询构建器
- 018.php函数
- 016.php循环do-while、for语句
- 015.php循环while
- 014.php分支语句switch
- 013.php流程控制语句
- 012.php运算符的短路
- 011.php连接运算符、三元表达式