从CF1744B学习鸽巢原理等trick

Problem - 1774B - Codeforces

思路

First, We can divide \(n\) cells into \(\left\lceil\frac{n}{k}\right\rceil\) segments that except the last segment, all segments have length \(k\). Then in each segment, the colors in it are pairwise different. It's easy to find any \(a_i\) should be smaller than or equal to \(\left\lceil\frac{n}{k}\right\rceil\).

Then we can count the number of \(a_i\) which is equal to \(\left\lceil\frac{n}{k}\right\rceil\). This number must be smaller than or equal to \(n \bmod k\), which is the length of the last segment.

All \(a\) that satisfies the conditions above is valid. We can construct a coloring using the method below:

First, we pick out all colors \(i\) that \(a_i=\left\lceil\frac{n}{k}\right\rceil\), then we use color \(i\) to color the \(j\)-th cell in each segment.

Then we pick out all colors \(i\) that \(a_i<\left\lceil\frac{n}{k}\right\rceil-1\) and use these colors to color the rest of cells with cyclic order(i.e. color \(j\)-th cell of the first segment, of second the segment ... of the \(\left\lceil\frac{n}{k}\right\rceil\) segment, and let \(j+1\). when one color is used up, we begin to use the next color)

At last, we pick out all colors \(i\) that \(a_i=\left\lceil\frac{n}{k}\right\rceil-1\), and color them with the cyclic order.

This method will always give a valid construction.

TRICK

  • 长度为 \(n\),能分成长度为 \(k\)​​ 连续段的数量

    • \(n = 8, k = 4\)

      • \(6666 6666\)
    • \(\left\lceil\frac{n}{k}\right\rceil\)

      • (n + k - 1) / k//向上取整通法
        
  • ((n - 1) % k + 1)//即n % k == 0 ? n : n % k
    
  • 鴿巢原理

    • 所以只要有 \(a_i > \left\lceil\frac{n}{k}\right\rceil\)​,就一定会有重复
    • 其他的都放入每个盒子中,如果数量大于 \(n % k\) 即最后一个盒子的数量,还是不行

代码

void solve() {
    int n, m, k;
    std::cin >> n >> m >> k;
    int tot(0);
    int cnt_seg((n + k - 1) / k);
    for (int i = 0, x; i < m; i++) {
        std::cin >> x;
        if (x > cnt_seg) tot = 1 << 30;
        else if (x == cnt_seg) tot++;
    }
    tot <= ((n - 1) % k + 1) ? YES : NO;
}
posted @ 2024-03-01 13:45  加固文明幻景  阅读(11)  评论(0编辑  收藏  举报