ENG  RUSTimus Online Judge
Online Judge
Problems
Authors
Online contests
About Online Judge
Frequently asked questions
Site news
Webboard
Links
Problem set
Submit solution
Judge status
Guide
Register
Update your info
Authors ranklist
Current contest
Scheduled contests
Past contests
Rules
back to board

Discussion of Problem 1510. Order

random algorithm
Posted by lian lian 27 Dec 2007 21:08
this is my code:


#include<stdio.h>
#include<stdlib.h>
#include<math.h>
int main()
{  long  K[5000005],N,i,j,t,value,k,count,a,flag;
            scanf ("%ld",&N);
            if (N%2==0) k=N/2;
            else k=N/2+1;
            for (i=0;i<N;i++)
                     scanf("%ld",&K[i]);
            for(i=0;i<N;i++){
                    j=rand()%N;
                    for(flag=0,count=1,t=0;t<N;t++){
                            if (K[t]==K[j])
                                    count++;
                            if (count>=k){
                                     value=K[j];
                                     flag=1;
                                     break;
                            }
                    }
                    if (flag=1) break;
             }
            printf("%ld\n",value);
 return 0;
}

why  Crash (stack overflow)?

please help me ?
Re: random algorithm
Posted by KIRILL(ArcSTUpid coder:) 28 Dec 2007 03:20
define big arrays outside procedures
long K[5000005]
Re: random algorithm
Posted by lian lian 28 Dec 2007 18:35
I   modify small than long K[5000005] to k[500005],but it  Crash (stack overflow)?
Re: random algorithm
Posted by KIRILL(ArcSTUpid coder:) 28 Dec 2007 21:43
change you prog to
--------------
long K[5000005]
int main()
{ long N,i,j,t,value,k,count,a,flag;
scanf ("%ld",&N);
------------
or use
#pragma comment(linker, "/STACK:16777216")

defaut stack size ~1mb
Re: random algorithm
Posted by lian lian 29 Dec 2007 00:07


Edited by author 29.12.2007 01:18
Re: random algorithm
Posted by lian lian 30 Dec 2007 08:47
           Thanks ( KIRILL(ArcSTUpid coder:)  )to solve problem of memory for me,but now answer is  wrong;
           could you mail to me for 1510?

            email: k13795263@126.com
                                                            thanks;
Re: random algorithm
Posted by Chmel_Tolstiy 30 Dec 2007 15:12
The problem in rand();
See documentation for return value. It's too small. You need something like this:
t = (rand()*rand())%N;