|
|
Deleted by author 06.10.2011 15:39 Edited by author 06.10.2011 15:40 My idea: There are 2 arrays: peoples[] and index[]. First with "quicksort" I sorted peoples[] and use "binary search". Then with AntiQuickSort I changed the array, as it appears before the first sorting and I've got TLE? Why? I don't found my misteke and I hope that you will help me!! Anybody tell me your algo!!!!!!!!!!! Let's count the complexity of your algo. As I see: NlogN(sorting)+NlogN(antiqsort)+logN(to find any occurrence of people) + N/2(to find suitable number that matches a segment)= (2N+1)logN+N/2. It's only for one query. So, in total it will be N((2N+1)logN+N/2). It's too slow. Try to sort once and answer for each query using binary search. Edited by author 05.10.2011 23:01 Thank you Ramil! You say that I should try to sort once, but how? I have three days could not decide! Edited by author 06.10.2011 10:56 Solution deleted by author Edited by author 06.06.2011 01:53 you are blowing balloons in this excercise May be there: L = 0;R = n; must be L = 1;R = n; I still get wrong answer. I don't know C++ well enough. Algorithm seems to be ok Are you sure in construction sort(arr,arr+n); not sort(arr,arr[n]); ? Oh thanks ... I already find my mistake. sort(arr,arr+n) are wrong sort(arr+1,arr+n+1); acceped I suppose quicksort + binary is not the optimal one. In Java I'm having TLE#10. I tried the solution by dimozzz (in another topic), but only with some optimizations I could get AC 0.91s Can anyone suggest really fast solution or the reason is that Java cannot perform better? (I didn't try in Pascal or C++). How to solve it?) Quick sort with binary search gets TLE on the 10'th test, and binary search tree - on the 9'th one! I did it! Hint for all: quicksort + binary search works. Can you show your code?) Or send it to my e-mail: 0x6fwhite@gmail.com I can tell my idea. a[i] - numbers of people, b[i] - numbers of towns. I sort it. First sorting - array a, second sorting - if a[i]=a[j] then you must sort b[i] and b[j]. And finally use binary search. That's all) Hmm. I do the same) But, I think, I do it some more simple. I use structure <people, year>. I sort the array of this type by people... And then I use binary search. Shit! But it gets TLE! I write my programs in PASCAL. Where do you do it? In C++ ? Maybe it works faster... Pavel, it doesnt matter, pascal or C++, its depends on only your brains, how your program works.. Your algo seems to be right, but think about your bin.search realization. Try to find endless cycle, for example. Good luck ;) thanks... I got AC... Edited by author 08.05.2008 01:10 I just sort the array and use lower bound i did it problem qsort+binarysearch and i've TLE#3, but with simple search i've TLE#6; I use built-in C 'qsort' and custom binary search routines (find-equal, find-first-greater-eq, find-last-less-eq). AC in 0.156 sec I wrote simple Binary search but got WA#4... Where is my mistake? Maybe there are some special cases... Can you help me, please? Thanks in advance... I had wa4 and this test helped me to get AC: 6 1 0 1 0 0 1 6 2 2 1 1 1 1 3 3 1 4 4 1 6 6 1 4 5 1 // correct answer is 011010 Try using the STL lower_bound for binary search. It really cuts down potential errors on edge cases. the array. just array of integer. int a[N]; but i have used vector<int> v[N]; array of dynamic arrays. each number in first array in input we must associate with some smaller number from 1 to N for this purpose i have used map<int, int> but it can be hashtable, hashmap or just another array + binary search. Forum says that 1613 most interesting problem in last list. This is because sorting one of the most fundamental idea in algorithmics. Concerning to this problem my advise: struct data{ int val; int num; }; bool operator <(struct data d1,struct data d2) { if (d1.val<d2.val)return 1; if (d2.val<d1.val)return 0; if (d1.num<d2.num) return 1; return 0; } After that we should make qsort, leftmost and right most binary search and so on. More exactly: Let l r x- inqure; and i1=leftmost(l,x); i2=rightmost(r,x); if ((i1!=-1)&&(i2!-1)&&(i1<i2)&&(data[i1]==x)&&(data[i2])==x)- Ok Edited by author 04.04.2008 23:08 I used map and vector with stl sort function got AC, but my solution time is 0.6s then I try to solve it by hash table with Double hash key, but i can't improve the time, and i got .91s. i increas the array lenghts to 2pow17 and hash key is : key% (2pow17-1) // it's prime rh key is: (i+(1+key% (2pow17-2) ) % (2pow17-1) but it appear that isn't good way. plz tell me your solutions with high performance I used a trie to record the question numbers,but why I was TLE on test 10? Could anybody help me or tell me something about test 10? E-mail:zhhy_oi@163.com Edited by author 02.04.2008 18:57 Can any body wright me code of quick sort? It's a very important thing,but i don't know it. sort array mas of integer between l and r Procedure Qsort(l,r:integer); var x,i,j,y:integer; begin i:=l; j:=r; x:=mas[(l+r) shr 1]; repeat while mas[i]<x do inc(i); while mas[j]>x do dec(j); if i<=j then begin y:=mas[i]; mas[i]:=mas[j]; mas[j]:=y; inc(i); dec(j); end; until i>j; if l<j then qsort(l,j); if i<r then qsort(i,r); end; procedure sort(r,l:longint); var i,j,x,b:longint; begin i:=r; j:=l; x:=ar[(i+j) div 2]; repeat while ar[i]>x do inc(i); while ar[j]<x do dec(j); if i<=j then begin b:=ar[i]; ar[i]:=ar[j]; ar[j]:=b; inc(i); dec(j); end; until i>j; if j>r then sort(r,j); if l>i then sort(i,l); end; |
|
|