Advertisement
tien_noob

DOMINOS

Feb 6th, 2021 (edited)
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.42 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3. #include <numeric>
  4. #include <set>
  5. #include <queue>
  6. #include <stack>
  7. #include <vector>
  8. #include <climits>
  9. #include <string>
  10. #include <cstdio>
  11. #include <cmath>
  12. #define task "DOMINOS"
  13. using namespace std;
  14. const int N = 1e3;
  15. long long hieu[N+1], a, b, dp[N+1][14*N+1], n, tmp, j1, j2;
  16. void read()
  17. {
  18.    cin >> n;
  19.    fill(dp[0], dp[N] + 14*N+1, INT_MAX);
  20.    for (int i = 1; i <= n; ++ i)
  21.    {
  22.        cin >> a >> b;
  23.        hieu[i] = a - b;
  24.    }
  25.    tmp = 6*n + 6;
  26. }
  27. void debug()
  28. {
  29.     for (int i = 1; i <= n; ++ i)
  30.     {
  31.         for (int j = -6*n; j <= 6*n; ++ j)
  32.         {
  33.             if (dp[i][j + tmp] >= INT_MAX)
  34.             {
  35.                 cout << ' ';
  36.             }
  37.             else
  38.             {
  39.                 cout << dp[i][j + tmp];
  40.             }
  41.             cout << ' ';
  42.         }
  43.         cout << '\n';
  44.     }
  45. }
  46. void traceback(int x)
  47. {
  48.    cout << dp[n][x + tmp] << '\n';
  49.    for (int i = n; i >= 2; -- i)
  50.    {
  51.        if (dp[i-1][x - hieu[i] + tmp] == dp[i][x + tmp])
  52.        {
  53.            x = x - hieu[i];
  54.        }
  55.        else
  56.        {
  57.            cout << i <<' ';
  58.            x = x + hieu[i];
  59.        }
  60.    }
  61.    if (dp[1][hieu[1] + tmp] != dp[1][x + tmp])
  62.    {
  63.        cout << 1;
  64.    }
  65. }
  66. void solve()
  67. {
  68.    dp[1][-hieu[1] + tmp] = 1;
  69.    dp[1][hieu[1] + tmp] = 0;
  70.    for (int i = 2; i <= n; ++ i)
  71.    {
  72.        for (int j = -6*n; j <= 6*n; ++ j)
  73.        {
  74.            dp[i][j + tmp] = min(dp[i-1][j-hieu[i] + tmp], dp[i-1][j + hieu[i] + tmp] + 1);
  75.        }
  76.    }
  77.    //debug();
  78.    for (int j = 0; j <= 6 *n; ++ j)
  79.    {
  80.        if (dp[n][j + tmp] < INT_MAX)
  81.        {
  82.            j1 = j;
  83.            //cout << dp[n][j1 + tmp] <<' '<<j1 << '\n';
  84.            break;
  85.        }
  86.    }
  87.    for (int j = -1; j >= -6 *n; -- j)
  88.    {
  89.        if (dp[n][j + tmp] < INT_MAX)
  90.        {
  91.            j2 = j;
  92.            //cout << dp[n][j2 + tmp] <<' '<<j2 << '\n';
  93.            break;
  94.        }
  95.    }
  96.    if (abs(j1) < abs(j2))
  97.    {
  98.        traceback(j1);
  99.    }
  100.    else if (abs(j1) > abs(j2))
  101.    {
  102.        traceback(j2);
  103.    }
  104.    else
  105.    {
  106.        if (dp[n][j1 + tmp] <= dp[n][j2 + tmp])
  107.        {
  108.            traceback(j1);
  109.        }
  110.        else
  111.        {
  112.            traceback(j2);
  113.        }
  114.    }
  115. }
  116. int main()
  117. {
  118.    ios_base::sync_with_stdio(false);
  119.    cin.tie(nullptr);
  120.    //freopen(task".INP", "r", stdin);
  121.    //freopen(task".OUT", "w", stdout);
  122.    read();
  123.    solve();
  124. }
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement