跳到正文

题解

[ARC131E] Christmas Wreath 题解

Egg_eating_master约 3 分钟
原文
文章信息与版本

文章信息

发布
2023年3月21日
收录
2026年10月12日
更新
2026年11月8日
篇幅
约 3 分钟
获赞
11

版本

  1. v1当前

首先当 n≤4n\le 4 或 n≡2(mod3)n\equiv 2\pmod 3 时显然无解。

考虑怎么保证不存在彩色三角形。对于每个 ii,把边 (1∼i−1,i)(1\sim i-1,i) 分成一类,同一类的边染相同的颜色。这样对于任意三个点 a<b<ca<b<c,边 (a,c)(a,c) 和 (b,c)(b,c) 都一定是同色的。

接下来保证三种颜色的边数量相同,问题转化为把 1,2,⋯ ,n−11,2,\cdots, n-1 分成三组,使得每组内的数之和相等。

先从边界情况开始考虑:

  • 当 n=6n=6 时,分成 {1,4},{2,3},{5}\{1,4\},\{2,3\},\{5\};
  • 当 n=7n=7 时,分成 {1,6},{2,5},{3,4}\{1,6\},\{2,5\},\{3,4\};
  • 当 n=9n=9 时,分成 {1,2,3,6},{4,8},{5,7}\{1,2,3,6\},\{4,8\},\{5,7\};
  • 当 n=10n=10 时,分成 {1,2,3,4,5},{6,9},{7,8}\{1,2,3,4,5\},\{6,9\},\{7,8\};

当 n>10n>10 时,考虑递推构造,把 [n−6,n−1][n-6,n-1] 首尾分成三组,然后转化为 n−6n-6 的子问题。

于是就做完了。

C++
#include<bits/stdc++.h>
using namespace std;
const int maxn = 55;
int n;
int ans[maxn];
int p;
char c[4] = {'#', 'R', 'W', 'B'};
void work5() {ans[1] = 1; ans[2] = 2; ans[3] = 2; ans[4] = 1; ans[5] = 3;}
void work8() {
    ans[1] = ans[2] = ans[3] = ans[6] = 1;
    ans[4] = ans[8] = 2; ans[5] = ans[7] = 3;
}
void work9() {
    ans[1] = ans[2] = ans[3] = ans[4] = ans[5] = 1;
    ans[6] = ans[9] = 2; ans[7] = ans[8] = 3;
}
int main() {
    cin >> n; n--;
    if (n % 3 == 1 || n <= 4) {cout << "No" << endl; return 0;}
    if (n % 6 == 5) work5(), p = 6;
    if (n % 6 == 2) work8(), p = 9;
    if (n % 6 == 3) work9(), p = 10;
    if (n % 6 == 0) p = 1;
    for (int i = p; i <= n; i += 6) {
        ans[i] = ans[i + 5] = 1;
        ans[i + 1] = ans[i + 4] = 2;
        ans[i + 2] = ans[i + 3] = 3;
    }
    cout << "Yes" << endl;
    for (int i = n; i >= 1; i--, cout << endl)
        for (int j = 1; j <= i; j++)
            cout << c[ans[i]];
    return 0;
}
原文