C. Okabe and Boxes

这个题目是一个有点思维的模拟,当时没有想到,

思维就是这个栈的排序这里,因为每次直接排序肯定会t的,所以不可以这么写,那怎么表示排序呢?

就是直接把栈清空,如果栈顶就是我们需要的这个值,那就把这个值直接pop,

但是如果不是呢,就可以直接清空这个栈表示排序,如果栈已经是空的了,说明之前排过序了。

如果不是那就清空(排序),这个有点难想。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <iostream>
#include <vector>
#include <algorithm>
#include <stack>
#include <string>
#define inf 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = 3e5 + ;
stack<int>sta;
int main()
{
int n, ans = , cnt = ;
scanf("%d", &n);
for(int i=;i<=*n;i++)
{
char s[];
int num;
scanf("%s", s);
if (s[] == 'a') scanf("%d", &num), sta.push(num);
else
{
cnt++;
if (!sta.empty() && sta.top() == cnt) sta.pop();
else {
if (!sta.empty()) ans++;
while (!sta.empty()) sta.pop();
}
}
}
printf("%d\n", ans);
return ;
}

模拟 思维

然后就是lj写的,是用线段树过的,我觉得太厉害了,挺难想到的。

这个用线段树是怎么想的呢,就是首先全部初始化为0,表示一开始就是一个序列,

然后如果有数字入栈,就更新比数字小的那一段,表示那一段的序列是乱的,然后这个数字的序列是正确的,更新为0.

然后如果要remove就判断这个数字是不是为0,如果是为0,那就说明这个数字是栈顶,因为数字肯定是从小到大数的,

虽然之后也有更大的数字为0,但是这个更小的如果是0,则说明是栈顶。

如果这个数字不是0,那就排序就全部更新为0。

具体看代码。

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <queue>
#include <stack>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
#define inf64 0x3f3f3f3f3f3f3f3f
using namespace std;
const int maxn = 3e5 + ;
int sum[maxn * ], lazy[maxn * ]; void push_up(int id) {
sum[id] = sum[id << ] + sum[id << | ];
} void build(int id, int l, int r) {
lazy[id] = -, sum[id] = ;
if (l == r) return;
int mid = (l + r) >> ;
build(id << , l, mid);
build(id << | , mid + , r);
} void push_down(int id) {
if (lazy[id] == -) return;
lazy[id << ] = lazy[id << | ] = lazy[id];
sum[id << ] = sum[id << | ] = lazy[id];
lazy[id] = -;
} void update(int id, int l, int r, int x, int y, int val) {
if (x > r || y < l) return;
if (x <= l && y >= r) {
sum[id] = val;
lazy[id] = val;
return;
}
push_down(id);
int mid = (l + r) >> ;
if (x <= mid) update(id << , l, mid, x, y, val);
if (y > mid) update(id << | , mid + , r, x, y, val);
push_up(id);
} int query(int id, int l, int r, int pos) {
if (l == r) return sum[id];
int mid = (l + r) >> ;
push_down(id);
if (pos <= mid) return query(id << , l, mid, pos);
return query(id << | , mid + , r, pos);
} int main() {
int n, cnt = , ans = ;
scanf("%d", &n);
for (int i = ; i <= * n; i++) {
char s[];
int num;
scanf("%s", s);
if (s[] == 'a') {
scanf("%d", &num);
update(, , n, , num - , );
update(, , n, num, num, );
}
else {
cnt++;
if (query(, , n, cnt) != ) {
ans++;
update(, , n, , n, );
}
}
}
printf("%d\n", ans);
return ;
}

线段树

C. Okabe and Boxes 思维 模拟 or 线段树的更多相关文章

  1. HDU5367 思维map // 动态线段树

    地主毛毛有n座山,这些山在地主家门前排成一条直线.这些山一开始均有相同的高度.  每一天,毛毛都会要求花花开挖机把几座山挖掉一定高度,或者给一些山堆上一些高度.并且要求花花报告现在有多少座山属于“高山 ...

  2. CF821 C. Okabe and Boxes 栈模拟

    Link 题意:给出操作,如果当前出栈操作使得出栈序列非顺序,可以在此之前自由排序栈中所有数,问最少排几次. 思路:已经出栈到第x个元素时,每次需要排序的操作后,能够保证前x元素出栈有序,否则说明该操 ...

  3. Codeforces 916E(思维+dfs序+线段树+LCA)

    题面 传送门 题目大意:给定初始根节点为1的树,有3种操作 1.把根节点更换为r 2.将包含u,v的节点的最小子树(即lca(u,v)的子树)所有节点的值+x 3.查询v及其子树的值之和 分析 看到批 ...

  4. hdu_5818_Joint Stacks(线段树模拟)

    题目链接:hdu_5818_Joint Stacks 题意: 给你两个栈,多了个合并操作,然后让你模拟 题解: 很容易想到O(1)的单个栈操作,O(n)的合并操作,这样肯定超时,所以我们要将时间复杂度 ...

  5. zkw线段树学习笔记

    zkw线段树学习笔记 今天模拟赛线段树被卡常了,由于我自带常数 \(buff\),所以学了下zkw线段树. 平常的线段树无论是修改还是查询,都是从根开始递归找到区间的,而zkw线段树直接从叶子结点开始 ...

  6. dfs+线段树 zhrt的数据结构课

    zhrt的数据结构课 这个题目我觉得是一个有一点点思维的dfs+线段树 虽然说看起来可以用树链剖分写,但是这个题目时间卡了树剖 因为之前用树剖一直在写这个,所以一直想的是区间更新,想dfs+线段树,有 ...

  7. Codeforces821C Okabe and Boxes 2017-06-28 15:24 35人阅读 评论(0) 收藏

    C. Okabe and Boxes time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces 821C - Okabe and Boxes

    821C - Okabe and Boxes 思路:模拟.因为只需要比较栈顶和当前要删除的值就可以了,所以如果栈顶和当前要删除的值不同时,栈就可以清空了(因为下一次的栈顶不可能出现在前面那些值中). ...

  9. HDU 4942 Game on S♂play(线段树、模拟、扩栈)

    比赛的时候想到这题的大概做法,但由于卡别的水题...就赛后做了... 题意:给一个二叉树,每个结点有一个w[i],有3种操作,0 x表示左旋x,1 x表示右旋x,3 x表示询问x结点的价值,其中,价值 ...

随机推荐

  1. sigmod函数求导

    sigmod函数: \[f(z)=\frac{1}{1+e^{-z}} \] 求导: \[\frac{\partial f(z)}{\partial z}=\frac{-1*-1*e^{-z}}{(1 ...

  2. spring 管理事务配置时,结果 报错: No Hibernate Session bound to thread, and configuration does not allow creation of non-transactional one here这个异常

    java.lang.IllegalStateException: No Hibernate Session bound to thread, and configuration does not al ...

  3. Docker搭建Nessus pro笔记

    0x01 准备Docker环境 拉取镜像: docker pull ubuntu 创建容器: docker run -p 9922:22 -p 8834:8834 --name nessus -it ...

  4. python超实用的30 个简短的代码片段(二)

    Python是目前最流行的语言之一,它在数据科学.机器学习.web开发.脚本编写.自动化方面被许多人广泛使用. 它的简单和易用性造就了它如此流行的原因. 如果你正在阅读本文,那么你或多或少已经使用过P ...

  5. centos7 安装php7遇到的问题

    环境中安装过php 5.4,觉得版本太低了,因此删除旧版本安装了新版本 1. 安装epel-release 通过命令: rpm -ivh http://dl.fedoraproject.org/pub ...

  6. Co-prime 杭电4135

    Given a number N, you are asked to count the number of integers between A and B inclusive which are ...

  7. [apue] getopt 可能重排参数

    看第21章时,介绍到了解析命令行的神器 getopt,了解了 linux 下处理通用命令行的方法. 命令行可分为参数与选项,其中不带 - 或 -- 前缀的为参数,对一个命令而言数量是固定的,多个参数之 ...

  8. search(6)- elastic4s-CRUD

    如果我们把ES作为某种数据库来使用的话,必须熟练掌握ES的CRUD操作.在这之前先更正一下上篇中关于检查索引是否存在的方法:elastic4s的具体调用如下: //删除索引 val rspExists ...

  9. Linux下安装python3环境搭建

    Linux下python3环境搭建 Linux安装软件有哪些方式? rpm软件包 手动安装 拒绝此方式 需要手动解决依赖关系 yum自动化安装 自动处理依赖关系 非常好用 源代码编译安装,可自定义的功 ...

  10. kioptrix靶机记录

    靶机地址:172.16.1.193 Kali地址:172.16.1.107 首页为Apache测试页,没看到有价值信息 尝试目录扫描: 点击查看: http://172.16.1.193/index. ...