# | User | Rating |
---|---|---|
1 | jiangly | 3756 |
2 | tourist | 3723 |
3 | orzdevinwang | 3696 |
4 | Kevin114514 | 3647 |
5 | Radewoosh | 3631 |
6 | ecnerwala | 3596 |
7 | Benq | 3527 |
8 | maroonrk | 3518 |
9 | ksun48 | 3484 |
10 | Nachia | 3463 |
# | User | Contrib. |
---|---|---|
1 | errorgorn | 169 |
2 | Qingyu | 165 |
3 | Dominater069 | 159 |
4 | cry | 157 |
4 | Um_nik | 157 |
6 | adamant | 155 |
7 | -is-this-fft- | 152 |
8 | djm03178 | 148 |
9 | soullless | 146 |
10 | chromate00 | 144 |
What form of I/O should we use in competitive programming?
For examplescanf
andprintf
are undoubtedly faster thancin
andcout
, but the later two are easier and faster to code. How should we adress this tradeoff?
» | Just include |
» » |
» » |
» » | Thenmukel why isn't it there by default? Any disadvantages of the same? |
» » » |
» » » | You can't use both cin/cout and printf/scanf. |
» » » » |
» » |
» » » | The problem is the output, cout << endl flushes the output every time wich hit performance badly. To output an end line without flushing just use #define endl '\n' |
» » » » | Yeah I got that. Actually I have tried that also. It works. Now my question is, if I do everything as recommended about cin/cout, will it be enough to solve all problems? I find that confusing. Just a few minutes ago, I have solved this problem at spoj:http://www.spoj.com/ranks/INVCNT/start=20 This is a simple problem of counting inversions. I just implemented BIT. By the way, the thing is, I made three submissions for the problem. Check out this screenshot: So you can see the difference. The first one was using scanf/printf. Second one was with cin/cout+sync_with_stdio and the third one used just cin/cout. |
» » » » » | Looks like the screenshot is not loading. Here is the link: |
» » |
» » | It happened in Round #304 Div2 Problem D. I submitted the problem with cin, cout and used The submission with cin, cout got TLE on 3rd testcase: Link:http://codeforces.com/contest/546/submission/11225677 whereas the same solution passed with scanf and printf. Link:http://codeforces.com/contest/546/submission/11342025 Can you explain why this happened? |
» » | Using |
» | Likemukel said, Once, at another Online Judge, I kept getting TLE's for an algorithm that was quite IO intensive. I was using cin/cout and PD:Also, favor '\n' instead of endl for ending lines. endl flushes the buffer every time. |
» » | Does this apply to all streams? i.e. reading files with |
» » » |
|
» » » » |
» » » » | But fstream is even faster than iostream with ios_base::sync_with_stdio() |
» |
» | but also notice: with |
» | Put this: |
» » |
» » | Checkthis I have used |
» » » |
» » » | try it in a problem which requires you to answer 10^5 or more queries. this question doesnt require fast I/O. try this problemhttp://codeforces.com/contest/456/problem/E just copy any accepted C++ solution and submit it with endl and ios_base::sync_with_stdio(false);cin.tie(0); it will fail. check my submission history. |
» | fastest I is getchar_unlocked() |
» » | just found out this blog is 2 years old ;| |
» » » | Two years old, but no one else said anything about getchar_unlocked =P. It was really hard to convince myself to move from C to C++ half a decade ago, but I did. I never liked cin/cout though, so I still use scanf/printf instead, and that probably won't change. In the very rare times I need fast input reading (like when the input size is O(n) and an O(n) solution is expected, but mine is O(n lg n)): Usually I write it as the following function:
Making it parse input as float, string, int with sign, return if eof, etc should be easy too... getchar_unlocked already gave me two or three balloons, so I'm grateful to it =)! |
» » » » | getchar_unlocked gave CE on CF. Would you please check? |
» » » » » |
» » » » » | http://stackoverflow.com/questions/13010505/getchar-unlocked-in-windows-undeclared most problems can be solved by googling a little |
» » » » » » | You are right =). I didn't even know CF used Windows system! O.o I remember having used getchar_unlocked on CF in the past, but I'm probably wrong then. I use Windows on my machine because I play a lot, and it's incovenient to keep rebooting. So, what I did was to add the following line to my stdio.h: About it not working on CF, though, there is nothing we can do =/. P.S.: using getchar is still faster than scanf, of course. But it usually isn't fast enough to make the difference (make a TLE become an AC). |
» |
» | I use this recently, and work very good :
|
» » |
» | Hey guys, thanks for such an excellent discussion. I've read the entire page, but forgive me if I missed something, as I am going to ask a stupid question. * What if sometimes I want to use |
» » |
» » | You wouldn't want to switch off thesync and then useboth cin/cout and scanf/printf as it "may result in unexpectedly interleaved characters". As it has been thoroughly explained above, cin/cout with syncing off and '\n' instead of endl should be faster than scanf / printf because the latter should undergo parsing all the time. You must call the function before any input / output is done, so no, you shouldn't be toggling it. |
» |
» | Before few minutes, I have solved Light oj problem no:1087,problem title:"Diablo". In that problem, at first, I have usedcin/count along withios_base::sync_with_stdio(false);cin.tie(0);. I have also used#define endl "\n" and used noprintf/scanf, which gave meRuntime Error. Later on, I have given up only this partcin.tie(0); and everything was similar like the before which gave meTime Limit Exceed. At last, I have used onlyprintf/scanf which gave meAccepted. Can any body explain it? (Thanks In Advance) |
» » | I also had same experience for light oj problem no. 1088. Please explain if anybody knows why?? is it platform issue or something else?? |
Name |
---|