poj 3250
来源:互联网 发布:杀人软件下载 编辑:程序博客网 时间:2024/06/16 22:24
题目大意:
输入n,表示有n头牛,然后每头牛只能看到他右边比他矮的牛的头发(一样高也不行,因为这个wa了一发),一旦有牛比他高或者和他一样高,此后的牛的头发他就看不见了,求每头牛能看到的其他牛的头发的总和;
基本思路:
单调栈;
代码如下:
#include<iostream>
#include<iomanip>
#include<algorithm>
#include<string>
#include<queue>
#include<vector>
#include<list>
#include<stack>
#include<map>
#include<set>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<cstdlib>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 80000+10;
typedef long long int ll;
ll ans[maxn],r[maxn];
int main()
{
int n;
cin>>n;
for(int i=1;i<=n;i++) scanf("%I64d",&ans[i]);
stack<int> s;
for(int i=n;i>=1;i--)
{
while(!s.empty()&&ans[s.top()]<ans[i]) s.pop();
if(s.empty()) r[i]=n;
else r[i]=s.top()-1;
s.push(i);
}
ll sum=0;
for(int i=1;i<=n;i++) sum+=(r[i]-i);
printf("%I64d\n",sum);
return 0;
}
- POJ 3250
- POj 3250
- poj 3250
- 【POJ】3250
- 【单调栈】POJ 3250
- POJ 3250 单调堆栈
- POJ 3250 单调栈
- poj 3250 Floyd求负圈
- POJ 3250 hair cut
- poj 3250 单调栈
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- 说话之道2
- [LeetCode] 40. Combination Sum II
- 观看顾客户口与看一看预亏路路
- angular的一个小问题
- 配置中文分析器
- poj 3250
- jsp访问servlet
- (1)冒泡法排序代码(另一个应用是找到第K大的数)
- 卡特兰数公式推导
- BP神经网络模型及梯度下降法
- 学 Win32 汇编[28]
- CSS中的多种选择器和它们的优先级
- 关于mybatis生成插件Generator配置文件中自定义注释
- IO端口和IO内存访问