[模拟] Codefroces 1175B Catch Overflow!
题目:http://codeforces.com/contest/1175/problem/B
1 second
256 megabytes
standard input
standard output
You are given a function ff written in some basic language. The function accepts an integer value, which is immediately written into some variable xx. xx is an integer variable and can be assigned values from 00 to 232−1232−1. The function contains three types of commands:
- for nn — for loop;
- end — every command between "for nn" and corresponding "end" is executed nn times;
- add — adds 1 to xx.
After the execution of these commands, value of xx is returned.
Every "for nn" is matched with "end", thus the function is guaranteed to be valid. "for nn" can be immediately followed by "end"."add" command can be outside of any for loops.
Notice that "add" commands might overflow the value of xx! It means that the value of xxbecomes greater than 232−1232−1 after some "add" command.
Now you run f(0)f(0) and wonder if the resulting value of xx is correct or some overflow made it incorrect.
If overflow happened then output "OVERFLOW!!!", otherwise print the resulting value of xx.
The first line contains a single integer ll (1≤l≤1051≤l≤105) — the number of lines in the function.
Each of the next ll lines contains a single command of one of three types:
- for nn (1≤n≤1001≤n≤100) — for loop;
- end — every command between "for nn" and corresponding "end" is executed nn times;
- add — adds 1 to xx.
If overflow happened during execution of f(0)f(0), then output "OVERFLOW!!!", otherwise print the resulting value of xx.
9
add
for 43
end
for 10
for 15
add
end
add
end
161
2
for 62
end
0
11
for 100
for 100
for 100
for 100
for 100
add
end
end
end
end
end
OVERFLOW!!!
In the first example the first "add" is executed 1 time, the second "add" is executed 150 times and the last "add" is executed 10 times. Note that "for nn" can be immediately followed by "end" and that "add" can be outside of any for loops.
In the second example there are no commands "add", thus the returning value is 0.
In the third example "add" command is executed too many times, which causes xx to go over 232−1232−1.
题意:
有l个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!
思路:
最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉
注意:1.如果大于等于inf就要标记溢出
2.模拟栈如果访问上一个元素则先单独在外++tp,不然本地都是对的但OJ的输出会迷之出错(不知道为什么)
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int amn=1e5+;
int main(){
ll inf=,ans=,tmp=;
for(int i=;i<=;i++)inf*=;
ll l,tp=,n,st[amn],have=;
bool overflow=;
string order;
ios::sync_with_stdio();
cin>>l;
st[++tp]=;
while(l--){
cin>>order;
if(order=="add"){
if(!overflow){
ans+=st[tp]; ///加上当前for造成的影响
if(ans>=inf)overflow=; ///溢出标记,是否大于等于inf
}
}
else if(order=="for"){
cin>>n;
tp++;
st[tp]=(min(inf,n*st[tp-])); ///当前这个for造成的影响是当前的n和前面的for的影响,相当于前缀积
}
else
tp--; ///碰到end后消去当前for的影响
}
if(overflow)printf("OVERFLOW!!!\n");
else printf("%lld\n",ans);
}
/**
有l个询问,3种命令,add是给x加1,for n是循环n次,end是对应for n的结束,初始x为0,问l次操作后x是否会溢出int(2的32次方-1),如果不会则输出操作过后的x,否则输出OVERFLOW!!!
最后进的for和最先进的end匹配,所以想到用栈来保存每个for的影响(当前for的影响是当前的n*(以前for的累乘),在有add时把这个for的影响加入答案,在有end时把这个for的影响从栈中pop掉
**/
[模拟] Codefroces 1175B Catch Overflow!的更多相关文章
- CodeForces - 1175B Catch Overflow!(栈模拟多重for循环)
You are given a function ff written in some basic language. The function accepts an integer value, w ...
- Educational Codeforces Round 66 (Rated for Div. 2) B. Catch Overflow!
链接:https://codeforces.com/contest/1175/problem/B 题意: You are given a function ff written in some bas ...
- CF-1175 B.Catch Overflow!
题目大意:有一个初始变量,值为0,三种操作 for x 一个循环的开始,循环x次 end 一个循环的结束 add 将变量值加一 问最后变量的值是否超过2^32-1,若超过,输出一串字符,不超过则输出变 ...
- C#中try catch中throw ex和throw方式抛出异常有何不同
我们在C#的try catch代码块中里面经常使用throw语句抛出捕捉到的异常,但是你知道吗使用throw ex和throw抛出捕获到的异常效果是不一样的. 异常捕捉的原理 首先先介绍一下C#异常捕 ...
- 程序中try、throw、catch三者之间的关系
c++程序中,采用一种专门的结构化处理逻辑的异常处理机制. 1.try语句 try语句块的作用是启动异常处理机制,检测try语句块中程序语句执行时可能出现的异常. try语句块总是与catch一同出现 ...
- 用Go语言异常机制模拟TryCatch异常捕捉
有的同学看到Go和TryCatch一起出现,心里可能会说,难道Go语言升级了,加入了try...catch语句.哈哈,其实Go语言从创建之初就没打算加入try...catch语句,因为创建Go的那帮大 ...
- 用Go语言异常机制模拟TryCatch异常捕捉1
有的同学看到Go和TryCatch一起出现,心里可能会说,难道Go语言升级了,加入了try...catch语句.哈哈,其实Go语言从创建之初就没打算加入try...catch语句,因为创建Go的那帮大 ...
- ES6 一些笔记
一. let/const: 1. “暂时性死区”概念:在代码块内,使用let/const命令声明变量之前,该变量都是不可用的.这在语法上,称为“暂时性死区”(temporal dead zone,简称 ...
- synchronized锁详解
synchronized的意义 解决了Java共享内存模型带来的线程安全问题: 如:两个线程对初始值为 0 的静态变量一个做自增,一个做自减,各做 5000 次,结果是 0 吗?(针对这个问题进行分析 ...
随机推荐
- 给你的Kubernetes集群建一个只读账户(防止高管。。。后)
给你的Kubernetes集群建一个只读账户 需求:我们知道搭完k8s集群会创建一个默认的管理员kubernetes-admin用户该用户拥有所以权限,有一天开发或测试的同学需要登录到k8s集群了解业 ...
- Vizceral小白入门
Vizceral小白入门 接到一个任务,要求将N个program可视化,能一目了然查看当前爬虫状态.记得之前做测试时,一个queue service前端可视化效果不错,经询问是用vizceral开源框 ...
- web资源预加载-生产环境实践
此文记录资源预加载在我们项目的实践,技术难度不算高,重在介绍一套技术方案的诞生与实施,其中都进行了哪些思考,依据什么来做决策,如何进行效果评估,等等.为读者在制定技术方案时提供一定启示. 背景 资源预 ...
- VUE实现Studio管理后台(一):鼠标拖放改变窗口大小
近期改版RXEditor,把改版过程,用到的技术点,记录下来.昨天完成了静态页面的制作,制作过程并未详细记录,后期已经不愿再补了,有些遗憾.不过工作成果完整保留在github上,地址:https:// ...
- .Net Core 使用jenkins 部署到远程服务器docker
最近一直在研究docker和kubernetes,小型的项目其实也用不到k8s,本文就是描述如何用jenkins 进行构建asp.net core 3.1项目并且发布到远程服务器docker容器中.本 ...
- [面试专题]Web缓存详解
Web缓存详解 标签(空格分隔): 缓存 缓存之于性能优化 请求更快:通过将内容缓存在本地浏览器或距离最近的缓存服务器(如CDN),在不影响网站交互的前提下可以大大加快网站加载速度. 降低服务器压力: ...
- FC及BFC
1.什么是FC 2.BFC块级格式化上下文(Block formatting context) Box 是 CSS 布局的对象和基本单位, 直观点来说,就是一个页面是由很多个 Box 组成的.元素的类 ...
- 进阶之路 | 奇妙的Thread之旅
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 需要已经具备的知识: Thread的基本概念及使用 AsyncTask的基本概念及使用 学习清单: 线程概述 ...
- Go性能分析大杀器PPROF
这是什么 想要进行性能优化,Go本身自带的工具链就包含了性能分析工具,而且也非常棒,pprof就是Go性能分析的利器,它是Go语言自带的包,有如下两种: runtime/pprof:采集程序(非 Se ...
- Spring MVC系列-(1) Spring概述
1. Spring概述 本章主要介绍Spring中的体系结构和常见概念,比如bean.控制反转(Inverse of Control,IoC)等. 1.1 体系结构 Spring 框架提供约 20 个 ...