AGC064A
题意简述:
给出
n
n
n,一个长度为
n
×
(
n
+
1
)
/
2
n \times (n+1) / 2
n×(n+1)/2 的数字,他包含
i
(
1
≤
i
≤
n
)
i(1 \leq i \leq n)
i(1≤i≤n) 个
i
i
i,求他的一个排列,使得把这个排列放在环上满足相邻两个数的差不超过
2
2
2 且大于等于
1
1
1。
我们首先把那些差为 1 1 1 的先排好,所以我们先从 n n n 开始,先减后增,当然也可以从 1 1 1 开始,先增后减,直到第二大的数已经被选完了,那么我们对于剩下的一些 n n n,我们尝试那他们插入到这个差为 1 1 1 的序列中,所以我们直接输出这个插入后的序列即可。
Code:
#include <bits/stdc++.h>
#define pb push_back
#define ll long long
using namespace std;
void tomin(int& x, int y){x = min(x, y);}
void tomax(int& x, int y){x = max(x, y);}
const int maxn = 1005, maxm = 1e6 + 5;
int n, now, c[maxn], Ans[maxm], tot;
int main() {
ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n;
now = n;
int f = 1;
while (true) {
if (c[n - 1] == n-1) break;
Ans[++tot] = now;
c[now]++;
if (!f) {
if (c[now + 1] < now + 1 && now+1 <= n){
now++;
} else {
f ^= 1;
now--;
}
}
else {
if(c[now - 1] < now - 1 && now-1 >= 1) {
now--;
} else {
f ^= 1;
now++;
}
}
}
int las = n - c[n];
for (int i = 1; i <= tot; i ++ ) {
if (las > 0) {
if (abs(n - Ans[i - 1]) >= 1 && abs(n - Ans[i - 1]) <= 2 && abs(n - Ans[i]) >= 1 && abs(n - Ans[i]) <= 2)
cout << n << ' ' << Ans[i] << ' ', las--;
else
cout << Ans[i] << ' ';
} else {
cout << Ans[i] << ' ';
}
}
return 0;
}