您的位置:首页 > 编程语言 > PHP开发

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: