int main(){ ios::sync_with_stdio(false); ll n, idx = 0, val, rs; cin >> n; Node nd; cin >> nd.x >> nd.y; val = nd.x, nd.diff = nd.y - nd.x + 1, Score.push_back(nd); for (int i = 1; i < n; i++){ cin >> nd.x >> nd.y; nd.diff = nd.y - nd.x + 1; if (nd.x > val) idx++; Score.push_back(nd); } rs = idx; sort(all(Score), cmp); PriorityQueue pq; int lstidx = idx + 1; for (int i = 0; i < idx; i++){ pq.add(Score[i].diff); } while (val >= pq.top()){ if (!rs || pq.top() == -1) break; if (pq.top() <= val){ val -= pq.top(); pq.remove(); idx--; } while (lstidx < n && Score[lstidx].x > val){ pq.add(Score[lstidx].diff); lstidx++; idx++; } rs = min(rs, idx); } cout << rs + 1 << ln; return 0; }
int main( ) { PriorityQueue q; q.add("Joi"); // Added to the front/back of the queue q.add("Gudrun", 5); // Added to the front of the queue q.add("Gummi", 7); // Added to the front of the queue q.add("Sigga", 3); // Added to the next to back of the queue q.add("Magga"); // Added to the back of the queue for (int i=1; i<=5; i++) { removed = q.remove(); cout << removed << " was removed" << endl; } removed = q.remove(); cout << removed << " was removed" << endl; return 0; }
int main() { PriorityQueue<string> q; q += "a"; q += "c"; q += "b"; try{ cout << q.remove() << endl; cout << q.remove() << endl; cout << q.remove() << endl; cout << q.remove() << endl; }catch(range_error e){ cout << e.what() << endl; } return 0; }
int* getOptimalPath(Vertex Graph[], int size, int start, int goal) { PriorityQueue<float> PQ; LinkedList<edge>* Edges; int current, next, i; float NewCost; float *CostSoFar = new float[size]; for (i = 1; i < size; i++) CostSoFar[i] = FLT_MAX; CostSoFar[0] = 0.0; int* parent = new int[size]; //PQ.insert(start, getEuclideanDistance(Coordinates[start], Coordinates[goal])); PQ.insert(start, 0); while (!PQ.isEmpty()) { //PQ.Print(); current = PQ.remove(); if (current == goal) { delete [] CostSoFar; return parent; } else { Edges = &Graph[current].getEdges(); for (Edges->begin(); !Edges->end(); Edges->next()) { next = Edges->getCurrent().to; NewCost = CostSoFar[current] + Edges->getCurrent().cost /*+ getEuclideanDistance(Coordinates[next], Coordinates[goal])*/; if (NewCost < CostSoFar[next]) { CostSoFar[next] = NewCost; parent[next] = current; PQ.insert(next, NewCost); } } } } delete [] CostSoFar; return NULL; }