hpnq

B region 2016-2017 first day

Dec 23rd, 2022
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include "bits/stdc++.h"
  2.  
  3. //speed coding
  4.  
  5. #define mp make_pair
  6. #define cve(a) for (auto i : a) {cout << i << " ";  } cout << "\n";
  7. #define f first
  8. #define s second
  9. #define loop(x, n) for (int i = x; i < n; i++)
  10. #define joop(x, n) for (ll j = x; j < n; j++)
  11. #define err cout << "ERROR" << endl;
  12. #define all(x) x.begin(), x.end()
  13. #define pb push_back
  14. #define sz(x) x.size()
  15.  
  16. // types
  17. #define pii pair<int, int>
  18. #define pll pair<ll, ll>
  19. #define vvi vector<vector<int>>
  20. #define vvll vector<vector<ll>>
  21. typedef long long ll;
  22.  
  23. // types of data
  24. #define inf 1000000000
  25. #define infll 1000000000000000000
  26. #define mod 1000000007
  27.  
  28. //#define DEBUG 1
  29.  
  30. using namespace std;
  31.  
  32. ll n;
  33. // (a, b, c) -> (a-1, b, c) / 2 ...
  34. // (a, b, c) -> (a, b-1, c) +1 / 2 ...
  35. // (a, b, c) -> (a, b, c-1) -1 / 2 ...
  36.  
  37. map<tuple<int,int,int>, ll> mem;
  38.  
  39. ll f(int a, int b, int c){
  40.  
  41.     if(mem.count({a,b,c})) return mem[{a,b,c}];
  42.  
  43.     if(a == 0 and b == 0 and c == 0){
  44.         return n;
  45.     }
  46.     //cout << a << ' ' << b << ' ' << c <<  " " << res << endl;
  47.     ll res = 1e18;
  48.     if(a > 0) res = min(res, f(a-1,b,c)/2);
  49.     if(b > 0) res = min(res, (f(a,b-1,c)+1)/2);
  50.     if(c > 0) res = min(res, (f(a,b,c-1)-1)/2);
  51.  
  52.     return mem[{a,b,c}] = res;
  53.  
  54. }
  55.  
  56. void solve(){
  57.     ll a, b, c;
  58.     cin >> n >> a >> b >> c;
  59.     cout << f(a, b, c);
  60.  
  61. }
  62. int main() {
  63.     ios::sync_with_stdio(0);
  64.     cin.tie(0);
  65. #ifdef DEBUG
  66.     freopen("text.txt", "r", stdin);
  67. #else
  68. #endif
  69.     solve();
  70.     return 0;
  71. }
  72.  
Tags: dp
Advertisement
Add Comment
Please, Sign In to add comment