here is my code. In my computer he got AC on test#1. But in timus that got WA#1 var a:array[0..500010] of longint; procedure sort(l,r:longint); var i,j,p,x:longint; begin randomize; i:=l; j:=r; x:=a[random(r-l+1)+l]; repeat while x>a[i] do inc(i); while x<a[j] do dec(j); if i<=j then begin p:=a[i];a[i]:=a[j];a[j]:=p; inc(i); dec(j); end; until i>j; if i<r then sort(i,r); if l<j then sort(l,j); end; var p,j,k,n,i,temp,have:longint; begin readln(n); for i:=1 to n do readln(a[i]); sort(1,n); temp:=-1; for i:=1 to n do begin if a[i]=temp then begin inc(have); continue; end; if n mod 2=1 then k:=1 else k:=0; if have>=n div 2+k then begin writeln(temp); halt; end; have:=1;temp:=a[i]; end; if n=1 then writeln(temp); end. Edited by author 02.05.2011 23:00 There is a method that you can find the solution with one loop. here is a hint: 1. if solution is s and sn is the number of s, then 2 * sn > n 2. if you delete any two different numbers until there is no different number, so this number is s.(because 2 * sn > n). And you can do deleting with one loop. good luck Thanks use quickSort and output [n/2] value... 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(a[ll]); end. b,a:array[1..1000] of real!! increase the size from 1000 to 100000. But now with this correction TLE Did anyone make it to the time limit using Python 3.3? I make it to memory limit To do it in Python 3.3, forget about numbers. Use only strings and Boyer-Moore algorithm :) more exactly O(N*log(K)) My algo is exactly O(n). Constant is 1. Just reading input, sometimes even not the whole input. What is K in your formula? I have O(N) solution, AC (0.156). I use "bucketing". But there is solution that works two times faster :( I think for this problem O(n) algorithms as many as stars in the sky. For example 1) qsort; 2) moving along and counting copies of equal values. I have O(N) solution, AC (0.156). I use "bucketing". But there is solution that works two times faster :( In this problem I/O hacks gave me much more speedup than algorithmic issues. My 0.078s solution uses the same linear time, constant memory algorithm as the 0.546s one. 0.062 :P just use your own procedure for reading numbers instead of fscanf. It must read numbers char-by-char Edited by author 31.10.2006 02:17 My solution uses random function and got AC! lol Hehe. I think there are no tests like this: 11 1 2 1 2 1 2 1 2 1 2 1 or you are very lucky =) 0.046 =P My program have AC(0.046) too. But I use only 96kB memory:-) Edited by author 08.03.2007 17:51 Bucketing assumes that we keep an array as big as the value range, which is one billion in this problem. So I suppose you talk about per-digit bucketing, but it is still O(N*log(K)) if we treat input numbers regardless of their base-10 representation. Did you get that constant because of limited length of particular number, given limit on its value? Just telling that it is O(N) just becase representation is fixed to base-10 is not quite far from telling that it is O(1) becase N itself is limited by 500'000 - a constant :) Edited by author 13.07.2008 20:45 Edited by author 13.07.2008 20:45 The O(n) algorithm is Boyer-Moore Majority Vote Algorithm, which time is linear, and memory constant... in fact you don't need more than 3 integer variables to run this algorithm... As some people have mentioned, writing your own int input procedure may improve your performance a lot. First time with no optimization i got 1.06s, then i used a getc() based int input procedure and did some little changes in the code and got 0.046s :) One more O(N) algo is K-th ordered statistics, which is explained in Kormen et. al. So, will program, which reads number and increases the variable, which counts how many times did we meet that number get AC, but not TLE? Thank you for this great suggestion! How did you guys optimize I/O ? Can send me your I/O procedure, my email : tungnk1993@gmail.com i tryed many different ways but always got tle on 19 or 20 tests .Than i saw Boyer-Moore Majority Vote Algorithm.it's really good algo for this problem .Here is algo http://www.cs.utexas.edu/~moore/best-ideas/mjrty/index.html. here is'n written than number of majority element must be more than half so i thought that it would write right answer in any case for example in that case B B B B A A A A A C C C but no .in that algo it's very important the number of majority element to be more than (n/2) thanks . Sorry for bad English. Edited by author 17.01.2012 14:52Boyer-Moore is one of those "brilliant in its simplicity" types of algorithms. Beautiful! Why WA? #include <iostream> using namespace std; void main(){ int x; int y; int jb = 0, jc = 0; cin >> x; //int mass[x]; int *mass = new int[x]; int *mass2 = new int[x]; int *mass3 = new int[x];
for(int i=0;i<x;i++){ cin >> y; mass[i] = y; }
for(int i=0, j=0, p=0;i<x;i++, j=0, p=0){ if(mass[i] == 0){ continue; } for(;j<x;j++){ if(mass[j] == 0){ continue; } if(mass[i]==mass[j]){ p++; if(i!=j){ mass[j]=0; } } } mass2[i] = p; mass3[i] = mass[i]; if(jb<=mass2[i]){ jb=mass2[i]; jc=i; } } cout << mass3[jc]; } AND JAVA How make this program faster? import java.util.*; public class Uno { public static void main(String[] args){ int jb = 0, jc = 0; Scanner in = new Scanner(System.in); int x = in.nextInt(); int[] mass = new int[x]; int[] mass2 = new int[x]; int[] mass3 = new int[x];
for(int i=0;i<mass.length;i++){ int y = in.nextInt(); mass[i] = y; }
for(int i=0, j=0, p=0;i<mass.length;i++, j=0, p=0){ if(mass[i] == 0){ continue; } for(;j<mass.length;j++){ if(mass[j] == 0){ continue; } if(mass[i]==mass[j]){ p++; if(i!=j){ mass[j]=0; } } } mass2[i] = p; mass3[i] = mass[i]; if(jb<=mass2[i]){ jb=mass2[i]; jc=i; } } System.out.print(mass3[jc]); } } OR C# using System; public class Uno { public static void Main(String[] args) { string x; string y; int jb = 0, jc = 0; x = Console.ReadLine(); int[] mass = new int[int.Parse(x)]; int[] mass2 = new int[int.Parse(x)]; int[] mass3 = new int[int.Parse(x)]; for (int i = 0; i < int.Parse(x); i++) { y = Console.ReadLine(); mass[i] = int.Parse(y); } for (int i = 0, j = 0, p = 0; i < int.Parse(x); i++, j = 0, p = 0) { if (mass[i] == 0) { continue; } for (; j < int.Parse(x); j++) { if (mass[j] == 0) { continue; } if (mass[i] == mass[j]) { p++; if (i != j) { mass[j] = 0; } } } mass2[i] = p; mass3[i] = mass[i]; if (jb <= mass2[i]) { jb = mass2[i]; jc = i; } } Console.Write(mass3[jc]); } } This is my code: program num_1510; type ar = array of longint; var N,i,j,c,a:longint; arr,arr2,arr3:ar; pow:boolean; BEGIN read(N); setlength(arr,N+2); setlength(arr2,N+2); setlength(arr3,N+2); for i:=1 to N do read(arr[i]); arr2[1]:=arr[1]; c:=1; for i:=1 to N do begin for j:=1 to N do begin if arr2[j]=arr[i] then begin pow:=false; break; end else begin pow:=true; end; end; if pow then begin arr2[c]:=arr[i]; c:=c+1; end; end; for i:=1 to N do begin for j:=1 to N do begin if arr[i]=arr2[j] then c:=j; end; arr3[c]:=arr3[c]+1; end; a:=arr3[1]; for i:=1 to N do begin if a>arr3[i+1] then a:=a else a:=arr3[i+1]; end; for i:=1 to N do begin if a = arr3[i] then begin a:=i; break; end; end; write(arr2[a]); END. Please, help me:) Edited by author 01.11.2012 00:53 I get access violation when i use array of size N = 500000. Edited by author 15.06.2010 15:54 uses std::nth_element My solution takes 15 lines of code :) And it is possible to do fewer. Did you use Linear time majority vote algorithm? Try 4 0 0 0 0 Answer: 0 Thanks! This helped me :) so if you encounter this remember to add one sentence to avoid being trapped by a series of already-sorted numbers I came across the same problem. Then changed the code to C++ and got AC. Pff, I'm doing quicksort and got AC in 0.25s Add this test, please 45 1 1 1 1 1 2 2 2 2 2 3 3 3 3 3 0 0 0 0 4 0 0 0 0 5 0 0 0 0 6 0 0 0 0 7 0 0 0 0 8 0 0 0 0 9 Good answer 0 But some greedy algorythms answer 1 P.S. Sorry for my english( my algo wrote 9 :D :D :D thanks heped me i think test 7 is like that а где вы видели купюры достоинством 0 (рублей, долларов, евро, фунтов, франков...) ??? ;) "В следующих N строках даны достоинства K этих банкнот (0 <= K <= 10^9)." I used q_sort and have time limit 15? And I used stl sort and got AC. Decides it is problem with Q_sort? yep There is a linear solution. Without sorting. how use q sort in this problem? please help me.for q sort it's need very big (1..10^9) array and it's not able in pascal. program order; var n,ap,i,j:longint; x:array[1..10000] of real; begin readln(n); for i:=1 to n do readln(x[i]); i:=1; while i<=n do begin ap:=1; for j:=i+1 to n do if x[i]=x[j] then ap:=ap+1; if ap>(n div 2) then begin writeln(x[i]:0:0); break; end else i:=i+1; end; end. my algo need less time but i got TLE on test 19 . Then i was interested and send your solution . At first your array size was very small and at second time it got TLE on test 19. So sorry, but it's not solution. What is there in this test? This solution now gets WA38 var ar : array[1..10,0..9] of longint; n,i,a1,a,j,max : longint; begin readln(n); for j := 1 to n do begin readln(a); i := 1; while a <> 0 do begin a1 := a mod 10; a := a div 10; inc(ar[i,a1]); inc(i); end; end; n := 0; for i := 10 downto 1 do begin max := 0; for j := 1 to 9 do if ar[i,j] > ar[i,max] then max := j; n := n*10 + max; end; writeln(n); end. this is my solutioon please help me! #include <iostream> #include <stdio.h> using namespace std; long a[50001], sum=0; void merge(long,long,long); void merge_sort(long low,long high) { long mid; if(low<high) { mid=(low+high)/2; merge_sort(low,mid); merge_sort(mid+1,high); merge(low,mid,high); } } void merge(long low,long mid,long high) { long h,i,j,b[50001],k; h=low; i=low; j=mid+1; while((h<=mid)&&(j<=high)) { if(a[h]<=a[j]) { b[i]=a[h]; h++; } else { b[i]=a[j]; sum+=mid-h+1; j++; } i++; } if(h>mid) { for(k=j;k<=high;k++) { b[i]=a[k]; i++; } } else { for(k=h;k<=mid;k++) { b[i]=a[k]; i++; } } for(k=low;k<=high;k++) a[k]=b[k]; } int main(){
long num,i,a[50001];; cin>>num; for(i=1;i<=num;i++) { cin>>a[i] ; }if(num==1)cout<<a[1]; else{ merge_sort(1,num); cout<<a[num/2]; }return 0;} |
|