The Way to Programming
The Way to Programming
Here is code:
#include#include using namespace std; vector heap; void ubaci(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 izvadi() { heap[1]=heap.back(); heap.pop_back(); int t=1,r; while(1) { if (t*2+1 heap[t*2+1]) r=t*2; else r=t*2+1; } else if(t*2 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 >x; ubaci(x); } int m,y; cin>>m; for(int i=0;i >y; if (!y){ if (heap.size()>1) { cout<
Some logical errors in the below – struggled to convert some of your logic, see the comments for where these issues are. The code will compile though.
import java.util.*; class WarezBB { private static ArrayListheap = new ArrayList (); private static Scanner cin = new Scanner(System.in); public static void main(String[] args) { heap.add(0); int n,x; n = cin.nextInt(); for (int i = 0; i < n; i++) { x = cin.nextInt(); ubaci(x); } int m, y = 0; m = cin.nextInt(); for(int i = 0; i < m; i++) { y = cin.nextInt(); if (y != 0){ if (heap.size() > 1) { System.out.println(heap.get(1) + " "); izvadi(); } else { System.out.println("Nema!"); } } else { ubaci(y); } } } private static void ubaci(int x) { heap.add(x); int t = heap.size(); // *something less than t /2 ??? while (/* t/2 && */ (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; } } private static void izvadi() { 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; } // NOT SURE WHAT YOU'RE DOING HERE... (CODE UNREACHABLE) 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; } } } }
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 bussiness 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
Sign in to your account