Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
Re:老是WA,有没有高手知道原因?极度郁闷中。。。。。In Reply To:老是WA,有没有高手知道原因?极度郁闷中。。。。。 Posted by:brightstar5 at 2010-04-05 16:00:32 已解决 #include<algorithm> using namespace std; int a[1000011];//数组数据 struct Heap { int x;//堆元素值 int ID;//堆元素在数组a[]中的下标 }; Heap D[1000011];//最大堆 Heap X[1000011];//最小堆 int DL[1000011];//DL[i]表示数组a[i]在D中的下标 int XL[1000011];//XL[i]表示数组a[i]在X中的下标 int OutMin[1000011];//最小值 int OutMax[1000011];//最大值 int n,k; void HeapAdjust(Heap H[] ,int s,int m,int L[],bool isMax) { Heap rc=H[s]; int j; for(j=s*2;j<=m;j*=2) { if(isMax) { if(j<m && H[j].x < H[j+1].x) j++; if(rc.x > H[j].x)break; H[s]=H[j]; L[ H[j].ID ]=s; s=j; } else { if(j<m && H[j].x > H[j+1].x) j++; if(rc.x < H[j].x)break; H[s]=H[j]; L[ H[j].ID ]=s; s=j; } } H[s]=rc; L[rc.ID]=s; } void MakeHeap(Heap H[],int length,int L[],bool isMax) { for(int i=length/2;i>0;--i) { HeapAdjust(H,i,length,L,isMax); } } int main() { int i; int s; scanf("%d%d",&n,&k); if(k>n) k=n; for(i=1;i<=k;++i) { scanf("%d",&a[i]); D[i].x=a[i]; D[i].ID=i; DL[i]=i; X[i].x=a[i]; X[i].ID=i; XL[i]=i; } for(i=k+1;i<=n;++i) { scanf("%d",&a[i]); } MakeHeap(D,k,DL,true); MakeHeap(X,k,XL,false); OutMax[0]=D[1].x; OutMin[0]=X[1].x; s=k/2; for(i=k+1;i<=n;++i) { int j; int d=DL[i-k]; D[d].x=a[i]; D[d].ID=i; DL[i]=d; j=d > s ? d/2 : d; for(;j>0;j/=2) { HeapAdjust(D,j,k,DL,true); } int dd=XL[i-k]; X[dd].x=a[i]; X[dd].ID=i; XL[i]=dd; j=dd >s ? dd/2 : dd; for(;j>0;j/=2) { HeapAdjust(X,j,k,XL,false); } OutMax[i-k]=D[1].x; OutMin[i-k]=X[1].x; } for(i=0;i<=(n-k);++i) { printf("%d%c", OutMin[i], (i < n - k) ? ' ' : '\n'); } for(i=0;i<=(n-k);++i) { printf("%d%c", OutMax[i], (i < n - k) ? ' ' : '\n'); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator