Страница 1 Can anyone tell me what is test number 6??? I used BIT and i get WA at test numebr 6... Help me please Try this test 3 3 1 2 3 3 1 2 3 2 1 right answer is 3 Edited by author 05.03.2009 18:23 Could anyone show me what the input for test #5 is? I can't figure out why my solution fails this test case. Thanks a lot. Try this test 3 3 1 2 3 3 1 2 3 2 1 right answer is 3 Edited by author 05.03.2009 18:22 I have tried using bucket search( N*sqrt(N)) and segment tree based search( N*log(N)), but still only get AC in 0.1s Anyone can shed some light on this? I have seen someone mentioned mergsort, is this the point? Well, I've also tried merge sort approach(use iterative mergesort instead of recursive), but still only get AC in 0.078s what's the point to boost it into 0.015s? Anyone can shed some light? Thanks a lot! oh, YES! This is brilliant data structure =) goood thing ) horrible code: #pragma comment(linker,"/stack:3000000") #include <iostream> int X[10100],n,k,r,h=1e9,g,x,i,j,l; int main(){ if(!j)for(i=n;i;X[i--]=0); if(!l&!j)std::cin>>n>>k; if(l<k){ std::cin>>x; for(i=x;i;r+=X[i],i-=i&-i); for(;x<=n;X[x]++,x+=x&-x); ++j==n?++l,(r<h?h=r,g=l:0),j=r=0:0; main(); } else std::cout<<g; } Edited by author 10.04.2011 19:41 it's the number of swaps in a bull sort. Edited by author 03.08.2011 02:27 var i, j, q, key, n, k, k1, max, row : integer; a : array [1..10000] of integer; begin read(n, k); row := 1; max := 0; for k1 := 1 to k do begin for i := 1 to n do read(a[i]); q := 0; For j:=2 to n do Begin Key:=a[j]; I:=j-1; While (i>0) and (A[i]>key) do Begin A[i+1]:=a[i]; inc(q); Dec(i); End; A[i+1]:=key; End; if max < q then begin max := q; row := k1; end; end; writeLn(row); end. here another code but yet TLE#4!! var b : array [1..10000] of smallint; j, n, k, x : smallint; max, s : integer; i, m, q : byte; begin read(n, m); q := 1; max := 0; for i := 1 to m do begin s := 0; for j := 1 to n do begin read(x); b[x] := j; end; for j := 1 to n do begin s := s + b[j] - j; x := b[j]; for k := b[j] downto j + 1 do b[k] := b[k-1]; b[j] := x; end; if max < s then begin max := s; q := i; end; end; writeLn(q); end. You should have nearly O(k*n*logn) (mayby O(k*n*sqrt(n)) algo to avoid TLE . What's about yours? i don't understand you, pls ubderstand and give me some hints var a, b, c : array [1..10000] of smallint; j, n, k : smallint; max, s : integer; i, m, q : byte; begin read(n, m); q := 1; max := 0; for i := 1 to m do begin s := 0; for j := 1 to n do begin read(a[j]); b[a[j]] := j; c[j] := 0; end; for j := 1 to n-1 do begin s := s + b[j] - j+c[j]; for k := a[b[j]] to b[j]-1 do inc(c[k]); end; if max < s then begin max := s; q := i; end; end; writeLn(q); end. Hey, I got TLE on this code with O(k*n*lgn) using Indexed trees. My code is attached for reference: #include<iostream> #include<vector> #include<cstdio> #include<algorithm> using namespace std; int bit[10001]; bool myfunc(vector<int> a,vector<int> b) { if(a[0]>b[0]) return true; return false; } void update(int x,int n) { int i; for(i=x;i<=n;i+=i&-i) bit[i]++; } int getval(int x) { int i,ans=0; for(i=x;i>0;i-=i&-i) ans=ans+bit[i]; return ans; } main() { int n,k,ans=0,ind; scanf("%d %d",&n,&k); int cas=1; while(k--) { int i;
vector<vector<int> > arr; vector<int> emp; arr.clear(); for(i=0;i<n;i++) { arr.push_back(emp); arr[i].push_back(0); scanf("%d",&arr[i][0]); arr[i].push_back(i+1);
} sort(arr.begin(),arr.end(),myfunc); memset(&bit,0,sizeof(bit)); // cout<<bit[1]<<endl; int val=0; for(i=0;i<n;i++) { // cout<<arr[i][1]<<" "; //cout<<val<<" "; val=val+getval(arr[i][1]); update(arr[i][1],n);
} // cout<<endl;
if(ans<val) {ans=val;ind=cas;} cas++; } printf("%d",ind); // cin>>ind; } Any hints? This is not an A+B problem and you should use a more efficient algorithm to avoid using a lot of time. at first i wrote bst,and i got tle at testcase 3. Now i know that i should have written SBT such as treap. <Chinese>闫X远,“SBT”是陈启峰大牛的“Size Balanced Tree”的专用简称,和Treap不是一码事。你大概是想说“平衡二叉树”吧?这个题用SBT, Treap,或者一种O(nlogn)的基于归并排序的算法都能AC。</Chinese> lonelycorn, the word "SBT" is for Chen Qifeng's "Size Balanced Tree", which is different from Treap. I think you were saying "Balanced BST". And yes, treap and SBT both work, and a simple algo.,O(nlogn) based on mergesort also works. I've written AVL Tree in Java and AC in 0.7 sec. Be careful, this solution should work fast, or will get TLE! Can someone give me test case..i use insertion sort to calculate number of jumps. Could anybody suggest the 3-rd test???? i don't know my mistake. :-( 5 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 1 2 3 4 5 the right one is 1 perhaps your programme give 0 A very useful test data! Thanks a lot can't be! i'm fighting with 3rd test for more than hour. And when i test your numbers i get 1 as answer, but still not passing test. #include <iostream> #include <fstream> #include <string> #include <vector> #include <math.h> using namespace std; class Summator { private: vector<bool> l; vector<int> b; int size; int b_size,b_count; public: void init (int _size) { size=_size; b_size=(int)pow(size,0.5); b_count=size/b_size+1; l.resize(size); b.resize(b_count); } void reset() { for(int i=0;i<size;i++) l[i]=false; for(int i=0;i<b_count;i++) b[i]=0; } int findsum(int a) { int p=a/b_size; if (a%b_size!=0) p++; int res=0; for(int i=p+1;i<b_count;i++) res+=b[i]; a-=(--p)*b_size; for(int i=a;i<=b_size;i++) if ((p*b_size+i<size)&&(l[p*b_size+i])) res++; return res; } void add(int pos) { l[pos]=true; int p=pos/b_size; if (pos%b_size!=0) p++; b[p]++; } }; int main () { #ifndef ONLINE_JUDGE ifstream cin("input.txt"); #endif int n,k,res,max=0; cin>>n>>k; Summator s; s.init(n+1); for(int t=0;t<k;t++){ int rs=0,p; s.reset(); for(int i=0;i<n;i++) { cin>>p; rs+=s.findsum(p+1); s.add(p); } if (rs>max) { max=rs; res=t+1; } } cout<<res<<endl; return 0; } Edited by author 16.04.2007 21:18 use b_size=(int)pow((double)size,0.5); also ifdef - not ifndef Kirill Edited by author 16.04.2007 21:33 Program p1090; Const Maxn=12000; Var Ans,good,p,n,m,i,j,k,fin:Longint; A:array[0..Maxn] of Longint; L:array[0..Maxn] of Longint; Procedure Find(left,right,p:Longint); Var mid:Longint; Begin If left>right Then Exit; If Left=Right Then Begin If (L[left]<p) And (L[left]<>0) And (Left>good) Then Good:=left; Exit; End; mid:=(left+right) div 2; If (L[mid]<P) And (L[mid]<>0) And (mid>good) Then Begin Good:=mid;Find(mid+1,right,p); End Else Find(left,mid-1,p); End; Begin Readln(N,M); Fin:=Maxlongint div 2; For i:=1 to m Do Begin For j:=1 to n Do Read(A[j]); Readln; Fillchar(L,sizeof(L),0); Ans:=0; For j:=1 to n Do Begin Good:=0; Find(1,Ans,A[j]); If good+1>Ans Then Ans:=good+1; If (L[good+1]=0) or (L[good+1]>A[j]) Then L[good+1]:=A[j]; End; If Ans<fin Then Begin Fin:=Ans;P:=I; End; End; WRiteln(p); End. I change languege from Java to cpp.It is very small time limit for Java. You shouldn't allocate memory for auxiliary array every time in your recursive method. You allocate this array O(K*log(N)) times. Try to allocate it only one time. Thank you very much. I have AC with Java too. post the fifth test or a similar one please if they are not too long why insert sort doesn't work? i've found my mistake: never display 0 when the first row is 1 I have tried both bucket sort and segment tree based(nlogn) approaches, but seems both can not beat the time limit? how to get the runtime to 0.015s? Can somebody give me some ideas? What algorithm to use? Post your e-mail, and I'll answer on your question LineTree But in Init,You must let ans be 1.maybe there is a test,the test's each line is 1,2,3,4...... Who can give me an AC program? no Edited by author 29.11.2019 21:57 Edited by author 29.11.2019 21:57 |
|