The Delivery Dilemma Codeforces Problem code solution -
Petya is preparing for his birthday. He decided that there would be
Unfortunately, all dishes are prepared in different restaurants and therefore Petya needs to pick up his orders from
- the dish will be delivered by a courier from the restaurant
i , in this case the courier will arrive inai minutes, - Petya goes to the restaurant
i on his own and picks up the dish, he will spendbi minutes on this.
Each restaurant has its own couriers and they start delivering the order at the moment Petya leaves the house. In other words, all couriers work in parallel. Petya must visit all restaurants in which he has not chosen delivery, he does this consistently.
For example, if Petya wants to order
Find the minimum time after which all the dishes can be at Petya's home.
The first line contains one positive integer
Each test case begins with a line containing one integer
The second line of each test case contains
The third line of each test case contains
The sum of
For each test case output one integer — the minimum time after which all dishes can be at Petya's home.
4 4 3 7 4 5 2 1 2 4 4 1 2 3 4 3 3 3 3 2 1 2 10 10 2 10 10 1 2
5 3 2 3
Solution-
Sort the array according to the delivery time and iterate over pair of vector.
store sum of pickup time till sum< delivery time .
then print the curr delivery time .
Follow code below-
#include <bits/stdc++.h> using namespace std; #define int long long int int32_t main() { int t; cin>>t; while(t--){ vector<int > v1; vector<int > v2; int n; cin>>n; int maxx=-1; int sum=0; for(int i=0;i<n;i++){ int k; cin>>k; // maxx=max(k,maxx); v1.push_back(k); } for(int i=0;i<n;i++){ int k; cin>>k; sum+=k; v2.push_back(k); } vector<pair<int ,int> > v3; for(int i=0;i<n;i++){ v3.push_back(make_pair(v1[i],v2[i])); } sort(v3.begin(),v3.end()); int ans=0; int fans=0; for(int i=n-1;i>=0;i--){ ans+=v3[i].second; if(ans<v3[i].first){ continue ; } else if(v3[i].first<ans){ fans=max(ans-v3[i].second,v3[i].first); break; } else{ fans+=v3[i].first; break; } } if(fans!=0){ cout<<fans<<endl; } else{ cout<<sum<<endl; } } return 0; }
- Get link
- Other Apps
Labels
c++ codeforces- Get link
- Other Apps
Comments
Post a Comment