http://acm.timus.ru/status.aspx?space=1&num=1510&author=107146 My first solution uses more than 67 kb only for data, but in result table there is 46 Kb. Second solution uses for data structures about 260kb (192 kb more) but in result table - 438Kb And third one uses about 20kb, in result table - empty cell. Why so strange results? is the problem as easy as it seems or there is some trick? ACM problems r never easy except a very few which i have solved.please provide some test cases. now getting TLE for test 19 can somebody please suggest some hints to solve the problem. try to check the variant when n = 1 If you change the Memory Limit to some small value, for example 512K, the problem could be very funny. Good luck~ My AC program (17 lines) used only 118KB )) Can anybody give some stress test for this task?pls If you have problem with WA #13 try this test. 7 10000000 10000001 10000001 100000002 1000000000 10000001 10000001 Right answer is 10000001 this algo with O(n*log(n)) but i don't know why WA on Test21, please help me! [code deleted] Edited by moderator 24.11.2019 13:39 Your mistakes: a[100001] must be a[500001] a[(n-1)/2] must be a[(n+1)/2] qsort(a,n,sizeof(int),compare) must be qsort(a,n+1,sizeof(int),compare). So AC is here: [code deleted] Edited by moderator 24.11.2019 13:38 I got AC with O(n) ! Several times I got TL, but when I stopped reading the input with java.util.Scanner and started using java.io.BufferedReader it worked. java.util.Scanner is too slow, don't use it ! thnx, otherwise TL21 class Scanner { StreamTokenizer in; Scanner(InputStream stream) { in = new StreamTokenizer(new BufferedReader(new InputStreamReader(stream))); } void asserT(boolean e) { if (!e) { throw new Error(); } } int nextInt() { try { in.nextToken(); asserT(in.ttype == in.TT_NUMBER); asserT((int) in.nval == in.nval); return (int) in.nval; } catch (IOException e) { throw new Error(e); } } } ".. and nothing else matters ..." (c)Metallica var max,ll,o,i,j,l,m,n:longint; k:real; b,a:array[1..1000] of real; begin readln(n); for i:=1 to n do begin read(k); j:=j+1; a[j]:=trunc(k); end; J:=0; repeat j:=j+1; for i:=1 to n do if a[j]=a[i] then l:=l+1; o:=o+1; b[o]:=l; l:=0; until j=n; max:=trunc(b[1]); ll:=1; for i:=1 to o do if b[i]>max then begin max:=trunc(b[i]);ll:=i; end; write(trunc(a[ll])); end. #include<iostream> #include<stack> using namespace std; stack < int > s; int n,s1; int main() { scanf("%d",&n); while(n!=0) { scanf("%d",&s1); if(s.empty())s.push(s1); else { if(s1==s.top())s.push(s1); else if(!s.empty()) s.pop(); } n--; } printf("%d\n",s.top()); return 0; } Could anyone tell me why it WA#2? program p1510_Use_QSort; var d:array[1..500000]of longint; n,i,j,max,maxh:longint; procedure qsort(s,t:longint); var i,j,p:longint; begin //make it randomize p:=random(t-s)+s+1; d[s]:=d[s] xor d[p]; d[p]:=d[s] xor d[p]; d[s]:=d[s] xor d[p]; //start sort i:=s;j:=t; while true do begin while (i<j)and(d[i]<=d[j]) do dec(j); if i=j then break; d[i]:=d[i] xor d[j];d[j]:=d[i] xor d[j];d[i]:=d[i] xor d[j]; while (i<j)and(d[i]<=d[j]) do inc(i); if i=j then break; d[i]:=d[i] xor d[j];d[j]:=d[i] xor d[j];d[i]:=d[i] xor d[j]; end; dec(i);inc(j); if s<i then qsort(s,i); if j<t then qsort(j,t); end; begin readln(n); for i:=1 to n do read(d[i]); randomize; qsort(1,n) writeln(d[n shr 1+1]); end. 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 ? define big arrays outside procedures long K[5000005] I modify small than long K[5000005] to k[500005],but it Crash (stack overflow)? 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 Edited by author 29.12.2007 01:18 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; The problem in rand(); See documentation for return value. It's too small. You need something like this: t = (rand()*rand())%N; I thing in this problem impossible get WA. TLE I understand. But... What wrong. I use Qsort and dihotomy. Can anyone give me some interesting tests? Thank's! ;) I have a one question. What here does dichotomy? It here is not necessary :) Maybe you don't understand this task? Try to read again ;) Thank you, Stas! Now I've got AC. In my first solution I use binary find[why not dichotomy?!] But after getting TLE, i see next - "More than half of them are the same." :) It's really fanny task! #include<iostream> #include<algorithm> using namespace std; int used[500000]={0}; bool cmp(int a,int b) { return (a>b); } int main (void) { int N; cin>>N; for (int k=0;k<N;k++) { int f; cin>>f; used[f]++; } sort (used,used+500000,cmp); cout<<used[0]<<endl; return 0; } WA#7 After fixed #include<iostream> #include<algorithm> using namespace std; struct note { int value; int rank; }; note used[500000]={0}; bool cmp(note a,note b) { return (a.rank>b.rank); } int main (void) { int N,j=0; cin>>N; for (int k=0;k<N;k++) { bool zepo=false; int f; cin>>f; for (int i=0;i<j;i++) if (used[i].value==f) { used[i].rank++; zepo=true; } if (!zepo) { used[j].value=f; used[j].rank=1; } } sort (used,used+j,cmp); cout<<used[0].value<<endl; //cin>>j; return 0; } It's so big...Just create a dynamic array,sort it by sort() function and output n/2-th element of array.Good luck! Edited by author 18.08.2007 14:02 i can't understand what is wrong((( var k:array[1..3] of integer; t:array[1..3] of integer; kt:byte=0; c,i,n:integer; begin readln(n); if n<3 then begin readln(c); writeln(c); end else begin kt:=0; for i:=1 to 3 do t[i]:=0; for i:=1 to n do begin read(c); if kt<3 then begin if kt=0 then begin t[1]:=c; inc(kt); end else begin if (kt=1) and (t[1]<>c) then begin inc(kt); t[2]:=c; end; if (kt=2) and (t[1]<>c) and (t[2]<>c) then begin inc(kt); t[3]:=c; end; end; end; if c=t[1] then inc(k[1]); if c=t[2] then inc(k[2]); if c=t[3] then inc(k[3]); end; c:=1; n:=k[1]; for i:=2 to 3 do if k[i]>n then begin c:=i; n:=k[i]; end; writeln(t[c]); end; {if k[1]>n div 2 then writeln(t[1]); if k[2]>n div 2 then writeln(t[2]); if k[3]>n div 2 then writeln(t[3]); readln;} end. Hi. I have WA on test #2. Can you post some samples? Thanks. Edited by author 17.03.2007 22:05 Edited by author 17.03.2007 22:05 Edited by author 17.03.2007 22:49 Edited by author 19.03.2007 18:44 Your program is O(n^2). But you can write simple quick sort(O(n*log(n))) and got AC!!! SORRY!IM BEGINNER,AND I SHALL VERY PLEASED TO YOU IF YOU SAY ME WHAT IT MEANS O(n*log(n))) OR O(n^2). THANK!!!! Edited by author 17.03.2007 23:49 I think I found book about quick sort Edited by author 18.03.2007 00:54 It so hard???? No :) Qsort is standart sorting algo you should not understand how it's work deeply just use it when you need(copypost:) or use included in C++ by the way this problem can be solved in O(n) if you post your mail I can send you several variants P.S. O() is complexity of algo shortly to say for example for i:=1 to n do ... O(n) for i:=1 to n do for j:=1 to n do ... O(n^2) but it's very stupid definition:) Edited by author 18.03.2007 02:37 Why O(n), I think it's O(2*n)!!! Why O(n), I think it's O(2*n)!!! :) O(2*n)= O(3*n)=O(100000000000000000*n) = O(n) constant is not uses with n in O so with small n sometimes O(N^2) solutions work faster then O(n) because of constant my O(n) is only 1 cycle for input for i:=1 to n MY mail:serchch@mail.ru!THANK YOU VERY much!YOU HELP me second time!!!!THANK YOU!!!!!!!! IS QSORT FUNCTION USE IN <ALGORITHM>? Edited by author 19.03.2007 17:50 YES. int a[100]; sort(a,a+100); Edited by author 19.03.2007 18:43 Edited by author 19.03.2007 18:43 My algo is the same :) [DELETED] Edited by author 19.03.2007 18:34 SORRY!CAN YOU SAY WHAT MEANS MALLOC?THANK!!!! Edited by author 19.03.2007 18:24 char *t; t = (char*)malloc(sizeof(char)*10); equal to char *t = new char[10]; THANK!!!NOW I UNDERSTAND! var b,a:array[1..100] of string; x,l,w,n,i,j:integer; begin assign(input,'fac.in');reset(input); assign(output,'fac.out');rewrite(output); readln(n); for i:=1 to n do readln(a[i]); for i:=1 to n do for j:=i+1 to n do begin if (a[i]=a[j]) and (a[i]<>'0') and (a[j]<>'0') then begin x:=x+1; b[x]:=a[i]; for l:=1 to n do if b[x]=a[l] then a[l]:='0'; end; end; for i:=1 to x do writeln(b[i]); close(input);close(output); end. Edited by author 19.03.2007 14:49 Why this source got WA#13 var a:array[1..5000]of longint; n,k,i,j,l,r,mid,com,cout:longint; procedure sort(l,r: longint); var i,j,x,y: longint; begin i:=l; j:=r; x:=a[(l+r) DIV 2]; repeat while a[i]<x do i:=i+1; while x<a[j] do j:=j-1; if i<=j then begin y:=a[i]; a[i]:=a[j]; a[j]:=y; i:=i+1; j:=j-1; end; until i>j; if l<j then sort(l,j); if i<r then sort(i,r); end; begin read(n); for i:=1 to n do read(a[i]); sort(1,n); mid:=-maxlongint; cout:=1; com:=0; for i:=2 to n do if a[i]=a[i-1] then inc(cout) else if cout>mid then begin mid:=cout; cout:=1; com:=a[i-1]; end; if ((com=0)and(n>0)) then com:=a[1] else if (mid<cout) then com:=a[n] else if ((com=0)and(n=0)) then com:=0; write(com); end. Edited by author 16.03.2007 10:55 3 of course Edited by author 16.03.2007 10:54 Edited by author 16.03.2007 10:56 maybe like this.... 5 1 2 3 4 5 or 1 1 or 5 1 1 1 1 1 Does each answer in your tests equal to 1 ? Edited by author 29.10.2006 14:28 yes. paste your code here.later delete. Edited by author 29.10.2006 14:50 ohhhh.your solution incorrect.because 0 ≤ K ≤ 1000000000 check this test 4 10000000 10000001 10000001 100000002 answer:10000001 It gives error, shouldn't longint include all of this data ? begin read(u); here>> 0 ≤ U ≤ 1000000000 and your array z:array[0..500000] of longint; inc(z[u]); end; use quicksort. Thanx a lot!!! I picked up a wrong solution way !!! the answer on test 4 10000000 10000001 10000001 100000002 is 100000002 Are you sure??? Read problem statement more carefuly. You should to output the majority element of this array[it's element which meeting in array more then n/2 times], so the majority element in your sample is 10000001. Test "5 1 2 3 4 5" is incorrect! hi Nurbek where do you study ? which ktl ? |
|