A classic topic on OJ: outputting the first K large numbers in an array from large to small.
my algorithm is to build a large top heap, then delete it K times, and get the first K big number.
the same data can be compiled and run locally, but Runtime Error when submitted to OJ
-sharpinclude <iostream>
using namespace std;
long long N,K;
long long * maxHeap;
long long size = 0;
void insertItem(long long * maxHeap)
{
long long item;
cin >> item;
long long pos = PPsize;
for ( pos; maxHeap[pos / 2] <= item; pos /= 2 ) maxHeap[pos] = maxHeap[pos / 2];
maxHeap[pos] = item;
}
long long deleteItem(long long * maxHeap)
{
long long max_item = maxHeap[1];
long long item = maxHeap[size--];
long long parent = 1;
long long child;
for ( parent; parent * 2 <= size; parent = child ) {
child = parent * 2;
if ( child < size && maxHeap[child] < maxHeap[child + 1] ) childPP;
if ( item > maxHeap[child] ) break;
else maxHeap[parent] = maxHeap[child];
}
maxHeap[parent] = item;
return max_item;
}
int main()
{
// freopen("F://input.txt","r",stdin);
cin >> N;
maxHeap = new long long[N];
maxHeap[0] = 1000000000;
for ( long long i = 0; i < N; iPP ) insertItem(maxHeap);
cin >> K;
for ( long long i = 0; i < K; iPP ) cout << deleteItem(maxHeap) << endl;
delete[] maxHeap;
return 0;
}
input:
19
11 2132 45 445 654 34 44 5645 68 455 32 56 51 63 47 45554 655 761
10output
5645
2132
761
655
654
554
455
453
445
68
you can see that the input data is not boundary data, but I don"t know what"s wrong with my code.