[COI2007] Patrik 音乐会的等待 单调栈
2024-09-07 22:09:37
Code:
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cstring>
#include<stack>
#include<string>
using namespace std;
void setIO(string a){ freopen((a+".in").c_str(),"r",stdin),freopen((a+".out").c_str(),"w",stdout); }
void shutIO(){fclose(stdin),fclose(stdout);} #define maxn 500009
int val[maxn];
struct Node{
int val,h;
Node(int val=0,int h=0):val(val),h(h){}
};
stack<Node>S;
int main(){
//setIO("wait");
int n,a;
long long ans=0;
scanf("%d",&n);
for(int i=1;i<=n;++i) scanf("%d",&val[i]);
S.push(Node(1,val[1]));
for(int i=2;i<=n;++i){
a=0;
while(!S.empty()&&S.top().h<val[i])ans+=S.top().val,S.pop();
if(!S.empty()&&S.top().h==val[i]) a+=S.top().val,S.pop();
ans+=a;
ans+=(!S.empty());
S.push(Node(a+1,val[i]));
}
printf("%lld",ans);
//shutIO();
return 0;
}
最新文章
- 常用的SQL语句
- Oracle基础知识(一)、简介与安装
- Java 正则表达式匹配模式[贪婪型、勉强型、占有型]
- MVVM中数据验证之 ViewModel vs. Model
- Linux 网络编程详解五(TCP/IP协议粘包解决方案二)
- Android的启动模式(上)
- tengine+lua的安装步骤
- python学习好书推荐
- hibernate 中id生成策略
- [模拟]ZOJ3485	Identification Number
- HDU-1874 畅通工程续 (最短路径启蒙题)
- uva 10934 Dropping water balloons(转载)
- AVFoundation自定义拍照
- 记小白的一次基于vue+express+mongodb个人站开发
- JVM(五)垃圾回收器的前世今生
- css预编译语言sass——mixin的使用
- VirtualBox查看虚拟机IP地址
- 物联网架构成长之路(23)-Docker练习之Elasticsearch服务搭建
- python结合redis模拟队列
- linux下转换windows文件格式为unix sed -i &#39;s/\r//&#39; <;filename>; 转化为unix格式
热门文章
- pydev 安装
- NUTCH2.3 hadoop2.7.1 hbase1.0.1.1 solr5.2.1部署(一)
- 12.红黑树set
- ajax中Post和Get请求方式的区别?
- SpringMVC后台使用对象接受参数字符串转日期
- vue中指令写了一个demo
- Benelux Algorithm Programming Contest 2014 Final(第二场)
- php函数: call_user_func()和call_user_func_array() 使用详解
- CentOS 7.4 ifconfig, ip/ss, nmcli, nmtui, 配置文件 修改ip信息用法
- php八大设计模式之装饰器模式