[模拟] 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 吗?(针对这个问题进行分析 ...
随机推荐
- 在没有APP的125年前 印度的外卖小哥是这样送餐
说到印度,你想到的是什么?咖喱.歌舞剧.开挂的火车?通通不是,我今天要说的是他们的外卖小哥,在印度如同"神"一般的存在.其实印度人不叫这批人为外卖小哥,而称他们为dabbawala ...
- 解决sendmail发送邮件慢的问题
sendmail默认会先监听本机的邮件服务,如果本机邮件服务访问不了,在访问其他的邮件服务器 自己测试将本机主机名(通过hostname命令查看)从/etc/hosts中删除,发送邮件的速度就非常快了 ...
- echart 新手踩坑
仪表盘踩坑 我采用的是单文件引入的方式来加载echarts图标也可以使用配置等方式详情参考文档,如果同学们要做出更加丰富的样式请参考文档配置手册配置手册:http://echarts.baidu.co ...
- npm发布包的那些事
npm发包的那些事 最近一直在研习关于node的知识,发布包虽然是最基础的一点,但由于一些地方的不注意很容易发生错误,我整理了我可能出现过的一些发布包的过程中的一些error,现在分享给大家: 正确的 ...
- 【JavaScript】DOM之Document对象
JS(JavaScript) 一.Document对象 1.Document对象是什么 Document对象 是DOM的基本规范也是重要的对象之一,以访问,更新页面内容的属性和方法通过conslie. ...
- 利用GitHub制作在线炫酷简历
首先我们先体验一下炫酷简历.然后决定我们要不要使用. https://jessezhao1990.github.... 如何使用本项目部署你自己的在线简历 1. 书写简历 在src文件夹里面有个con ...
- HBuilder-X 关闭eslint-vue 插件语法检查
HBuilder-X 在写vue项目的时候发现,代码在保存的时候回自动检查eslint语法,会报一大堆的红色警告! 这时候就很烦人,看着不爽,看了下eslint 配置里面介绍了关闭语法检查的配置-- ...
- Object-Oriented Programming Summary Ⅳ
目录 UML单元总结博客 总结本单元两次作业的设计 总结自己在四个单元中架构设计以及OO方法理解的演进 总结自己在四个单元中测试理解与实践的演进 总结自己的课程收获 立足于自己的体会给课程组提三个具体 ...
- 超详细的网络抓包神器 tcpdump 使用指南
原文链接:Tcpdump 示例教程 本文主要内容翻译自<Tcpdump Examples>. tcpdump 是一款强大的网络抓包工具,它使用 libpcap 库来抓取网络数据包,这个库在 ...
- Redis系列一 - 入门篇
问:项目中为何要选用Redis? 答:传统的关系型数据库(如MySQL)已经不适用所有的场景了,比如美云销抢单活动的库存扣减,APP首页的访问流量高峰等等,都容易把数据库打崩,所以引入了缓存中间件,目 ...