从CF1744B学习鸽巢原理等trick

Problem - 1774B - Codeforces

1|0思路

First, We can divide n cells into nk 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 ai should be smaller than or equal to nk.

Then we can count the number of ai which is equal to nk. This number must be smaller than or equal to nmodk, 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 ai=nk, then we use color i to color the j-th cell in each segment.

Then we pick out all colors i that ai<nk1 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 nk 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 ai=nk1, and color them with the cyclic order.

This method will always give a valid construction.

2|0TRICK

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

    • n=8,k=4

      • 66666666
    • nk

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

    • 所以只要有 ai>nk​,就一定会有重复
    • 其他的都放入每个盒子中,如果数量大于 n 即最后一个盒子的数量,还是不行

3|0代码

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; }

__EOF__

本文作者Kdlyh
本文链接https://www.cnblogs.com/kdlyh/p/18046841.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   加固文明幻景  阅读(15)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 一文读懂知识蒸馏
· 终于写完轮子一部分:tcp代理 了,记录一下
点击右上角即可分享
微信分享提示