[模拟] 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 吗?(针对这个问题进行分析 ...
随机推荐
- SQL命令汇总
order by rocketmq_id; 查找主从在同一IP的集群和节点2. select rocketmq_id,ip,port,type,count(*) as num from t_rock ...
- 【Hardware】i386、x86和x64的故事
(1)x86的由来 x86架构首度出现在1978年推出的Intel 8086中央处理器,它是从Intel 8008处理器中发展而来的,而8008则是发展自Intel 4004的.在8086之后,Int ...
- api安全规范
1. API签名的目的 校验API调用者的身份,是否有权访问 校验请求的数据完整性,防止被中间人篡改 防止重放攻击 2.基本概念 AccessKey: API使用者向API提供方申请的Ac ...
- python 写入 execl 文件 之 xlwt 模块
1. xlwt 安装 pip install xlwt 2. xlwt 操作, 代码 #!/usr/bin/env python3 import xlwt # 只能创建 新的 excel 文件 # 1 ...
- SpringBoot 全局异常处理 @RestControllerAdvice +@ExceptionHandler 请求参数校验
ControllerAdvice 指示带注释的类辅助“控制器”. 作为的特殊化@Component,允许通过类路径扫描自动检测实现类. 通常用于定义@ExceptionHandler, @InitBi ...
- py面向对象编程基础
'''类:一类事物的抽象,用于定义抽象类型 实例:类的单个实际描述 如:人是一个类,而单个人是一个实例 用class来创建一个类,调用一个类来创建一个实例'''class Person: passxi ...
- 30分钟学会Objective-C
注: 本文首发于我的个人博客:https://evilpan.com/2019/04/05/objc-basics/ 请原谅我的标题党.但是如果你有其他语言的学习经验,要学习Objective-C的语 ...
- js监听离开或刷新页面时的弹窗提示
一.看图 二.使用场景. 填写表单时内容,当离开页面或者刷新的时候回丢失页面的内容,因此人性化的设计该有一个提示.所以这样的功能也就应用而生了. 三.思路. 1,页面内容改变.2,离开或刷新浏览器触发 ...
- [续更]一起来撸一下Flex布局里面的那些属性
Flex的全称是Flexible Box,意为弹性布局,用来为盒模型提供最大的灵活性. Flex包含的属性有很多,每个属性又包含了许多不同意义的属性值···然而在实际开发中,能被我们临幸的可能也只是那 ...
- 输入URL到浏览器显示页面的过程,搜集各方面资料总结一下
面试中经常会被问到这个问题吧,唉,我最开始被问到的时候也就能大概说一些流程.被问得多了,自己就想去找找这个问题的全面回答,于是乎搜了很多资料和网上的文章,根据那些文章写一个总结. 写得不好,或者有意见 ...