Advertisement
BaoJIaoPisu

Untitled

Sep 26th, 2021
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.06 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. #define fi first
  3. #define se second
  4. #define pb push_back
  5. #define pf push_front
  6. #define popb pop_back
  7. #define popf pop_front
  8. #define ins insert
  9. #define pq priority_queue
  10. #define minele min_element
  11. #define maxele max_element
  12. #define lb lower_bound //first pos >= val
  13. #define ub upper_bound // first pos > val
  14. #define cnt_bit __builtin_popcount
  15. #define debug(...) " [" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] "
  16. //#pragma GCC optimize("Ofast")
  17. //#pragma GCC target("avx,avx2,fma")
  18. using namespace std;
  19.  
  20. mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
  21.  
  22. typedef long long ll;
  23. typedef pair<ll, ll> pll;
  24. typedef pair<int, int> pii;
  25.  
  26.  
  27. int d4x[4] = {1, 0, -1, 0}; int d4y[4] = {0, 1, 0, -1};
  28. int d8x[8] = {0, 1, 1, 1, 0, -1, -1, -1};
  29. int d8y[8] = {1, 1, 0, -1, -1, -1, 0, 1};
  30.  
  31. const ll oo = 1e18;
  32. const ll maxN = 1e6;
  33.  
  34. /* Author : Le Ngoc Bao Anh, 10A5, LQD High School for Gifted Student */
  35.  
  36. void maximize(int &a, int b) {
  37.     a = max(a, b);
  38. }
  39.  
  40. void minimize(int &a, int b) {
  41.     a = min(a, b);
  42. }
  43.  
  44. const int N = 2000050;
  45. int ans[N], a[N], pref[N];
  46. bitset<3335> dp[101][3335];
  47. pair<int, pii> qu[N * 30];
  48. bool vs[N][2];
  49. ll p[N];
  50. int n;
  51.  
  52. void dfs(int pos, int x, int y, int target) {
  53.     if(pos > n) return;
  54.     if(dp[n][target][target]) return;
  55.     if(dp[pos][x][y]) return;
  56.     dp[pos][x][y] = true;
  57.     if(x + a[pos + 1] <= target) dfs(pos + 1, x + a[pos + 1], y, target);
  58.     if(dp[n][target][target]) return;
  59.     if(y + a[pos + 1] <= target) dfs(pos + 1, x, y + a[pos + 1], target);
  60.     if(dp[n][target][target]) return;
  61.     if(pref[pos + 1] - x - y <= target) dfs(pos + 1, x , y, target);
  62. }
  63.  
  64. void backtrack(ll tot, int msk, ll g, int n, int cnt) {
  65.     if(vs[msk][tot == g]) return;
  66.     vs[msk][tot == g] = true;
  67.     if(msk == (1 << n) - 1) {
  68.         if(tot == g) {
  69.             for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  70.             exit(0);
  71.         }
  72.  
  73.         return;
  74.     }
  75.  
  76.     if(tot == g) backtrack(0, msk, g, n, cnt + 1);
  77.  
  78.     for(int i = 1; i <= n; i++) {
  79.         if(msk >> (i - 1) & 1) continue;
  80.         if(tot + a[i] <= g) {
  81.             ans[i] = cnt;
  82.             backtrack(tot + a[i], msk | (1 << (i - 1)), g, n, cnt);
  83.         }
  84.     }
  85. }
  86.  
  87. void solve() {
  88.     int k;
  89.     cin >> n >> k;
  90.  
  91.     ll S = 0;
  92.  
  93.     bool ok = true;
  94.     for(int i = 1; i <= n; i++) {
  95.         cin >> a[i];
  96.         S = S + a[i];
  97.         ok &= (a[i] == i);
  98.     }
  99.  
  100.     if(S % k != 0) {
  101.         cout << -1;
  102.         return;
  103.     }
  104.  
  105.     auto sub1 = [&]() -> void {
  106.         ll g = S / k;
  107.         vector<int> f;
  108.  
  109.         vector<bool> d((1 << n) + 10, false);
  110.         vector<int> prv((1 << n) + 10, 0);
  111.         for(int msk = 0; msk < (1 << n); msk++) {
  112.             ll tot = 0;
  113.             for(int i = 0; i < n; i++) {
  114.                 if(msk >> i & 1) {
  115.                     tot += a[i + 1];
  116.                 }
  117.             }
  118.  
  119.             if(tot == g) f.pb(msk);
  120.         }
  121.  
  122.         d[0] = true;
  123.         for(int msk = 0; msk < (1 << n); msk++) {
  124.             for(auto u : f) {
  125.                 if((u & msk) != u) continue;
  126.                 if(d[msk ^ u]) {
  127.                     d[msk] = true;
  128.                     prv[msk] = (u ^ msk);
  129.                     break;
  130.                 }
  131.             }
  132.         }
  133.  
  134.         if(!d[(1 << n) - 1]) {
  135.             cout << -1;
  136.             return;
  137.         }
  138.  
  139.         int msk = (1 << n) - 1;
  140.         int cnt = 0;
  141.         while(msk) {
  142.             cnt++;
  143.             int prev = prv[msk];
  144.             for(int i = 0; i < n; i++) {
  145.                 if((msk >> i & 1) && !(prev >> i & 1)) {
  146.                     ans[i + 1] = cnt;
  147.                 }
  148.             }
  149.             msk = prev;
  150.         }
  151.  
  152.         for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  153.     };
  154.  
  155.     if(n <= 10) {
  156.         sub1();
  157.         return;
  158.     }
  159.  
  160.     auto sub2 = [&]() -> void {
  161.         backtrack(0, 0, S / k, n, 1);
  162.         cout << -1;
  163.         return;
  164.     };
  165.  
  166.     if(n <= 20) {
  167.         sub2();
  168.         return;
  169.     }
  170.  
  171.     auto sub3 = [&]() -> void {
  172.         for(int i = 1; i <= n; i++) pref[i] = pref[i - 1] + a[i];
  173.         int g = S / 3;
  174.         dfs(0, 0, 0, g);
  175.         // dp[0][0][0] = true;
  176.         // int x = 0;
  177.         // for(int i = 1; i <= n; i++) {
  178.         //  x = x + a[i];
  179.         //  int d = min(x, (int)S / 3);
  180.         //  for(int j = d; j >= 0; j--) {
  181.         //      int y = x - j;
  182.         //      y = min(y, (int)S / 3);
  183.         //      for(int k = y; k >= 0; k--) {
  184.         //          if(dp[i - 1][j][k]) {
  185.         //              dp[i][j][k] = true;
  186.         //          }
  187.         //          if(j >= a[i] && dp[i - 1][j - a[i]][k]) {
  188.         //              dp[i][j][k] = true;
  189.         //          }
  190.         //          if(k >= a[i] && dp[i - 1][j][k - a[i]]) {
  191.         //              dp[i][j][k] = true;
  192.         //          }
  193.         //      }
  194.         //  }
  195.         // }
  196.  
  197.         if(!dp[n][S / 3][S / 3]) {
  198.             cout << -1;
  199.             return;
  200.         }
  201.  
  202.         int x = S / 3, y = S / 3, pos = n;
  203.         while(pos) {
  204.             int turn = 0;
  205.             if(x >= a[pos] && dp[pos - 1][x - a[pos]][y]) turn = 1;
  206.             else if(y >= a[pos] && dp[pos - 1][x][y - a[pos]]) turn = 2;
  207.             else turn = 3;
  208.             ans[pos] = turn;
  209.             if(turn == 1) x -= a[pos];
  210.             if(turn == 2) y -= a[pos];
  211.             pos--;
  212.         }
  213.  
  214.         for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  215.     };
  216.    
  217.     if(k == 3 && n <= 100) {
  218.         sub3();
  219.         return;
  220.     }
  221.  
  222.     auto sub4 = [&]() -> void {
  223.         if(n % 6 == 0) {
  224.             int mid = (n / 2);
  225.             int turn = 1;
  226.             for(int i = 1; i <= mid; i++) {
  227.                 ans[i] = ans[n - i + 1] = turn;
  228.                 turn++; if(turn > 3) turn = 1;
  229.             }
  230.  
  231.             for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  232.             return;
  233.         }
  234.  
  235.         if(n % 3 == 0) {
  236.             int mid = (n - 3) / 2;
  237.             int turn = 1;
  238.             for(int i = 1; i <= mid; i++) {
  239.                 ans[i] = ans[n - 3 - i + 1] = turn;
  240.                 turn++; if(turn > 3) turn = 1;
  241.             }
  242.  
  243.             swap(ans[n - 3], ans[n - 4]);
  244.             ans[n - 2] = 2;
  245.             ans[n - 1] = 3;
  246.             ans[n] = 1;
  247.             for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  248.             ll tot[4] = {0};
  249.             for(int i = 1; i <= n; i++) {
  250.                 tot[ans[i]] += i;
  251.             }
  252.             return;
  253.         }
  254.     };
  255.  
  256.     auto sub5 = [&]() -> void {
  257.         for(int i = 1; i <= k; i++) {
  258.             ll target = S / k;
  259.             for(int j = n; j >= 1; j--) {
  260.                 if(target >= a[j] && !ans[j]) {
  261.                     ans[j] = i;
  262.                     target -= a[j];
  263.                 }
  264.             }
  265.         }
  266.  
  267.         for(int i = 1; i <= n; i++) {
  268.             if(!ans[i]) {
  269.                 cout << -1;
  270.                 return;
  271.             }
  272.         }
  273.  
  274.         for(int i = 1; i <= n; i++) cout << ans[i] << " ";
  275.  
  276.     };
  277.  
  278.     if(k == 3 && (n % 3 == 0) && ok) {
  279.         sub4();
  280.         return;
  281.     }
  282.  
  283.     sub5();
  284. }
  285.  
  286. int main()
  287. {
  288.     ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  289.     #ifndef ONLINE_JUDGE
  290.     freopen("input.txt", "r", stdin);
  291.     freopen("output.txt", "w", stdout);
  292.     #else
  293.     //online
  294.     #endif
  295.  
  296.     int tc = 1, ddd = 0;
  297.     // cin >> tc;
  298.     while(tc--) {
  299.         //ddd++;
  300.         //cout << "Case #" << ddd << ": ";
  301.         solve();
  302.     }
  303. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement