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

Neoma ·
更新时间:2024-09-20
· 714 次阅读

超时原因没加上
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 #include #include #include #include #include using namespace std; int n, s, l, m; unordered_map disks; void fun(string& a,string b) { for (int i = 0; i 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 > temp >> s; disks.insert({ temp,s }); } cin >> m; for (int i = 0; i > 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 <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; } } }
作者:-Z-G-D-



cin c+ FALSE with sync C++ raid IOS null

需要 登录 后方可回复, 如果你还没有账号请 注册新账号