Advertisement
SorahISA

2019 NHSPC north1 pB

Nov 12th, 2019
259
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.37 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define IOS ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
  5. #define X first
  6. #define Y second
  7. #define Push push_back
  8. #define ALL(x) x.begin(), x.end()
  9.  
  10. using lli = long long;
  11. using pll = pair<lli, lli>;
  12. using Double = long double;
  13. template<typename T>
  14. using Vector = vector<vector<T>>;
  15. template<typename T>
  16. using prior = priority_queue<T>;
  17.  
  18. int main() {
  19.     IOS;
  20.     int n;
  21.     cin >> n;
  22.    
  23.     Vector<int> mp(n + 1, vector<int>(n + 1, 0));
  24.     for (int i = 1; i <= n; ++i) {
  25.         for (int j = 1; j <= i; ++j) {
  26.             cin >> mp[i][j];
  27.         }
  28.     }
  29.    
  30.     Vector<pll> dp(n + 1, vector<pll>(n + 1, {0, 0}));
  31.     dp[1][1].X = mp[1][1];
  32.    
  33.     for (int i = 1; i <= n; ++i) {
  34.         for (int j = 1; j <= i; ++j) {
  35.             dp[i][j].X = max(dp[i][j - 1].X, dp[i - 1][j].X) + mp[i][j];
  36.             if (dp[i][j - 1].X > dp[i - 1][j].X) dp[i][j].Y = 0; // east
  37.             else dp[i][j].Y = 1; // south
  38.         }
  39.     }
  40.    
  41.     string walk = "E";
  42.     int x = n, y = n;
  43.     while (x > 1 or y > 1) {
  44.         if (dp[x][y].Y) {
  45.             walk += 'S';
  46.             --x;
  47.         }
  48.         else {
  49.             walk += 'E';
  50.             --y;
  51.         }
  52.     }
  53.    
  54.     walk += "E";
  55.     reverse(ALL(walk));
  56.    
  57.     cout << walk << "\n";
  58.     cout << dp[n][n].X << "\n";
  59.  
  60.     return 0;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement