Coca cola canteen of Bharati Vidyapeeth University has n hungry students waiting in line. Each unique order, i , is placed by a student at time ti, and the order takes di units of time to process.
Now canteen in charge wanted to know the sequence in which the orders will be completed so that order which is completed first is served first which resulting in less chaos in the canteen during the break hours but he doesn't know how to solve this problem, so he asks Aman but Aman is busy completing unnecessary lab files(No one knows what is the use of writing the lab file, external even doesn't see what we have written inside the file) as other students, so he asks you to solve this problem Given the information for all n orders, can you find and print the order in which all n students will receive their orders? If two or more orders are fulfilled at the exact same time t, sort them by ascending order number.
Example:
Input: n = 5, v ={{8, 1}, {4, 2}, {5, 6}, {3, 1}, {4, 3}}
Output: 4 2 5 1 3
Approach
C++
#include <bits/stdc++.h>using namespace std;void labFileWork(int n, vector<pair<int, int>> &v){vector<pair<int, int>> vec;for (int i = 0; i < n; i++){int x = v[i].first, y = v[i].second;vec.push_back({x + y, i + 1});}sort(vec.begin(), vec.end());for (int i = 0; i < n; i++){cout << vec[i].second << " ";}}int main(){int n = 5;vector<pair<int, int>> v = {{8, 1},{4, 2},{5, 6},{3, 1},{4, 3}};labFileWork(n, v);return 0;}
No comments:
Post a Comment