关于C++输入输出优化的一点事

本文讲述了作者在解决CodeForces 1600J问题时遇到的TLE问题,通过分析发现输入输出是瓶颈,特别是大量输出时。作者通过改用stdio.h库的输入输出或者禁用iostream与stdio的同步,成功优化了代码并获得AC。强调在处理大数据输入输出时,应考虑使用scanf/printf或禁止iostream与stdio同步。
部署运行你感兴趣的模型镜像

这几天在练习CF的时候,有一题TLE了,这里是原题(CF 1600J):

J. Robot Factory
time limit per test: 1 second
memory limit per test: 256 megabytes
inputstandard input
outputstandard output
You have received data from a Bubble bot. You know your task is to make factory facilities, but before you even start, you need to know how big the factory is and how many rooms it has. When you look at the data you see that you have the dimensions of the construction, which is in rectangle shape: N x M.
Then in the next N lines you have M numbers. These numbers represent factory tiles and they can go from 0 to 15. Each of these numbers should be looked in its binary form. Because from each number you know on which side the tile has walls. For example number 10 in its binary form is 1010, which means that it has a wall from the North side, it doesn’t have a wall from the East, it has a wall on the South side and it doesn’t have a wall on the West side. So it goes North, East, South, West.
It is guaranteed that the construction always has walls on its edges. The input will be correct.
Your task is to print the size of the rooms from biggest to smallest.
Input
The first line has two numbers which are N and M, the size of the construction. Both are integers:
n ( 1 ≤ n ≤ 1 0 3 ) n(1≤n≤10^3) n1n103
m ( 1 ≤ m ≤ 1 0 3 ) m(1≤m≤10^3) m1m103
Next N x M numbers represent each tile of construction.
Output
Once you finish processing the data your output consists of one line sorted from biggest to smallest room sizes.
Example
input
4 5
9 14 11 12 13
5 15 11 6 7
5 9 14 9 14
3 2 14 3 14
output
9 4 4 2 1

一开始我就想到了用类BFS(就是BFS算法,但不是求距离)的方法进行写程。后来上传后,Test1和Test2均过关,但是在Test3时TLE了。我看了数据(因为是练习而非比赛),发现n和m都是1000的数据量。我一开始以为是算法的问题,但是经过我的测算,好像是没有问题的。
最后,我设计了一个n平方运算量的n=1000, m=1000数据。发现关键问题在于输出的时候,太慢了。因为是10的6次方的数据,而我的设计是每个“地板块”都是一个房间,故此共有10的6次方的输出。运行了好一会儿,输出都没有输出完毕。
现在仔细想来,我是用的cin和cout,而非scanf和printf。或许用stdio.h库中的输入输出或许可以不用TLE。
从这里可以看出来:运行是正常“瞬间”完成的。而关键的时间消耗就是输出所消耗的。
于是乎,我尝试加上了

ios::sync_with_stdio(false);

结果再次上传,就发现AC了。我自己测试同样的数据,1秒钟就输出完毕了。由此可见,对于大数据的输入输出,要么用scanf和printf,要么就加上ios::sync_with_stdio(false);

最后附上我的两次submit:
TLE代码(131760367)
AC代码(132135194)

原题地址:CodeForces1600J Robot Factory

[10月17日新增]
注意一件事情,那就是当使用

ios::sync_with_stdio(false);

时,请只使用iostream库中的输入输出(cin和cout)或只使用stdio.h库中的输入输出(scanf,printf,getchar,putchar等,虽然说用这个上面这行代码似乎没必要),否则会出现输出错乱的情况,如:

#include <bits/stdc++.h>
using namespace std;
int main () {
	int a, b;
	ios::sync_with_stdio(false);
	cin >> a >> b;
	cout << a + b;
	puts("");
	return 0;
}

该代码的输出为

(我是换行)
2

而非想象中的

2
(我是换行)

而将

puts("");

更改为

cout << endl;

后输出就正常了(即第二种输出),当个注意点吧,因为对于有的题,会同时用cin和cout以及puts或scanf、printf(反正我是这么干),这时候还是统一一下的好。

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值