Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define task "CIRCLE"
- #include <iostream>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- using namespace std;
- using ll = long long;
- using ld = long double;
- constexpr int N = 1e5 + 5;
- constexpr int Inf = 1e9 + 7;
- int id[] = {0, 1, 2};
- int n, a[N], b[N], ans[N], tmp[N];
- bool ck[N];
- vector<int> s[3];
- void Read()
- {
- cin >> n;
- for (int i = 0; i < n; ++i)
- cin >> a[i];
- }
- void Sub_1()
- {
- int x = 0, sum(0);
- do
- {
- sum += a[(x + 1) % n] - a[(x + 2) % n];
- (x += 3) %= n;
- b[x] = -sum;
- } while (x != 0);
- int minn = *min_element(b, b + n);
- for (int i = 0; i < n; ++i)
- cout << b[i] - minn + 1 << " ";
- }
- void Sub_2()
- {
- for (int i = 0; i < 3; ++i)
- {
- int x = i, sum(0);
- while (x < n)
- {
- s[i].emplace_back(x);
- b[x] = -sum;
- sum += a[(x + 1) % n] - a[(x + 2) % n];
- x += 3;
- }
- }
- do
- {
- fill(tmp, tmp + n, 0);
- fill(ck + 1, ck + n + 1, false);
- int mex(1);
- for (int i = 0; i < 3; ++i)
- {
- int minn(Inf);
- for (auto j : s[id[i]])
- minn = min(minn, b[j]);
- for (auto j : s[id[i]])
- {
- tmp[j] = b[j] + mex - minn;
- if (tmp[j] > n || ck[tmp[j]])
- goto done;
- ck[tmp[j]] = 1;
- }
- for (int j = 1; j <= n; ++j)
- if (!ck[j])
- {
- mex = j;
- break;
- }
- }
- for (int i = 0; i < n; ++i)
- if (ans[i] == 0 || ans[i] > tmp[i])
- {
- for (int j = 0; j < n; ++j)
- ans[j] = tmp[j];
- break;
- }
- else if (ans[i] < tmp[i])
- goto done;
- done:;
- } while (next_permutation(id, id + 3));
- for (int i = 0; i < n; ++i)
- cout << ans[i] << " ";
- }
- int32_t main()
- {
- ios::sync_with_stdio(0);
- cin.tie(0);
- cout.tie(0);
- if (fopen(task ".INP", "r"))
- {
- freopen(task ".INP", "r", stdin);
- freopen(task ".OUT", "w", stdout);
- }
- Read();
- if (n % 3 != 0)
- Sub_1();
- else
- Sub_2();
- }
Advertisement
Add Comment
Please, Sign In to add comment