|
|
back to boardWhy Runtime error(access violation)???? using namespace std; #include<iostream> #include<stdio.h> #include<algorithm> long cnt=0; long heapsize; long heapsize1; void heapsort(struct st a[]); void buildmaxheap(struct st a[]); void maxheapify(struct st a[],long index); long left(long a); long right(long a); struct st { long serial; long f; long l; }; int main() { long n; scanf("%ld",&n); long s1; long s2; struct st* a=new struct st[n+1]; heapsize=n; for(long i=1;i<n+1;i++) { cnt++; a[i].serial=cnt; scanf("%ld",&s1); getchar(); scanf("%ld",&s2); a[i].f=s1; a[i].l=s2; } heapsort(a); for(long i=n;i>0;i--) { cout<<a[i].f<<" "<<a[i].l<<endl; } } void heapsort(struct st a[]) { buildmaxheap(a); for(long i=heapsize;i>=2;i--) { swap(a[i],a[1]); heapsize1--; maxheapify(a,1); } } void buildmaxheap(struct st a[]) { heapsize1=heapsize; for(long i=heapsize/2;i>=1;i--) { maxheapify(a,i); } } void maxheapify(struct st a[],long index) { long l1=left(index); long r1=right(index); long largest; if(l1<=heapsize1&&a[l1].l>=a[index].l) { if(a[l1].l==a[index].l) { if(a[l1].serial<a[index].serial) largest=l1; } else largest=l1; } else largest=index; if(r1<=heapsize1&&a[r1].l>=a[largest].l) { if(a[r1].l==a[largest].l) { if(a[r1].serial<a[largest].serial) largest=r1; } else largest=r1; } if(largest!=index) { swap(a[largest],a[index]); maxheapify(a,largest); //maintaining property } } long left(long a) { return 2*a; } long right(long a) { return 2*a+1; } |
|
|