UVA Problem no. 10055, Hashmat the Brave Warrior, probably the easiest problem there. The input consists of a series of pairs of unsigned integers ≤ 2^32 (thus mandating the use of 64bit integers…) For each pair the task is to print out the difference between the greater and the lesser integer.
According to the statistics, the fastest solutions run in below 0.01 sec. However, all my attempts to solve this typically run in 0.02 sec, with probably random deviations of ± 0.01 sec.
I tried:
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
uint_fast64_t i, j;
while(cin >> i >> j) {
if(i > j)
cout << i-j << '\n';
else
cout << j-i << '\n';
}
}
And also:
#include <cstdlib>
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int_fast64_t i, j;
while(cin >> i >> j) {
cout << abs(i-j) << '\n';
}
}
And also:
#include <algorithm>
#include <cstdint>
#include <iostream>
using namespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
uint_fast64_t i, j;
while(cin >> i >> j) {
cout << max(i,j)-min(i,j) << '\n';
}
}
All with same results.
I also tried using printf()
/scanf()
instead of cin/cout
, still with same results (besides, my benchmarks were showing that cin/cout
preceded by cin.tie(nullptr)
can be even a little faster than printf()/scanf()
– at least unless there are some ways to optimize the performance of cstdio
I’m not aware of).
Is there any way to optimize this down to below 0.01 sec., or should I assume that guys who’ve achieved this time are either extremely lucky or cheaters printing out a precomputed answer to the judge’s input?
The programs are compiled with C++11 5.3.0 - GNU C++ Compiler with options: -lm -lcrypt -O2 -std=c++11 -pipe -DONLINE_JUDGE
.
EDIT: This is my attempt to combine the advices of @Sorin and @MSalters:
#include <stdio.h>
#include <stdint.h>
unsigned long long divisors[] = {
1000000000,
1000000000,
1000000000,
1000000000,
100000000,
100000000,
100000000,
10000000,
10000000,
10000000,
1000000,
1000000,
1000000,
1000000,
100000,
100000,
100000,
10000,
10000,
10000,
1000,
1000,
1000,
1000,
100,
100,
100,
10,
10,
10,
1,
1,
1
};
int main()
{
unsigned long long int i, j, res;
unsigned char inbuff[2500000]; /* To be certain there's no overflow here */
unsigned char *in = inbuff;
char outbuff[2500000]; /* To be certain there's no overflow here */
char *out = outbuff;
int c = 0;
while(1) {
i = j = 0;
inbuff[fread(inbuff, 1, 2500000, stdin)] = '\0';
/* Skip whitespace before first number and check if end of input */
do {
c = *(in++);
} while(c != '\0' && !(c >= '0' && c <= '9'));
/* If end of input, print answer and return */
if(c == '\0') {
*(--out) = '\0';
puts(outbuff);
return 0;
}
/* Read first integer */
do {
i = 10 * i + (c - '0');
c = *(in++);
} while(c >= '0' && c <= '9');
/* Skip whitespace between first and second integer */
do {
c = *(in++);
} while(!(c >= '0' && c <= '9'));
/* Read second integer */
do {
j = 10 * j + (c - '0');
c = *(in++);
} while(c >= '0' && c <= '9');
if(i > j)
res = i-j;
else
res = j-i;
/* Buffer answer */
if(res == 0) {
*(out++) = '0';
} else {
unsigned long long divisor = divisors[__builtin_clzll(res)-31];
/* Skip trailing 0 */
if(res < divisor) {
divisor /= 10;
}
/* Buffer digits */
while(divisor != 0) {
unsigned long long digit = res / divisor;
*(out++) = digit + '0';
res -= divisor * digit;
divisor /= 10;
}
}
*(out++) = '\n';
}
}
Still 0.02sec.