0x11 栈

【例题】Push,Pop,GetMin

手写一个栈

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 using namespace std;
 8 const int maxn=1000000;
 9 int stack[maxn], m[maxn], top=0, m_top=0;
10 int n;
11 
12 void push(int x) {
13     stack[++top]=x;
14     int tmp=m[m_top];
15     m[++m_top]=min(x, tmp);
16 }
17 
18 void pop() {
19     stack[top]=m[m_top]=0;
20     --top, --m_top;
21 }
22 
23 int main() {
24     int x;
25     char ops[3];
26     m[0]=0x3f3f3f3f;
27     scanf("%d", &n);
28     for (int i=1; i<=n; ++i) {
29         scanf("%s %d", ops, &x);
30         if (ops[0]=='p') push(x);
31         else if(ops[0]=='q') pop();
32         printf("The current minimum number: %d\n", m[m_top]);
33     }
34     return 0;
35 }
View Code

【例题】HDOJ4699 Editor

考虑维护一个对顶栈,一开始用STL里的栈过不了

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 using namespace std;
 9 const int maxn=1000000+10;
10 const int INF=0x3f3f3f3f;
11 int f[maxn], sum[maxn];
12 //stack<int> A, B;
13 int A[maxn], B[maxn], tpA, tpB;
14 
15 int main() {
16     //freopen("a.txt", "r", stdin);
17     int n;
18     while (~scanf("%d", &n)) {
19     char ops[3]; int t;
20     sum[0]=0, f[0]=-INF, tpA=0, tpB=0;
21     while (n--) {
22         scanf("%s", ops);
23         if (ops[0]=='I') {
24             scanf("%d", &t);
25             /*A.push(t);
26             int p=A.size();
27             sum[p]=sum[p-1]+t;
28             f[p]=max(f[p-1], sum[p]);*/
29             A[++tpA]=t;
30             sum[tpA]=sum[tpA-1]+t;
31             f[tpA]=max(f[tpA-1], sum[tpA]);
32         }
33         if (ops[0]=='D') {
34             //A.pop();
35             --tpA;
36         }
37         if (ops[0]=='L') {
38             /*if (!A.empty()) {
39                 int tmp=A.top();
40                 A.pop();
41                 B.push(tmp);
42             }*/
43             if (tpA) {
44                 int x=A[tpA];
45                 --tpA;
46                 B[++tpB]=x;
47             }
48         }
49         if (ops[0]=='R') {
50             /*if (!B.empty()) {
51                 int tmp=B.top();
52                 B.pop();
53                 A.push(tmp);
54                 int p=A.size();
55                 sum[p]=sum[p-1]+tmp;
56                 f[p]=max(f[p-1], sum[p]);
57             }*/
58             if (tpB) {
59                 int x=B[tpB];
60                 --tpB;
61                 A[++tpA]=x;
62                 sum[tpA]=sum[tpA-1]+x;
63                 f[tpA]=max(f[tpA-1], sum[tpA]);
64             }
65         }
66         if (ops[0]=='Q') {
67             scanf("%d", &t);
68             printf("%d\n", f[t]);
69         }
70     }
71     }
72     return 0;
73 } 

【例题】CH1101/CH1102 进出栈序列问题

方法一:搜索

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 using namespace std;
 9 const int maxn=30;
10 int n, cnt=0, kase=0, ans[maxn]; 
11 int s[maxn], top=0;
12 
13 void dfs(int x) {
14     if (kase>20) return;
15     if (x==n+1) {
16         if (++kase>20) return;
17         for (int i=1; i<=cnt; ++i)
18             printf("%d", ans[i]);
19         for (int i=top; i>0; --i)
20             printf("%d", s[i]);
21         printf("\n");
22         return;
23     }
24     if (top) {
25         ans[++cnt]=s[top--];    //出栈 
26         dfs(x);
27         s[++top]=ans[cnt--];
28     }
29     s[++top]=x;
30     dfs(x+1);
31     --top;
32 }
33 
34 int main() {
35     cin>>n;
36     dfs(1);
37     return 0;
38 } 
View Code

方法二:递推

因为不计算具体的序列,只计算方案数

8分代码,需要高精度,我不会。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 using namespace std;
 9 const int maxn=60000+10;
10 int n;
11 long long s[maxn];
12 
13 
14 int main() {
15     scanf("%d", &n);
16     s[0]=1, s[1]=1, s[2]=2;
17     for (int i=3; i<=n; ++i) {
18         //考虑1这个数排在最终出栈序列的位置
19         for (int j=1; j<=i; ++j)
20             s[i]+=s[j-1]*s[i-j]; 
21     }
22     printf("%lld\n", s[n]);
23     return 0;
24 } 
View Code

方法三:dp

放弃。。。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <stack>
 8 using namespace std;
 9 const int maxn=60000+10;
10 int n;
11 long long f[maxn][maxn];
12 
13 
14 int main() {
15     scanf("%d", &n);
16     f[0][0]=1;
17     for (int i=1; i<=n; ++i) {
18         for (int j=1; j<=n; ++j) {
19             f[i][j]=f[i-1][j]+f[i][j-1];
20         }
21     }
22     printf("%lld\n", f[n][0]);
23     return 0;
24 } 
View Code

 方法四:数学

Catalan数

单调栈

及时排除不可能的选项,保持策略集合的高度有效性和秩序性

【例题】POJ2559 Largest Rectangle in a Histogram

考虑维护一个单调栈,在出栈过程中不断更新答案,可以在an右边添加一个高度为0的矩形,避免栈内有剩余矩形。

#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <queue>
#include <stack>
using namespace std;
const int maxn=100000+10;
const int INF=0x3f3f3f3f;
int n, a[maxn], w[maxn];
int s[maxn], p=0;int main() {while (scanf("%d", &n)&&n) {long long ans=0;for (int i=1; i<=n; ++i)scanf("%d", &a[i]);a[n+1]=p=0;for (int i=1; i<=n+1; ++i) {if (a[i]>s[p]) {s[++p]=a[i], w[p]=1;}else {int width=0;while (s[p]>a[i]) {width+=w[p];ans=max(ans, (long long)width*s[p]);--p;} s[++p]=a[i], w[p]=width+1;}}printf("%lld\n", ans);}return 0;
} 

 

转载于:https://www.cnblogs.com/kkkstra/p/11104795.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/550391.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java war包合并,使用maven warpath插件合并多module的war包

查看原文&#xff1a;http://www.yeetrack.com/?p899现在java项目一般使用maven、gradle等工具来管理jar包、打包、发布。如果一个项目有很多模块&#xff0c;那般是分成多个module&#xff0c;主目录有个parent&#xff0c;负责包含全部的module&#xff0c;然后目录中多个mo…

儿童编程python入门_儿童编程python入门

经常会有小朋友问我&#xff0c;“我想做个黑客&#xff0c;我该学什么编程语言&#xff1f;”&#xff0c;或者有的小朋友会说&#xff1a;“我要学c&#xff0c;我要做病毒”。其实对于这些小朋友而言他们基本都没有接触过编程语言&#xff0c;只是通过影视或者其他地方看到的…

ARM GNU 常用汇编伪指令介绍

abort.abort: 停止汇编 .align absexpr1, absexpr2: 以某种对齐方式,在未使用的存储区域填充值. 第一个值表示对齐方式,4, 8,16 或 32. 第 二个表达式值表示填充的值. if...else...endif.if .else .endif: 支持条件预编译 include.include "file": 包含指定的头文件,…

java 同类型转换失败,你们见过java类型转换,自己转自己失败的情况吗?很神奇的操作...

问题就是上面这个问题。List slaughterProducts slaughterForm.getSlaughterProductModelForm();for (SlaughterProductModelForm e : slaughterProducts) {....}居然运行到for的时候出现上面这个错误。很神奇吧&#xff0c;工作这么多年了第一次发现 JAVA自己转自己转不成功。…

python可视化代码_python可视化实现代码

python可视化#导入两个库import numpy as npimport matplotlib.pyplot as plt#第一个参数就是x轴的初始值#第二个参数是x轴的终止值#第三个返回num均匀分布的样本&#xff0c;也就是0-12的区间取多少个点&#xff0c;如果为曲线的最好数值大一点x np.linspace(0, 12, 50)y np…

用户管理与文件权限

一&#xff1a;用户管理 现代操作系统一般属于多用户的操作系统&#xff0c;也就是说&#xff0c;同一台机器可以为多个用户建立账户&#xff0c;一般这些用户都是为普通用户&#xff0c;这些普通用户能同时登录这台计算机&#xff0c;计算机对这些用户分配一定的资源。 普通用…

php中划线,html中下划线、删除线、上划线的样式与用法实例

这篇文章主要介绍了下划线、删除线、上划线等常用的实例&#xff0c;划线是非常常见的一种样式&#xff0c;为了网页中的视觉效果以及对文字的说明&#xff0c;我们经常对文体进行一些划线操作。下面文章就是对各种划线的详细介绍。一. 下划线的详细介绍在我们日常的Web的开发中…

rabbitmq3.7集群搭建实战

环境&#xff1a; 3台 centos7.4rabbitmq3.7erlang 22 1. 有几种方式安装&#xff0c;这里使用的yum安装&#xff08;官方推荐&#xff09;2. 使用rabbitmq时需要安装erlang&#xff0c;在各个节点上使用vim添加两个repo文件**/etc/yum.repos.d/rabbitmq_erlang.repo**[rabbitm…

php获取页面中的指定内容,php 获取页面中指定内容的实现类

[email protected]image&#xff1a;Grep.class.php/** grep class* Date: 2013-06-15* Author: fdipzone* Ver: 1.0** Func:** set: 设置内容* get: 返回指定的内容* replace: 返回替换后的内容* get_pattern 根据type返回pattern*/class Grep{ // class startprivate $_patte…

数据增量更新定义_TiDB 在 OPPO 准实时数据仓库中的实践

作者介绍OPPO 数据分析与解决方案团队主要负责 OPPO 全集团的大数据分析和解决方案提供&#xff0c;团队成员多来自一线互联网公司及著名高校&#xff0c;在 OPPO 众多场景的大数据应用方面有很深经验&#xff0c;极大的支撑了业务迅速发展。文章具体作者&#xff1a;羊欢&…

Python之模块初识-自定义模块

1. 模块的定义与分类 什么是模块&#xff1f;一个模块就是一个py文件。 模拟博客园系统作业&#xff0c;100000行代码. 不可能全部存在一个文件. 不易维护.效率低.分文件: 10个文件.每个文件有50个函数,有一写相同功能或者相似功能的函数.代码冗余,重复性.我们应该将这10个函数…

php统计用户留存脚本,SQL 统计用户留存

问题描述有一个用来记录每日客户消耗数据的表 t&#xff0c;它的表结构如下&#xff1a;字段类型描述created_dayDate消耗日期customer_idInteger客户IDamountInteger消耗金额要求&#xff1a;统计出头部客户、腰部客户、尾部客户在上个月(2020-06-01 ~ 2020-06-30)的留存情况。…

python socket udp_python网络-Socket之udp编程(24)

一、udp简介udp --- 用户数据报协议&#xff0c;是一个无连接的简单的面向数据报的运输层协议。udp不提供可靠性&#xff0c;它只是把应用程序传给IP层的数据报发送出去&#xff0c;但是并不能保证它们能到达目的地。udp在传输数据报前不用在客户和服务器之间建立一个连接&…

selenium--单选下拉列表

下拉选择 from selenium import webdriver from time import sleepdriver webdriver.Chrome() driver.get("https://www.xxxxx.com/") sleep(2) driver.find_elements_by_tag_name(option)[2].click() # 通过标签名定位到 option 标签&#xff0c;选择第三个&#x…

matlab实现字符识别,字符识别 - MATLAB Simulink Example - MathWorks 中国

定义问题脚本 prprob 定义了一个包含 26 列的矩阵 X&#xff0c;每列对应一个字母。每列有 35 个值&#xff0c;值可能是 1&#xff0c;也可能是 0。每列(包含 35 个值)定义一个字母的 57 位图。矩阵 T 是一个 2626 的单位矩阵&#xff0c;它将 26 个输入向量映射到 26 个类。[…

python建立ip代理池_Python搭建代理IP池实现存储IP的方法

上一文写了如何从代理服务网站提取 IP&#xff0c;本文就讲解如何存储 IP&#xff0c;毕竟代理池还是要有一定量的 IP 数量才行。存储的方式有很多&#xff0c;直接一点的可以放在一个文本文件中&#xff0c;但操作起来不太灵活&#xff0c;而我选择的是 MySQL 数据库&#xff…

低秩矩阵分解 matlab,低秩分解的matlab代码看不懂,分解的两个矩阵在哪呀??...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼有四个文件&#xff1a;demo.mfunction [] demo()%This routine demonstrates an example of using LRR to do subspace segmentation. We cosntruct 5 independent subspaces, each of which has a rank of 10,%sample 200 point…

python深度神经网络算法_02.深度神经网络算法之Python基础与数据分析

资源内容&#xff1a;02.深度神经网络算法之Python基础与数据分析|____2016最新python基础篇视频教程22课|____Python学习手册(第4版).pdf|____Python程序入门与进阶|____基础篇01-福利课python先入为主上篇|____基础篇02-福利课python先入为主下篇|____基础篇03-虚拟机安装xub…

我们为什么要用MVC?ASP.NET MVC模式的优点?

1.关注点分离&#xff1a;每个功能最少会被切分为M-V-C三个部分&#xff0c;让开发者一次只需要关注一个部分&#xff0c;进而降低复杂难度&#xff0c;提高开发效率2.分层负责&#xff1a;明确切割&#xff0c;M-V-C三个部分并行开发3.自由操控HTML&#xff1a;在ASP.Net MVC中…

python 英语词频统计软件_Python实现统计英文文章词频的方法分析

本文实例讲述了Python实现统计英文文章词频的方法。分享给大家供大家参考&#xff0c;具体如下&#xff1a;应用介绍&#xff1a;统计英文文章词频是很常见的需求&#xff0c;本文利用python实现。思路分析&#xff1a;1、把英文文章的每个单词放到列表里&#xff0c;并统计列表…