Advertisement
Asif_Anwar

LightOJ-1004(Monkey Banana Problem)

Jun 2nd, 2021
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4. //using namespace chrono;
  5.  
  6. typedef long long ll;
  7. typedef vector< int > vi;
  8. typedef vector < ll > V;
  9. typedef map<int, int > mp;
  10.  
  11. #define pb push_back
  12. #define FastIO ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
  13. #define F first
  14. #define S second
  15.  
  16. #define debug cout << -1 << endl;
  17. #define REP(i, a, b) for(int i=a; i<b; i++)
  18. #define f0r(i, n) for (int i = 0; i < n; ++i)
  19. #define f0r1(i, n) for (int i = 1; i <= n; ++i)
  20. #define r0f(i, n) for(int i=n-1; i>=0; i--)
  21. #define fore(a, x) for (auto& a : x)
  22. #define fori(i, a, b) for (int i = (a); i < (b); ++i)
  23.  
  24. #define MP make_pair
  25. #define UB upper_bound
  26. #define LB lower_bound
  27. #define nw cout << "\n"
  28.  
  29. #define issq(x) (((ll)(sqrt((x))))*((ll)(sqrt((x))))==(x))
  30. #define rev(v) reverse(v.begin(),v.end())
  31. #define asche cerr<<"Ekhane asche\n";
  32. #define rev(v) reverse(v.begin(),v.end())
  33. #define srt(v) sort(v.begin(),v.end())
  34. #define grtsrt(v) sort(v.begin(),v.end(),greater<ll>())
  35. #define all(v) v.begin(),v.end()
  36. #define mnv(v) *min_element(v.begin(),v.end())
  37. #define mxv(v) *max_element(v.begin(),v.end())
  38. #define valid(tx,ty) (tx>=0 && tx<n && ty>=0 && ty<m)
  39. #define one(x) __builtin_popcount(x)
  40. //#define pop pop_back
  41. #define setPrec(x) cout << fixed << setprecision(x)
  42. #define sz(a) (int)a.size()
  43. //#define fin cin
  44. //#define fout cout
  45. const int INF = 1e9;
  46. const ll INFL = 1e18;
  47. const double diff = 10e-6;
  48. const int maxn = 100005;
  49. const double PI = acos(-1);
  50. using namespace std;
  51.  
  52. // int dx[] = {-1, 0, 1};
  53. // int dy[] = {-1, 0, 1};
  54.  
  55. int n;
  56. ll arr[205][205];
  57. ll dp[205][205];
  58.  
  59. bool ok(int i, int j)
  60. {
  61.     if(arr[i][j]==-1) return false;
  62.  
  63.     return true;
  64. }
  65.  
  66. ll dpF(int i, int j)
  67. {
  68.     if(i>=2*n) return 0LL;
  69.  
  70.     if(dp[i][j]!=-1LL) return dp[i][j];
  71.  
  72.     ll op1 = 0LL, op2 = 0LL, op3 = 0LL;
  73.     //  option-1: just niche jao
  74.     if(ok(i+1, j)) op1 = dpF(i+1, j);
  75.     // option-2: niche left e jao
  76.     if(ok(i+1, j-1) && i>=n) op2 = dpF(i+1, j-1);
  77.     // option-3: niche right e jao
  78.     if(ok(i+1, j+1) && i<n) op3 = dpF(i+1, j+1);
  79.  
  80.     return dp[i][j] = arr[i][j]+max({op1, op2, op3});
  81. }
  82.  
  83. void solve()
  84. {
  85.     memset(arr, -1LL, sizeof(arr));
  86.     memset(dp, -1LL, sizeof(dp));
  87.     cin >> n;
  88.     for(int i=1; i<=n; i++) {
  89.         for(int j=1; j<=i; j++) {
  90.             cin >> arr[i][j];
  91.         }
  92.     }
  93.     // int cnt = 0;
  94.     for(int i=n+1; i<=2*n-1; i++) {
  95.         // cnt++;
  96.         for(int j=1; j<=n-(i-n); j++) cin >> arr[i][j];
  97.     }
  98.     cout << dpF(1, 1) << "\n";
  99. }
  100.  
  101. int main()
  102. {
  103.     FastIO;
  104.     int t;
  105.     t = 1;
  106.     cin >> t;
  107.     f0r(i, t) {
  108.         cout << "Case " << i+1 << ": ";
  109.         solve();
  110.     }
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement