简体   繁体   English

获取超出Spoj的时间限制

[英]Get time limit exceeded in Spoj

I am trying to solve this problem http://www.spoj.com/problems/TSORT/ but I receive this error time limit exceeded. 我正在尝试解决此问题http://www.spoj.com/problems/TSORT/,但是我收到了超过此错误时间限制的信息。 My code compiles and sorts correctly on my computer, but when I submit to the spoj it's return this error. 我的代码可以在计算机上正确编译和排序,但是当我提交给spoj时,它将返回此错误。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>



void random_shuffle(int arr[],unsigned long int tamanho)
{
    srand(time(NULL));
    int i, j, temp;
    for (i = tamanho - 1; i > 0; i--)
    {
        j = rand()%(i + 1);
        temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }
}

void swap(int *a, int *b)
{
    int temp;
    temp = *a;
    *a = *b;
    *b = temp;
}
int partion(int arr[], int p, int r)
{
    int pivotIndex = p + rand()%(r - p + 1); 
    int pivot;
    int i = p - 1;
    int j;
    pivot = arr[pivotIndex];
    swap(&arr[pivotIndex], &arr[r]);
    for (j = p; j < r; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }

    }
    swap(&arr[i+1], &arr[r]);
    return i + 1;
}

void quick_sort(int arr[], int p, int q)
{
    int j;
    if (p < q)
    {
        j = partion(arr, p, q);
        quick_sort(arr, p, j-1);
        quick_sort(arr, j+1, q);
    }
}
int main()
{
    int  *arr;
    int size = 0;
    int n;
    int i = 0;    

    scanf("%d",&size);
    arr = (int*)malloc(size*sizeof(int));

    while(scanf("%d",&n) != EOF){
        arr[i++] = n;
    }

    random_shuffle(arr,size); 
    quick_sort(arr, 0, size-1); 
    for (i = 0; i < size; i++)
         printf("%d\n", arr[i]);

    return 0;

}

the following proposed code cleanly compiles and links 以下建议的代码可以干净地编译和链接

Caveat: it is using the OPs algorithm, which I have not tested. 警告:它使用的是OPs算法,我尚未测试过。

for speeding up the code: 用于加快代码:

  1. uses the 'fastRead()' function rather than 'scanf()' 使用'fastRead()'函数而不是'scanf()'
  2. uses the 'fastWrite()' function rather than 'printf()' 使用“ fastWrite()”函数而不是“ printf()”
  3. uses 3 exclusive-or statements (as a macro) rather than the 'swap()' function 使用3个异或语句(作为宏),而不是'swap()'函数
  4. sets functions 'inline' when the compiler allows it 在编译器允许的情况下将函数设置为“内联”
  5. uses the 'getchar_unlocked()', 'putchar_unlocked()' from stdio.h rather than the much slower 'getchar(), and 'putchar()' 使用stdio.h中的'getchar_unlocked()','putchar_unlocked()',而不是慢得多的'getchar()和'putchar()'

and now the proposed code: 现在建议的代码:

#include <stdio.h>


// prototypes
size_t partion( size_t arr[], size_t p, size_t r);
void quick_sort(size_t arr[], size_t p, size_t q);

void fastRead( size_t *a );
void fastWrite( size_t a );


size_t array[ 1000000 ];

#define swap( x, y ) \
        *(x) = *(x)^*(y);  \
        *(y) = *(y)^*(x);  \
        *(x) = *(x)^*(y);


inline void fastRead(size_t *a)
{
    int c=0;
    // note: 32 is space character
    while (c<33) c=getchar_unlocked();

    // initialize result value
    *a=0;

    // punctuation parens, etc are show stoppers
    while (c>47 && c<58)
    {
        *a = (*a)*10 + (size_t)(c-48);
        c=getchar_unlocked();
    }
    //printf( "%s, value: %lu\n", __func__, *a );
} // end function: fastRead


inline void fastWrite(size_t a)
{
    char snum[20];
    //printf( "%s, %lu\n", __func__, a );

    int i=0;
    do
    {
        // 48 is numeric character 0
        snum[i++] = (char)((a%10)+(size_t)48);
        a=a/10;
    }while(a>0);

    i=i-1; // correction for overincrement from prior 'while' loop

    while(i>=0)
    {
        putchar_unlocked(snum[i--]);
    }
    putchar_unlocked('\n');
} // end function: fastWrite


int main( void )
{
    size_t numEntries;
    fastRead(&numEntries);

    for( size_t i = 0; i<numEntries; i++ )
    {
        fastRead( &array[i++] );
    }

    quick_sort(array, 0, numEntries-1);

    for (size_t i = 0; i < numEntries; i++)
    {
         fastWrite( array[i]);
    }

    return 0;
} // end function: main


void quick_sort(size_t arr[], size_t p, size_t q)
{
    size_t j;

    if (p < q)
    {
        j = partion(arr, p, q);
        quick_sort(arr, p, j-1);
        quick_sort(arr, j+1, q);
    }
} // end function: quick_sort


size_t partion(size_t arr[], size_t p, size_t r)
{
    size_t pivotIndex = r >> 1;
    size_t pivot;
    size_t i = p - 1;
    size_t j;

    pivot = arr[pivotIndex];
    swap(&arr[pivotIndex], &arr[r]);

    for (j = p; j < r; j++)
    {
        if (arr[j] < pivot)
        {
            i++;
            swap(&arr[i], &arr[j]);
        }

    }
    swap(&arr[i+1], &arr[r]);
    return i + 1;
} // end function: partion

to eliminate any duplicate number output this code block: 消除此代码块输出的任何重复编号:

    for (size_t i = 0; i < numEntries; i++)
    {
         fastWrite( array[i]);
    }

could be replaced with: 可以替换为:

    // prime the 'pump'
    fastWrite( array[0] );
    size_t lastOutput = array[0];

    // 'pump' all the rest
    for (size_t i = 1; i < numEntries; i++)
    {
         if( lastOutput != array[i] )
         {
             fastWrite( array[i]);
             lastOutput = array[i];
         }
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM