Advertisement
konchin_shih

Runtime error on test 2

Jul 1st, 2022
800
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.26 KB | None | 0 0
  1. #include<iostream>
  2. #include<algorithm>
  3. #include<vector>
  4. #include<unordered_map>
  5. #include<array>
  6. #include<functional>
  7. #include<cstring>
  8. #include<stack>
  9. #include<queue>
  10. #define endl '\n'
  11. using namespace std;
  12. using ll = long long;
  13. template<typename T> using F = function<T>;
  14. template<typename T> using V = vector<T>;
  15. template<typename T, size_t size> using A = array<T, size>;
  16. template<typename T1, typename T2 = T1> using P = pair<T1, T2>;
  17. void _debug() {cerr << endl;}
  18. template<typename T, typename... Args>
  19. void _debug(const T& a, Args... b) {cerr << a << ' ', _debug(b...);}
  20. #define debug(...) cerr<<'('<<#__VA_ARGS__<<"): ", _debug(__VA_ARGS__)
  21. template<typename T>
  22. ostream& operator<<(ostream& os, const V<T>& v) {
  23.     for (const auto& i : v)
  24.         os << i << ' ';
  25.     return os;
  26. }
  27. template<typename T, size_t size>
  28. ostream& operator<<(ostream& os, const A<T, size>& a) {
  29.     for (auto i : a)
  30.         os << i << ' ';
  31.     return os;
  32. }
  33. constexpr int inf = 0x3f3f3f3f;
  34. constexpr ll infll = (ll)inf << 32 | inf;
  35. int table[256];
  36. inline void init() {
  37.     table['B'] = 0, table['G'] = 1, table['R'] = 2, table['Y'] = 3;
  38. }
  39. using block = A<P<int>, 4>;
  40. istream& operator>>(istream& is, block& b) {
  41.     char color, num;
  42.     for (int i = 0; i < 4; i++)
  43.         is >> color >> num, b[i] = {table[(int)color], num - '0'};
  44.     return is;
  45. }
  46. int comp(int a, int b, int c, int d) {
  47.     if (a > b) {swap(a, b);} if (b > c) {swap(b, c);} if (c > d) {swap(c, d);}
  48.     if (a > b) {swap(a, b);} if (b > c) {swap(b, c);} if (a > b) {swap(a, b);}
  49.     return a << 6 | b << 4 | c << 2 | d;
  50. }
  51. tuple<int, int, int, int> extr(int cond) {
  52.     return {(cond >> 6) & 3, (cond >> 4) & 3, (cond >> 2) & 3, cond & 3};
  53. }
  54. int dp[2][256], s[2][256], stop[2];
  55. bool ins[2][256];
  56. inline void solve() {
  57.     for (int k = 0; k < 2; k++)
  58.         for (int i = 0; i < 256; i++)
  59.             dp[k][i] = ~inf;
  60.     int n, ans = ~inf; cin >> n;
  61.     block curb; cin >> curb;
  62.     int cond = comp(curb[0].first, curb[1].first, curb[2].first, curb[3].first);
  63.     dp[0][cond] = 0, ins[0][cond] = true, s[0][stop[0]++] = cond;
  64.     for (int k = 0; k + 1 < n; k++) {
  65.         cin >> curb;
  66.         int sum = 0;
  67.         for (int i = 0; i < 4; i++)
  68.             sum += curb[i].second;
  69.         while (stop[k & 1]) {
  70.             int cond = s[k & 1][--stop[k & 1]], cur = dp[k & 1][cond], a, b, c, d;
  71.             tie(a, b, c, d) = extr(cond), ins[k & 1][cond] = false, dp[k & 1][cond] = ~inf;
  72.             dp[~k & 1][cond] = max(dp[~k & 1][cond], cur - sum);
  73.             if (!ins[~k & 1][cond])
  74.                 ins[~k & 1][s[~k & 1][stop[~k & 1]++] = cond] = true;
  75.             for (int* e : {&a, &b, &c, &d})
  76.                 for (int i = 0; i < 4; i++) {
  77.                     int x, y; tie(x, y) = curb[i];
  78.                     if (x != *e) continue;
  79.                     int tmp = *e; *e = curb[(i + 2) & 3].first;
  80.                     switch (x) {
  81.                     case 0: y = sum - y; break;
  82.                     case 1: y = sum + y; break;
  83.                     case 2: y = sum * y; break;
  84.                     case 3: y = sum / y; break;
  85.                     }
  86.                     int next = comp(a, b, c, d);
  87.                     dp[~k & 1][next] = max(dp[~k & 1][next], cur + y);
  88.                     if (!ins[~k & 1][next])
  89.                         ins[~k & 1][s[~k & 1][stop[~k & 1]++] = next] = true;
  90.                     *e = tmp;
  91.                 }
  92.         }
  93.     }
  94.     while (stop[~n & 1]) {
  95.         int cond = s[~n & 1][--stop[~n & 1]];
  96.         ans = max(ans, dp[~n & 1][cond]);
  97.     }
  98.     cout << ans << endl;
  99. }
  100. signed main() {
  101.     // cin.tie(nullptr)->sync_with_stdio(false);
  102.     int T = 1;
  103.     cin >> T;
  104.     init();
  105.     while (T--)
  106.         solve();
  107.     return 0;
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement