A plus B
Submit solution
Points:
1
Time limit:
5.0s
Memory limit:
256M
Author:
Problem type
Allowed languages
C, C++, Haskell, Java, Python
This problem is intended as an introduction to the submission and judging system. For a detailed write-up of how to process input and output in your language of choice, see algocoding.wordpress.com/2015/04/23/fast-io-methods-for-competitive-programming/.
All input and output should be done using the standard input and output streams respectively.
Input Specification
The first line will contain an integer \(N\) (\(1 \leq N \leq 10^5\)). The next \(N\) lines will each contain a pair of space-separated integers whose absolute value is less than \(10^9\).
Output Specification
Output \(N\) lines each containing a single integer: The sum of each pair of integers from the input, in the same order as they are given.
Sample Input
2
1 1
-1 0
Sample Output
2
-1
Comments
I spent far too long trying to cram this into one gross line.
for _ in range(int(input())): print(sum(map(int, input().split())))
Can anything think of something shorter? I feel like this could be much worse but I/O makes it hard to obfuscate.
I suppose you could take advantage of the fact that Python functions are first-class objects:
Ha, that's fantastic! I knew you can overload std::function::operator() to create first-class functions, but I never really considered how Python functions are obviously first-class by definition.
A couple of minutes in Haskell gets me this:
Forgive the lack of whitespace. Readable Haskell would have between 4 and 16 extra spaces depending on your tastes.
The time seems to vary quite a lot between runs. Something I posted took about a second, then I resubmitted the same code later and it took about 4 seconds..
It is because you are using endl, which causes a buffer flush, and thus is variable.
I'm surprised at the difference between using
cout << endl
and
cout << '\n'
thencout.flush()
at the end of the program.as well as the difference made with
ios::sync_with_stdio(false)
andcin.tie(0)
.I don't think there are any problems at all with only flushing
cout
at the end of your program, but are there any disadvantages when it comes to competitive programming whenios::sync_with_stdio(false)
andcin.tie(0)
would cause an error (other than when mixing C-style input/output and C++ streams)?I am actually not surprised, this is a common issue with cin/cout. Using endl is very slow, as it forces a buffer flush. Using cin and cout can also be very slow unless you use the commands you posted, especially when the judge uses a HDD, which it does. Luckily, with the commands you mentioned, cin/cout become very fast. AFAIK it has no disadvantages other than that you can't use scanf/printf with cin/cout if you unsync and untie them.
Awesome, I'll make sure to use it in the future then. Thanks