Advertisement
Guest User

Untitled

a guest
Oct 19th, 2019
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include <iostream>
  2. #include<queue>
  3. #include<fstream>
  4. #include<vector>
  5. #include<algorithm>
  6. using namespace std;
  7.  
  8. int cmp(pair<int, pair<int,int>>x,pair<int,pair<int,int>>y) // sortez vectorul descrescator dupa t, si descrescator dupa p
  9. {
  10. if(x.second.first < y.second.first || (x.second.first == y.second.first && x.second.second < y.second.second))
  11. return 0;
  12. return 1;
  13. }
  14.  
  15. void citire(vector<pair<int,pair<int,int>>> &v,int &n)
  16. {
  17. ifstream fin("date.in");
  18.  
  19. int p,t,i;
  20.  
  21. fin>>n;
  22. for(i=1;i<=n;i++)
  23. {
  24. fin>>p>>t;
  25. v.push_back({i,{t,p}});
  26. }
  27.  
  28. sort(v.begin(),v.end(),cmp);
  29.  
  30. fin.close();
  31. }
  32.  
  33. struct cmp_pq
  34. {
  35. bool operator()(pair<int,pair<int,int>>x,pair<int,pair<int,int>>y)
  36. {
  37. if(x.second.second < y.second.second) /// sortez descrescator dupa p
  38. return true;
  39. return false;
  40. }
  41. };
  42.  
  43.  
  44. int greedy(vector<pair<int,pair<int,int>>>v,int n)
  45. {
  46. int i=0,h_max=v[0].second.first,T=0;
  47. priority_queue< pair<int,pair<int,int>>,vector<pair<int,pair<int,int>>>, cmp_pq >pq; // coada de prioritati in care avem indicele activitatii (din vectorul nesortat), timpul limita si profitul
  48.  
  49. while(h_max && i<n)
  50. {
  51. while(v[i].second.first==h_max && i<n)
  52. {
  53. pq.push({v[i].first,{v[i].second.first,v[i].second.second}}); // adaugam in coada indicele( din vectorul nesortat),timpul limita si profitul
  54. i++;
  55. }
  56.  
  57. if(pq.size())
  58. {
  59.  
  60. int p=pq.top().second.second; // profitul
  61. cout<<pq.top().first<<" "; // indicele activitatii (din vectorul nesortat )
  62. pq.pop();
  63. T+=p;
  64. }
  65.  
  66. h_max--;
  67. }
  68. cout<<"\nRez este ";
  69. return T;
  70. }
  71.  
  72. int main()
  73. {
  74. vector<pair<int,pair<int,int>>>v;
  75. int n;
  76. citire(v,n);
  77. cout<<greedy(v,n);
  78. return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement