首页 > 其他分享 >CF1774B-Coloring

CF1774B-Coloring

时间:2023-01-04 21:12:07浏览次数:36  
标签:Cirno ch 鸽笼 cells 9baka CF1774B Coloring 原理

挺有意思的一个思维题

题面翻译

Cirno_9baka 的纸条上有 \(n\) 个格子,他觉得空白的纸条看着有点无趣,于是想在纸条的格子上涂上 \(m\) 种颜色。同时,他认为第 \(i\) 种颜色必须要用 \(a_i\) 次,且每连续 \(k\) 个格子里涂的颜色必须互不相同。

Cirno_9baka 想知道有没有这样的一种涂色方案能符合他的要求。

Cirno_9baka has a paper tape with $ n $ cells in a row on it. As he thinks that the blank paper tape is too dull, he wants to paint these cells with $ m $ kinds of colors. For some aesthetic reasons, he thinks that the $ i $ -th color must be used exactly $ a_i $ times, and for every $ k $ consecutive cells, their colors have to be distinct.

Help Cirno_9baka to figure out if there is such a way to paint the cells.

分析

这里用到了鸽笼原理 (鸽巢原理、抽屉原理、狄利克雷原理),鸽笼原理容易理解,但是活用并不容易。

将 \(n\) 个元素的集合划分成 \(m\) 个集合,则其中至少有一个集合中含有大于等于 \(\left\lceil\dfrac{n}{m}\right\rceil\)。

那么活用鸽笼原理,一定要记住这一点:分清楚谁是苹果谁是抽屉。

这题的抽屉便是 \(k\),\(k\) 的长度为一段,这一段内不能有重复。

那么我们可以算出段数,\(v = \left\lceil\dfrac{n}{k}\right\rceil\),这个段数上取整的意义是为了包括最后一段。即每段都是 \(k\) 段的段数加上模 \(k\) 的余数 \(r\),\(r\) 就是最后一段内的数量 \(0\le r<k\)。

那么结合 \(a_i\) 考虑。

(1)如果 \(\exists a_i > v\),则无法构造出合法序列,最多只有 \(c\) 个盒子,\(a_i>v\) 意味着一定有一个盒子中的数量 \(\ge 2\),这里面蕴含了鸽笼原理的思想;

(2)如果有 \(q\) 个 \(a_i=v\),且 \(q > r\),也无法构造出合法序列,因为这 \(q\) 个 \(a_i\) 都是要往最后一段里加一个 \(1\) 的,所以当 \(q>r\) 时,必定有一个盒子重复。

(3)除此之外都可行。

#include <bits/stdc++.h>

inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();} while(ch>='0' && ch<='9')x=(x<<3)+(x<<1)+ch-'0',ch=getchar();return x*f;}

using namespace std;

int main()
{
	int T = read();
	int n,m,k;
	while (T--) {
		n = read(), m = read(), k = read();
		int v = int(ceil(n*1.0/k)), a, f = 0, c = 0;
        // v 的取整特别严格,只有这么写才不会挂分
		for (int i = 1; i <= m; i++) {
			a = read();
			if (a > v) f = 1;
			if (a == v) c++;
		}
		if (f == 1) printf("NO\n");
		else if (c > (n  - 1) % k + 1) printf("NO\n");
		else printf("YES\n");
	}
    return 0;
}

关注程序的第21行,(n - 1) % k + 1 这个处理很妙。如果我们写 n%k 则是值域在 \([0,k-1]\),而前者是 \([1,k]\)。我们可以写成 n%k==0?n:n%k。那么这里为什么要这么写呢?因为当 \(k|n\) 时,\(n\) 被恰好分为 \(k\) 段,那么这时就没有多余的 \(r\) 也就无需这样操作。

标签:Cirno,ch,鸽笼,cells,9baka,CF1774B,Coloring,原理
From: https://www.cnblogs.com/CYLSY/p/17026003.html

相关文章

  • UVA193 Graph Coloring - 一般图最大独立集 -
    题目链接:https://www.luogu.com.cn/problem/UVA193题解:注意不是二分图最大独立集和最大匹配没啥关系直接dfs//bySkyRainWind#include<bits/stdc++.h>#definempr......
  • 题解【CF1592F2 Alice and Recoloring 2】
    CF1592F2AliceandRecoloring2解题报告。不一定更好的阅读体验。摘自我的构造题目选做例题IV。CF2800的构造就这?/cf/cf/cf(首先,操作2和操作3都是没有用......
  • AtCoder Grand Contest 025B - RGB Coloring
    题解:一开始想把AA,BB,AA+B......
  • CF1027E Inverse Coloring
    考场上没敲出来(其实想到就挺好写的捏组数据手动模拟一下:\(\begin{bmatrix}1&0&1&1&1&0&1\\1&0&1&1&1&0&1\\0&1&0&0&0&1&0\\1&0&1&1&1&0&1\\0&1&0&0&0&1&0\\0......
  • CF149D Coloring Brackets
    题意:给出一串合法括号,按以下规则给括号染色:1.每个符号可以染红色、蓝色,或者不染色;2.相邻两个符号不能染同一种颜色,可以都不染色;3.一对括号有且仅有一个符号染色。求......
  • 【XSY3270】Domino Colorings(轮廓线dp,状压)
    若已经知道了每个格子的颜色,我们可以用轮廓线DP(类似插头DP)判断棋盘是否能被多米诺骨牌填满,设\(dp[S]\)表示是否存在某种填法使得轮廓线每个位置是否被填的状态为\(S\)......
  • CF1329A Dreamoon Likes Coloring 题解
    提供一个简短的题解:首先如果所有长度加起来还不到\(n\)直接无解。可以直接贪心,把第\(i\)条线段的右端点放在\(n-i+1\)这个位置,就可以最省长度(只占一个点)而且不会遗......
  • CF149D Coloring Brackets
    #include<bits/stdc++.h>usingnamespacestd;#defineintlonglongclasssolve{ public: chars[777]; intf[1000][1000][3][3]; intmod; intothers[1000......