CCF 201903-3损坏的RAID5 100分 c++解答超时原因 ios::sync_with_stdio(false); cin.tie(NULL);解释

超时原因没加上
ios::sync_with_stdio(false);
cin.tie(NULL);
我真傻,真的,我单知道语言不可能限制我的分数,我单认为这是什么玄学加速,加速不可能靠这些东西,这辈子都不可能,我不信邪。今天我敲这一题直30分超时反复检查,百思不得其解,看别人代码不可能啊他这效率也没比我高多少啊,各处一问,大家说糟了,怕是遭了c++了,一看原来是那两家伙没带上 holy sh*t

ios::sync_with_stdio(false);
因为C++为了兼容c,cin和cout要与stdio同步,中间会有一个缓冲,所以导致cin,cout语句输入输出缓慢,把它置为false就可以接触同步,以前一直没问题可能是这次cin的字符串太长问题显露出来了
副作用设置为false后不能在混用c和c++的输入输出了

cin.tie(NULL);
在默认的情况下cin绑定的是cout,每次执行 << 操作符的时候都要调用flush,这样会增加IO负担。可以通过tie(0)(0表示NULL)来解除cin与cout的绑定,进一步加快执行效率。

下图为实际测试
从上到下区别只有2条代码
最上面ios::sync_with_stdio(false);cin.tie(NULL);
中间ios::sync_with_stdio(false);
下面没加
在这里插入图片描述
恶心坏了

// 损坏的RAID5.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>
#include <fstream>
#include <map>
#include <unordered_map>
#include <string>
using namespace std;
int n, s, l, m;
unordered_map<int, string> disks;
void fun(string& a,string b) {
	for (int i = 0; i < 8; ++i) {
		a[i] = a[i] - '0' > 9 ? a[i] - 'A' + 10 : a[i] - '0';
		b[i] = b[i] - '0' > 9 ? b[i] - 'A' + 10 : b[i] - '0';
		a[i] ^= b[i];
		a[i] = a[i] > 9 ? a[i] - 10 + 'A' : a[i] + '0';
		b[i] = b[i] > 9 ? b[i] - 10 + 'A' : b[i] + '0';
	}
}

#define DEBUG
int main()
{
#ifdef DEBUG
	fstream cin("input.txt");
#endif // DEBUG
	ios::sync_with_stdio(false);
	cin >> n >> s >> l;
	for (int i = 0; i < l; ++i) {
		int temp;
		string s;
		cin >> temp >> s;
		disks.insert({ temp,s });
	}
	cin >> m;
	for (int i = 0; i < m; ++i) {
		int block;
		cin >> block;
		if (block > disks.begin()->second.size() / 8 * (n - 1)) {
			cout << '-' << endl;
			continue;
		}
		int y = block / s / (n - 1),
			z = block % s,
			start = (n - y % n) % n,
			disk = ((block / s) % (n - 1) + start) % n,
			depth = y*s + z;
		auto diskp = disks.find(disk);
		if (diskp != disks.end()) 
			cout << diskp->second.substr(depth*8, 8) << endl;
		else if (n - l == 1) {
			string res;
			for (const auto& x : disks) {
				if (res.empty())
					res = x.second.substr(depth*8,8);
				else 
					fun(res, x.second.substr(depth * 8, 8));
			}
			cout << res << endl;
		}
		else {
			cout << '-' << endl;
		}

	}
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值