ICPC World Finals 2018 Problem H Single Cut of Failure
解法概要:
问题可以转化为
考虑一个长为 $2n$ 的数组 $A$,$1$ 到 $n$ 这 $n$ 个整数每个恰在 $A$ 中出现 $2$ 次。判断是否存在一个长为 $n$ 的子段使得 $1$ 到 $n$ 在其中各出现一次。
这个经典问题可用双指针在 $O(n)$ 的时间内解决。
这道题我 WA 到死,不是因为思路有漏洞,而是因为我不了解 std::cout 。
什么是 IO manipulators
默认情况下,cout 输出浮点数的格式是 std::defaultfloat。
下面我们来看一下 defaultfloat 究竟是个什么东西。
defaultfloat 是一个 ios_base manipulator 。
那么 manipulator 是什么呢
IO manipulator are helper functions that make it possible to control input/output streams using
operator<<oroperator>>.The manipulators that are invoked without arguments (e.g.
std::cout << std::boolalpha;orstd::cin >> std::hex;) are implemented as functions that take a reference to a stream as their only argument.(e.g.std::fixedis declared asios_base& fixed(ios_base& str);)The special overloads ofbasic_ostream::operator<<andbasic_istream::operator>>accept pointers to these functions.The manipulators that are invoked with arguments (e.g.
std::cout << std::setw(10);) are implemented as functions returning objects of unspecified type. These manipulators define their ownoperator<<oroperator>>which perform the requested manipulation. Manipulators that take arguments are defined in theiomanipheader.
SOURCE
basic_ostream::operator<< 默认情况下如何输出浮点数
By default, floating-point values are printed using six digits of precision (
1.55printed as1.55,1.55667printed as1.55667,3.1415926printed as3.14159); the decimal point is omitted if the value has no fractional part (e.g.1.00is printed as1); trailing zeros are removed (e.g.1.5000printed as1.5); and they are printed in eighter fixed or scientific notation depending on the value of the number. The library chooses a format that enhances readability of the number. Very large and very small values are printed using scientific notation. Other values are printed in fixed decimal.
总结:
默认情况下(对应于 manipulator std::defaultfloat),basic_ostream::operator<< 输出浮点数的格式为:
最多 6 位数字(整数部分和小数部分加起来共 6 位,术语为 precision 为 6),具体而言
- 若原本整数部分与小数部分位数之和就不超过 6 位,则原样输出,并去除 trailing zeros。
- 若整数部分不超过 6 位,则先四舍五入去除多出小数部分再去除 trailing zeros。
- 在上述两种情况中,若小数部分完全被去除,则小数点也不输出。
- 若整数部分超过 6 位,则用科学记数法。
std::setprecision(n) 与 std::fixed 的作用
setprecision(n) 的作用是将浮点数的输出精度 (precision) 设为 n。那么这里的 precision 究竟是什么意思呢?
By default, precision controls the total number of digits that are printed. When printed, floating-point values are rounded, not truncated, to the current precision. Thus, if the current precision is four, then
3.14159becomes3.142; if the precision is three, then it is printed as3.14.We can change precision by calling the precision by calling the
precisionmember (function) or by using thesetprecisionmanipulator. Theprecisionmember is overloaded. One version takes anintvalue and sets the precision to that new value. It returns the previous value. The other version takes no arguments and returns the current precision value. Thesetprecisonmanipulator takes an argument, which it uses to set the precision.
C++ Primer, 5th edition, pp 756
注意,这里所说的 “rounded to the current precision” 具体仍然是按上面总结的方法操作。
std::fixed 控制浮点数的记数方式(这里“记数方式”意思是“书写方式”,不是计数方式,也不是精度),std::fixed 使得浮点数始终按十进制格式(fixed decimal,不理解 fixed 在这里是什么意思;fixed decimal 与 scientific notation 相对)输出,也就是说较大的数或较小的数不用科学记数法了。
另外使用 fixed 这个 manipulator 之后,精度的含义也改变了。
These manipulators (
fixed,scientific,hexfloat) also change the default meaning of the precision for the stream. After executingscientific,fixed, orhexfloat, the precision value controls the number of digits after the decimal point. By default, precision specifies the total number of digits——both before and after the decimal point.
总结
如果题目中要输出浮点数,若指明保留几位小数(例如 4 位小数),则在 main 函数的开头加上一行
std::cout << std::fixed << std::setprecision(4);
若只写
std::cout << std::fixed;
就是保留 6 位小数。
Reference
ICPC World Finals 2018 Problem H Single Cut of Failure的更多相关文章
- 2020 ICPC Asia Taipei-Hsinchu Regional Problem H Optimization for UltraNet (二分,最小生成树,dsu计数)
题意:给你一张图,要你去边,使其成为一个边数为\(n-1\)的树,同时要求树的最小边权最大,如果最小边权最大的情况有多种,那么要求总边权最小.求生成树后的所有简单路径上的最小边权和. 题解:刚开始想写 ...
- HDU 6326.Problem H. Monster Hunter-贪心(优先队列)+流水线排序+路径压缩、节点合并(并查集) (2018 Multi-University Training Contest 3 1008)
6326.Problem H. Monster Hunter 题意就是打怪兽,给定一棵 n 个点的树,除 1 外每个点有一只怪兽,打败它需要先消耗 ai点 HP,再恢复 bi点 HP.求从 1 号点出 ...
- Codeforces Bubble Cup 8 - Finals [Online Mirror]H. Bots 数学
H. Bots Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/575/problem/H Desc ...
- Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞
Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...
- Western Subregional of NEERC, Minsk, Wednesday, November 4, 2015 Problem H. Parallel Worlds 计算几何
Problem H. Parallel Worlds 题目连接: http://opentrains.snarknews.info/~ejudge/team.cgi?SID=c75360ed7f2c7 ...
- ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)
题目链接 ZOJ Monthly, March 2018 Problem G 题意 给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...
- ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)
题目链接 ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...
- 实验12:Problem H: 整型数组运算符重载
Home Web Board ProblemSet Standing Status Statistics Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...
- The Ninth Hunan Collegiate Programming Contest (2013) Problem H
Problem H High bridge, low bridge Q: There are one high bridge and one low bridge across the river. ...
随机推荐
- 【转】VS2010发布、打包安装程序(超全超详细)
1. 在vs2010 选择“新建项目”→“ 其他项目类型”→“ Visual Studio Installer→“安装项目”: 命名为:Setup1 . 这是在VS2010中将有三个文件夹, 1.“应 ...
- Spring学习记录(二)
1.Spring中的AOP思想 aop思想:横向重复,纵向抽取. AOP(Aspect-OrientedProgramming,面向切面编程),AOP包括切面(Aspect),通知(Advice),连 ...
- C# 多态与new关键字
1. 你通常怎样用多态? 假设我有一个类,里面有一个 PrintStatus 方法,用于打印实例的当前状态,我希望该类的派生类都带有一个 PrintStatus 方法,并且这些方法都用于打印其实例的当 ...
- linux 下nginx除了首页404的问题
今天在部署tp5的时候除了首页能访问.其他都是not found 原因是 Nginx服务器默认不支持pathinfo,index.php后面的参数都没带上 在需要pathinfo支持的程序中 则无 ...
- Java - 若try中有return语句,finally会执行吗?在return之前还是之后呢?
会执行,在方法return动作之前,return语句执行之后,若finally中再有return语句,则此方法以finally的return作为最终返回,若finally中无return语句,则此方法 ...
- 企业shell面试题及解答
1.面试题:使用for循环在/tmp目录下批量创建10个html文件,其中每个文件需要包含10个随机小写字母加固定字符串template,示例如下 aaesdffbnv_template.html 方 ...
- yii2初步讲解 验证规则
http://www.yii-china.com/post/detail/9.html
- 动态规划:HDU1248-钱币兑换问题
解题心得: (青蛙跳台阶:有n阶台阶,青蛙可以一次跳一阶也可以一次跳两阶,问总共有多好中跳法) 1.之前把这个问题的思路弄错了,以为是递推,就像青蛙跳台阶,用斐波那契求解.但是用斐波那契肯定会超范围. ...
- C# datagridview列绑定类中类的属性
原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://www.cnblogs.com/linghaoxinpian/p/5906374. ...
- 使用WMI Filter 实现组策略的筛选!
今天接到一个客户的一个问题,提到需要分系统版本分发相应的MSI程序.比如简体版接受简体版的分发程序,繁体版接受繁体版的分发程序!这个建立组策略的不同版本分发本身不会太难,我们只需要建立两个不同组策略分 ...