ZROJ3318 划分
python_stripe · · 个人记录
ZROJ3318 划分
现有数列
a=[1,2,\cdots,4n+2] 。称点对(i,j) 为好的,当且仅当1 \le i<j\le 4n+2 且在a 中删去i,j 后,剩余4n 个数可以划分为n 个长为4 的等差数列。输出所有的好点对,并分别构造划分方案。
部分分:记
k=\binom{4n+2}{2} ,点对数量超过\frac{k}{16} 获得20\% 分数;点对数量超过\frac{k}{8} 获得60\% 分数。多测,
\sum n \le 200 ,3 秒,512 MB。
:::::success[题解]
显然
通过手玩
观察下标,我们可以发现
一类点的数目共有
观察
如果你注意力不好,你也可以使用下文的 DFS 来得到这个观察。
上述发现的意义在于,如果我们钦定了两个位置
如图所示,橙色部分可以等价于
因此
此时点对数目
::::info[代码]
int n;
using node = pair<int, int>;
vector<node> A, B;
void _main() {
cin >> n;
A.clear(), B.clear();
for (int a = 0; a <= n; a++) {
for (int b = a; b <= n; b++) A.emplace_back(4 * a + 1, 4 * b + 2);
}
for (int a = 0; a <= n; a++) {
for (int b = a + 2; b <= n; b++) B.emplace_back(4 * a + 2, 4 * b + 1);
}
cout << A.size() + B.size() << '\n';
for (auto [x, y] : A) {
printf("( %d , %d )", x, y);
for (int i = 1; i <= n; i++) printf(" %d %d %d %d", i, i, i, i);
cout << '\n';
}
for (auto [x, y] : B) {
printf("( %d , %d )", x, y);
int c = 0, len = (y - x + 3) / 4;
for (int i = 1; i <= x - 2; i += 4) c++, printf(" %d %d %d %d", c, c, c, c);
for (int i = x - 1; i <= y + 1; i++) {
if (i == x || i == y) continue;
printf(" %d", c + i % len + 1);
}
c += len;
for (int i = y + 2; i <= 4 * n + 2; i += 4) c++, printf(" %d %d %d %d", c, c, c, c);
cout << '\n';
}
}
::::
思考问题出现在哪里。我们写一个 DFS,在
进一步对
对于
观察
( 2 , 5 ) 1 2 1 3 1 4 3 1 4 3 2 4 3 5 4 6 6 6 6 5 2 7 7 7 7 5 8 8 8 8 2 5
( 6 , 9 ) 1 2 1 3 1 1 2 3 4 5 4 2 4 3 4 5 6 2 6 3 6 5 6 7 7 7 7 5 8 8 8 8
( 10 , 13 ) 1 1 1 1 2 3 2 4 2 2 3 4 5 6 5 3 5 4 5 6 7 3 7 4 7 6 7 8 8 8 8 6
( 14 , 17 ) 1 1 1 1 2 3 4 3 5 3 4 3 2 4 5 6 4 6 2 6 5 6 7 7 7 7 2 5 8 8 8 8
( 18 , 21 ) 1 1 1 1 2 2 2 2 3 4 5 4 6 4 5 4 3 5 6 7 5 7 3 7 6 7 8 8 8 8 3 6
( 22 , 25 ) 1 1 1 1 2 2 2 2 3 4 5 5 5 5 6 3 6 4 6 7 6 3 7 4 8 7 8 3 8 7 8 4
( 26 , 29 ) 1 1 1 1 2 3 2 4 2 4 2 4 5 4 3 6 5 6 7 6 5 6 7 3 5 7 8 8 7 8 3 8
( 30 , 33 ) 1 2 3 3 3 3 1 4 4 4 4 2 1 5 5 5 5 6 1 7 6 2 7 6 8 7 6 8 7 8 2 8
排除公差为
根据对称性,
对于长为
将三种构造方法打表到代码中,并根据
| 提交后发现上述做法达到了精确上界,可以通过。 |
|---|
:::::info[代码]
int n;
using node = pair<int, int>;
vector<node> A, B, C;
using vecw = vector<array<int, 4>>;
vecw way;
bool dfs(vector<int> vec) {
auto lb = [&](int x) -> vector<int>::iterator {
auto it = lower_bound(vec.begin(), vec.end(), x);
return it == vec.end() ? it : (*it == x ? it : vec.end());
};
if (vec.size() == 4) {
if (vec[1] - vec[0] != vec[2] - vec[1]) return false;
if (vec[2] - vec[1] != vec[3] - vec[2]) return false;
way.push_back({vec[0], vec[1], vec[2], vec[3]});
return true;
}
vector<int> pre(vec.begin(), vec.end());
for (int d = 1; d <= 4 * n; d++) {
int v1 = vec[0], v2 = vec[0] + d, v3 = vec[0] + 2 * d, v4 = vec[0] + 3 * d;
if (lb(v2) == vec.end() || lb(v3) == vec.end() || lb(v4) == vec.end()) continue;
vec.erase(vec.begin()), vec.erase(lb(v2)), vec.erase(lb(v3)), vec.erase(lb(v4));
way.push_back({v1, v2, v3, v4});
if (dfs(vec)) return true;
vec = pre, way.pop_back();
}
return false;
}
void bf() {
vector<pair<node, vecw>> ans;
for (int i = 1; i <= 4 * n + 2; i++) {
for (int j = i + 1; j <= 4 * n + 2; j++) {
vector<int> vec;
for (int k = 1; k <= 4 * n + 2; k++) {
if (k != i && k != j) vec.emplace_back(k);
}
way.clear();
if (!dfs(vec)) continue;
ans.emplace_back(node(i, j), way);
}
}
cout << ans.size() << '\n';
for (auto [pir, way] : ans) {
auto [x, y] = pir;
// if (y - x == 3) puts("Point 3: ");
printf("( %d , %d )", x, y);
vector<int> bel(4 * n + 3);
for (int i = 0; i < (int)way.size(); i++) {
for (int u : way[i]) bel[u] = i + 1;
}
for (int i = 1; i <= 4 * n + 2; i++) {
if (i == x || i == y) continue;
printf(" %d", bel[i]);
}
cout << '\n';
}
}
void outAB() {
for (auto [x, y] : A) {
printf("( %d , %d )", x, y);
for (int i = 1; i <= n; i++) printf(" %d %d %d %d", i, i, i, i);
cout << '\n';
}
for (auto [x, y] : B) {
printf("( %d , %d )", x, y);
int c = 0, len = (y - x + 3) / 4;
for (int i = 1; i <= x - 2; i += 4) c++, printf(" %d %d %d %d", c, c, c, c);
for (int i = x - 1; i <= y + 1; i++) {
if (i == x || i == y) continue;
printf(" %d", c + i % len + 1);
}
c += len;
for (int i = y + 2; i <= 4 * n + 2; i += 4) c++, printf(" %d %d %d %d", c, c, c, c);
cout << '\n';
}
}
const int X[] = {0, 1, 0, 2, 1, 0, 3, 1, 4, 3, 1, 4, 3, 2, 4, 3, 5, 4,
6, 6, 6, 6, 5, 2, 7, 7, 7, 7, 5, 8, 8, 8, 8, 2, 5};
const int Y[] = {0, 1, 2, 1, 3, 1, 0, 1, 2, 0, 3, 4, 5, 4, 2, 4, 3, 4,
5, 6, 2, 6, 3, 6, 5, 6, 7, 7, 7, 7, 5};
const int Z[] = {0, 1, 2, 3, 2, 4, 2, 3, 2, 1, 0, 3, 4, 0, 5, 3, 5, 1,
5, 4, 5, 6, 6, 6, 6, 1, 4};
void outC() {
for (auto [x, y] : C) {
printf("( %d , %d )", x, y);
int a = (x - 2) / 4, c = 0;
vector<int> bel(4 * n + 10);
auto norm = [&](int l, int r) -> void {
for (int i = l; i <= r; i += 4) fill(bel.begin() + i, bel.begin() + i + 4, c), c++;
};
if (a == 0) {
copy(X, X + 35, bel.begin());
c = 9, norm(35, 4 * n + 2);
} else if (a == 1) {
copy(Y, Y + 31, bel.begin());
c = 8, norm(31, 4 * n + 2);
} else if (a == n - 1) {
int st = 4 * n - 31;
c = 1, norm(1, st - 1);
for (int i = 1; i <= 34; i++) bel[st + i - 1] = c - 1 + X[35 - i];
} else if (a == n - 2) {
int st = 4 * n - 27;
c = 1, norm(1, st - 1);
for (int i = 1; i <= 30; i++) bel[st + i - 1] = c - 1 + Y[31 - i];
} else if (a == n - 3) {
int st = 4 * n - 23;
c = 1, norm(1, st - 1);
for (int i = 1; i <= 26; i++) bel[st + i - 1] = c - 1 + Z[27 - i];
} else {
int st = 4 * a - 7;
c = 1, norm(1, st - 1);
for (int i = 1; i <= 26; i++) bel[st + i - 1] = c - 1 + Z[i];
c += 6, norm(st + 26, 4 * n + 2);
}
for (int i = 1; i <= 4 * n + 2; i++) {
if (i == x || i == y) continue;
printf(" %d", bel[i]);
}
cout << '\n';
}
}
void _main() {
cin >> n;
if (n == 6 || n == 7 || n == 8) return bf(), void();
A.clear(), B.clear(), C.clear();
for (int a = 0; a <= n; a++) {
for (int b = a; b <= n; b++) A.emplace_back(4 * a + 1, 4 * b + 2);
}
for (int a = 0; a <= n; a++) {
for (int b = a + 2; b <= n; b++) B.emplace_back(4 * a + 2, 4 * b + 1);
}
for (int a = 0; a < n; a++) C.emplace_back(4 * a + 2, 4 * a + 5);
if (n <= 5) {
cout << A.size() + B.size() << '\n';
outAB();
} else {
cout << A.size() + B.size() + C.size() << '\n';
outAB(), outC();
}
}
:::::