I hope that I translate task well :
A bread is being sold at the bakery. At the start of the day n bread are baked each of them with quality x. As time passes new bread are baked with various qualities, customers come in and buy the bread. When a customer comes to the salesperson: "Give me the best bread you've got!" The salesperson takes the best bread and sells it to the customer. Help the bakery to have better bussines by always selling their customers the best bread. n(1<=n<=100000) is given and after that n numbers x. The number x describes the quality of the bread. The number m follows after that, then m(1<=m<=100000) numbers y. y describes the events during the day. If y is zero. it means that a customer came and bought the best bread , so it has to write out the quality of the sold bread or "No" if there is no bread. If y isn't zero, it means that a new bread has been baked of the quality y which can be bought from that moment on.
Input:
5
15 32 4 18 29
12
0 0 50 0 24 97 0 0 0 0 0 0
Print:
32 29 50 97 24 18 15 4 No
(Task is like binary tree)
I programmed this task in C++ and works.
Code:
#include <iostream>
#include <vector>
using namespace std;
vector<int> heap;
void insert(int x)
{
heap.push_back(x);
int t=heap.size()-1;
while (t/2 && heap[t]>heap[t/2])
{
int temp=heap[t/2];
heap[t/2]=heap[t];
heap[t]=temp;
t/=2;
}
}
void out()
{
heap[1]=heap.back();
heap.pop_back();
int t=1,r;
while(1)
{
if (t*2+1<heap.size())
{
if(heap[t*2]>heap[t*2+1]) r=t*2;
else r=t*2+1;
}
else if(t*2<heap.size()) r=t*2;
else break;
if(heap[r]>heap[t])
{
int temp=heap[t];
heap[t]=heap[r];
heap[r]=temp;
t=r;
}
else break;
}
}
int main ()
{
heap.push_back(0);
int n,x;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>x;
insert(x);
}
int m,y; cin>>m;
for(int i=0;i<m;i++)
{
cin>>y;
if (!y)
{
if (heap.size()>1)
{
cout<<heap[1]<<' ';
out();
}
else cout<<"No";
}
else insert(y);
}
return 0;
}
But, when I try translate in Java it doesn't work . I programmed in Eclipse, when compiled I get Java exception breakpoints (2 items) -unknown location, and can't insert data entry (numbers) .
import java.util.*; public class Rad { public static java.util.ArrayList<Integer> heap = new java.util.ArrayList<Integer>(); public static Scanner cin = new Scanner(System.in); public static void insert(int x) { heap.add(x); int t = heap.size() - 1; while ( t/2!=0 && (heap.get(t) > heap.get(t/2))) { int temp = heap.get(t / 2); heap.set(t / 2, heap.get(t)); heap.set(t, temp); t /= 2; }} public static void out() { heap.set(1, heap.get(heap.size())); heap.remove(heap.size()); int t = 1,r; while (true) { if (t * 2 + 1 < heap.size()){ if (heap.get(t * 2) > heap.get(t * 2 + 1)) r = t * 2; else r = t * 2 + 1;} else if (t * 2 < heap.size()) r = t * 2; else break; if (heap.get(r) > heap.get(t)) { int temp = heap.get(t); heap.set(t, heap.get(r)); heap.set(r, temp); t = r; } else break; }} public static void main(String[] args) { heap.add(0); int n; int x; n = cin.nextInt(); for (int i = 0;i < n;i++) { x = cin.nextInt(); insert(x); } int m; int y; m = cin.nextInt(); for (int i = 0;i < m;i++) { y = cin.nextInt(); if (y != 0) { if (heap.size() > 1) { System.out.print(heap.get(1)); System.out.print(' '); out(); } else { System.out.print("No");}} else insert(y); }}}
Can you help me to translate well or fix the code?