Advertisement
_rashed

SRM228-D1-500 BagsOfGold

Jan 26th, 2023
890
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.22 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. class BagsOfGold {
  20.     public:
  21.     int a[50],mem[50][50];
  22.  
  23.     int solve(int s, int e) {
  24.         if(s > e) {
  25.             return 0;
  26.         }
  27.         if(s == e) {
  28.             return a[s];
  29.         }
  30.         if(mem[s][e] != -1) {
  31.             return mem[s][e];
  32.         }
  33.         int &ret = mem[s][e];
  34.         ret = max(a[s]-solve(s+1,e),a[e]-solve(s,e-1));
  35.         return ret;
  36.     }
  37.  
  38.     int netGain(vector <int> bags) {
  39.         for(int i = 0; i < bags.size(); i++) {
  40.             a[i] = bags[i];
  41.             for(int j = i; j < bags.size(); j++) {
  42.                 mem[i][j] = -1;
  43.             }
  44.         }
  45.         return solve(0,bags.size()-1);
  46.     }
  47. };
  48.  
  49. int main()
  50. {
  51.     ios_base::sync_with_stdio(NULL);
  52.     cin.tie(0);
  53.     return 0;
  54. }
  55.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement