📜 ⬆️ ⬇️

When the algorithm is correct, but still TL

Many people are surprised, but how do these different people solve problems so that they are accepted instantly or almost instantly ? The answer is simple: they put a lot of interesting experiments, optimize the code, and sometimes come to funny results. Here I will bring some of my own.

Read integers

1. Accepted in 0.193
std :: ios_base :: sync_with_stdio (0 ) ;
//...
std :: cin >> a [ i ] ;
2. Accepted in 0.187
scanf ( "%d" , &a [ i ] ) ;
3. Accepted in 0.109
int nextInt ( ) {
int res = 0, c = getchar ( ) ;
while ( c < '0' || c > '9' ) c = getchar ( ) ;
while ( c >= '0' && c <= '9' ) {
res = res * 10 + c - '0' ;
c = getchar ( ) ;
}
return res ;
}
//...
a [ i ] = nextInt ( ) ;
4. Accepted in 0.098
a [ i ] = 0 0 ;
c = getchar ( ) ;
while ( c < '0' || c > '9' ) c = getchar ( ) ;
while ( c >= '0' && c <= '9' ) {
a [ i ] = a [ i ] * 10 + c - '0' ;
c = getchar ( ) ;
}

The most interesting is the sharp difference between the second and third paragraph. The third and fourth are also interesting: after all, in the latter there is a multiple reference to the element of the array, which in theory requires more time than a function call once.

Appeal to array

1. Time Limit Exceeded (test 9)
for ( int i = 0 ; i < n ; i ++ ) {
// array[i]
}
2. Accepted
for ( int i = 0 ; i < n ; i ++ ) {
double x = array [ i ] ;
// x
}
Perhaps these observations can help someone. For example, they helped me :)

')

Source: https://habr.com/ru/post/71601/


All Articles