Advertisement
Vince14

custom struct comparator and constructor usage/example

Dec 1st, 2023
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.14 KB | Source Code | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <cstring>
  4. #include <algorithm>
  5. #include <cmath>
  6. #include <vector>
  7. #include <set>
  8. #include <map>
  9. #include <stack>
  10. #include <queue>
  11. #include <deque>
  12. #include <unordered_map>
  13. #include <numeric>
  14. #include <iomanip>
  15. using namespace std;
  16. #define pii pair<long long, long long>
  17. #define ll long long
  18. #define FAST ios_base::sync_with_stdio(false); cin.tie(NULL)
  19. #define mp(x1, x2) ( make_pair(x1, x2) )
  20. const long long dx[4] = {1, 0, 0, -1}, dy[4] = {0, 1, -1, 0};
  21. const long long dl[2] = {1, -1};
  22. const long long MOD = 100000;
  23. const long long MAXN = 2005;
  24.  
  25. int n, a, b;
  26. struct cow {
  27.     int p;
  28.     int c;
  29.     int x;
  30.     cow(){
  31.         p = 0;
  32.         c = 0;
  33.         x = 0;
  34.     }
  35.     cow (int p_, int c_, int x_){
  36.         p = p_;
  37.         c = c_;
  38.         x = x_;
  39.     }
  40. };
  41.  
  42. vector<cow> v;
  43. int dp[MAXN][2 * MAXN];
  44.  
  45. bool operator<(cow const& cx, cow const& cy){
  46.     if(cx.x != cy.x) return cx.x < cy.x;
  47.     return (cx.p / (double) cx.c) > (cy.p / (double) cy.c);
  48. }
  49.  
  50. int main(){
  51.     FAST;
  52.     cin >> n >> a >> b;
  53.     for(int i = 0; i < n; i++){
  54.         int p, c, x;
  55.         cin >> p >> c >> x;
  56.         v.push_back(cow(p, c, x));
  57.     }
  58.     sort(v.begin(), v.end());
  59.     memset(dp, -1, sizeof dp);
  60.     dp[0][a + b] = 0;
  61.     for(int i = 0; i < n; i++){
  62.         int p = v[i].p;
  63.         int c = v[i].c;
  64.         int x = v[i].x;
  65.         for(int j = 0; j <= a + b; j++){
  66.             if(dp[i][j] == -1) continue;
  67.             dp[i + 1][j] = max(dp[i + 1][j], dp[i][j]);
  68.             if(j >= a + c * x){
  69.                 dp[i + 1][j - c * x] = max(dp[i + 1][j - c * x], dp[i][j] + p);
  70.             }
  71.             else if(j > a){
  72.                 int red = (j - a) / x;
  73.                 if(a >= (c - red)){
  74.                     dp[i + 1][a - (c - red)] = max(dp[i + 1][a - (c - red)], dp[i][j] + p);
  75.                 }
  76.             }
  77.             else if(j >= c){
  78.                 dp[i + 1][j - c] = max(dp[i + 1][j - c], dp[i][j] + p);
  79.             }
  80.         }
  81.     }
  82.     int maxe = 0;
  83.     for(int i = 0; i <= a + b; i++){
  84.         maxe = max(maxe, dp[n][i]);
  85.     }
  86.     cout << maxe << "\n";
  87. }
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement