Advertisement
Kali_prasad

Fractional knapsack//used greedy approach

Jun 3rd, 2022
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.43 KB | None | 0 0
  1. #pragma GCC optimize ("O3")
  2. #pragma GCC target ("sse4")
  3.  
  4. #include <bits/stdc++.h>
  5. using namespace std;
  6.  
  7. typedef long long ll;
  8. typedef pair<int, int> pii;
  9. typedef pair<string,int> psi;
  10. typedef priority_queue<int> pqmax;
  11. typedef priority_queue<int,vector<int>,greater<int>> pqmin;
  12. typedef unordered_map<int,int> mii;
  13. typedef unordered_map<long long,long long> mll;
  14. typedef unordered_map<string,int> msi;
  15. typedef unordered_map<char,int> mci;
  16. typedef unordered_set<int> si;
  17. typedef unordered_set<long long> sll;
  18. typedef unordered_set<string> ss;
  19. typedef unordered_set<char> sc;
  20. typedef map<int,int> ormii;
  21. typedef map<long long,long long> ormll;
  22. typedef map<string,int> ormsi;
  23. typedef map<char,int> ormci;
  24. typedef set<int> orsi;
  25. typedef set<long long> orsll;
  26. typedef set<string> orss;
  27. typedef set<char> orsc;
  28. typedef vector<int> vi;
  29. typedef vector<string> vs;
  30. typedef vector<char> vc;
  31. typedef vector<ll> vll;
  32. typedef vector<vector<int>> vvi;
  33. typedef vector<vector<string>> vvs;
  34. typedef vector<vector<ll>> vvll;
  35.  
  36. #define FOR(i, a, b) for (auto i=a; i<=(b); i++)
  37. #define FORd(i,b,a) for (int i =b; i >= a; i--)
  38. #define sortinc(v)  sort(v.begin(),v.end())
  39. #define sortdec(v)  sort(v.rbegin(),v.rend())
  40. #define sz(x) (int)(x).size()
  41. #define mp make_pair
  42. #define pb push_back
  43. #define pob pop_back
  44. #define pf push_front
  45. #define pof pop_front
  46. #define fi first
  47. #define se second
  48. #define ins insert
  49.  
  50. const int MOD = 1000000007;
  51. int m(ll k) {return k%MOD;}
  52. //type functions here
  53.  
  54. float findMaxProfit(vi &v,vi &w,int &cap)
  55. {
  56.     float profit=0;
  57.     int n=v.size();
  58.     map<float,pair<int,int>>m;
  59.     while(n--){
  60.         //cout<<n<<endl;
  61.        m[w[n]/(float)v[n]]=make_pair(v[n],w[n]);
  62.     }
  63.     int count=0;
  64.     for(auto x:m)
  65.     {
  66.         auto y=x.second.first;
  67.         auto z=x.second.second;
  68.         if(z>cap)
  69.         break;
  70.         cap-=z;
  71.         //cout<<y<<endl;
  72.         profit+=y;
  73.         count++;
  74.     }
  75.    
  76.     while(count--) m.erase((*m.begin()).first);
  77.      float rem=1.0;
  78.    
  79.     rem=cap/(float)((*(m.begin())).second.second);
  80.     if(cap!=0)
  81.     profit+=(rem*((*(m.begin())).second.first));
  82.     //cout<<rem<<endl;
  83.     return profit;
  84. }
  85.  
  86. int main() {
  87.     ios_base::sync_with_stdio(false);
  88.     cin.tie(NULL);
  89.    
  90.     int tc=1;
  91.     //cin>>tc;
  92.     FOR(ww,1,tc)
  93.     {
  94.        vi v={1,2,3,4},w={3,7,4,6};
  95.        int cap=10;
  96.        cout<<findMaxProfit(v,w,cap)<<endl;
  97.        
  98.     }
  99.     return 0;
  100. }
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement