题目链接
题解视频
题解文档

解法概要:
问题可以转化为

考虑一个长为 $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<< or operator>>.

The manipulators that are invoked without arguments (e.g. std::cout << std::boolalpha; or std::cin >> std::hex;) are implemented as functions that take a reference to a stream as their only argument.(e.g. std::fixed is declared as ios_base& fixed(ios_base& str);)The special overloads of basic_ostream::operator<< and basic_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 own operator<< or operator>> which perform the requested manipulation. Manipulators that take arguments are defined in the iomanip header.
SOURCE

basic_ostream::operator<< 默认情况下如何输出浮点数

By default, floating-point values are printed using six digits of precision (1.55 printed as 1.55, 1.55667 printed as 1.55667, 3.1415926 printed as 3.14159); the decimal point is omitted if the value has no fractional part (e.g. 1.00 is printed as 1); trailing zeros are removed (e.g. 1.5000 printed as 1.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.14159 becomes 3.142; if the precision is three, then it is printed as 3.14.

We can change precision by calling the precision by calling the precision member (function) or by using the setprecision manipulator. The precision member is overloaded. One version takes an int value 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. The setprecison manipulator 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 executing scientific, fixed, or hexfloat, 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

  1. https://www.uow.edu.au/~lukes/TEXTBOOK/notes-cpp/io/omanipulators.html

ICPC World Finals 2018 Problem H Single Cut of Failure的更多相关文章

  1. 2020 ICPC Asia Taipei-Hsinchu Regional Problem H Optimization for UltraNet (二分,最小生成树,dsu计数)

    题意:给你一张图,要你去边,使其成为一个边数为\(n-1\)的树,同时要求树的最小边权最大,如果最小边权最大的情况有多种,那么要求总边权最小.求生成树后的所有简单路径上的最小边权和. 题解:刚开始想写 ...

  2. 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 号点出 ...

  3. 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 ...

  4. Codeforces Gym 100610 Problem H. Horrible Truth 瞎搞

    Problem H. Horrible Truth Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/1006 ...

  5. 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 ...

  6. ZOJ 4010 Neighboring Characters(ZOJ Monthly, March 2018 Problem G,字符串匹配)

    题目链接  ZOJ Monthly, March 2018 Problem G 题意  给定一个字符串.现在求一个下标范围$[0, n - 1]$的$01$序列$f$.$f[x] = 1$表示存在一种 ...

  7. ZOJ 4009 And Another Data Structure Problem(ZOJ Monthly, March 2018 Problem F,发现循环节 + 线段树 + 永久标记)

    题目链接  ZOJ Monthly, March 2018 Problem F 题意很明确 这个模数很奇妙,在$[0, mod)$的所有数满足任意一个数立方$48$次对$mod$取模之后会回到本身. ...

  8. 实验12:Problem H: 整型数组运算符重载

    Home Web Board ProblemSet Standing Status Statistics   Problem H: 整型数组运算符重载 Problem H: 整型数组运算符重载 Tim ...

  9. 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. ...

随机推荐

  1. shiro学习记录(三)

    1.使用ehcache缓存权限数据 ehcache是专门缓存插件,可以缓存Java对象,提高系统性能. l ehcache提供的jar包: 第一步:在pom.xml文件中引入ehcache的依赖 &l ...

  2. 循环 -----JavaScript

    本文摘要:http://www.liaoxuefeng.com/ JavaScript的循环有两种,一种是for循环,通过初始条件.结束条件和递增条件来循环执行语句块: var x = 0; var ...

  3. Access数据库远程连接的实用方法

    一般在远程文件夹开启文件共享即可通过像平常一样用连接字符串访问,注意共享的读写权限. 远程(如通过互联网)连接access数据库的示例: 首先,需要使用TCP/IP,ADO及XML(需要安装Micro ...

  4. C# 常用函数和方法集汇总

    1.DateTime 数字型 System.DateTime currentTime=new System.DateTime(); 1.1 取当前年月日时分秒 currentTime=System.D ...

  5. React后台管理系统-table-list组件

    table-list组件可用于商品列表,用户列表页面 需要传入一个tableHeads集合和tablebody import React from 'react';   // 通用的列表 class ...

  6. 1911: [Apio2010]特别行动队

    Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 5706  Solved: 2876[Submit][Status][Discuss] Descriptio ...

  7. ElasticSearch High Level REST API【2】搜索查询

    如下为一段带有分页的简单搜索查询示例 在search搜索中大部分的搜索条件添加都可通过设置SearchSourceBuilder来实现,然后将SearchSourceBuilder RestHighL ...

  8. tp5查询

    TP5的EXP.批量查询.聚合查询等. <!--more--> //使用EXP条件表达式,表示后面是原生的SQL表达式 $result = Db::table('think_inno')- ...

  9. Linux 下PHP获取服务器状态CPU、MEM使用率、磁盘使用率、IP地址获取、MAC地址获取等信息记录

    获取服务器状态记录使用的是SHELL命令方式进行获取输出结果  然后进行字符串拆分处理等方式进行获取到自己有用信息 贴出获取方式以及常用处理方式 <?PHP $fp = popen('执行SHE ...

  10. 各种友(e)善(xin)数论总集(未完待续),从入门到绝望

    目录 快速幂 扩展欧几里得 GCD 扩展欧几里得 同余系列 同余方程 同余方程组 一点想法 高次同余方程 BSGS exBSGS 线性筛素数 埃式筛 欧拉筛 欧拉函数 讲解 两道水题 法雷级数 可见点 ...