当前位置:网站首页>C. Color the Picture(贪心/构造)
C. Color the Picture(贪心/构造)
2022-07-29 21:24:00 【罗gkv】
题意
给定n * m的矩阵,用k种染料去涂这些格子。每种燃料有a[i]个。
要求涂抹后的每个格子,它的相邻格子,至少有3个是相同颜色。
这里相邻格子的定义比较特殊。对于(x1,y1)和(x2,y2),它们相邻,当且仅当满足以下其中之一。
比如对于下图,(1,2)的相邻格子是4个灰色结点。
思路
为了保证每个结点有3个同色的相邻结点,那么它至少有3个方向的格子是相同颜色的。
这意味着,我们构造的同色格子,要么是占据至少2行的,要么是占据至少2列的。
不失一般性,我们下面只讨论按列划分的情况。
问题转化为,给定m列,如果用k种燃料去填充每以列,使得每种燃料至少有填充2列以上。
- 为了尽量不浪费燃料,我们需要优先使用数量多的燃料。
- 为了能尽量利用每种燃料,我们需要尽量让每种燃料都用上。
为了满足第一条件,我们需要优先从数量大的燃料开始取;
为了满足第二条件,我们需要每种燃料都只取2列,多余的再额外记录下。
详见代码res和left变量。
代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f
const int maxn = 200010;
int n, m, k;
ll a[maxn];
void solve() {
scanf("%d%d%d", &n, &m, &k);
for (int i = 1; i <= k; ++i) {
scanf("%lld", &a[i]);
}
sort(a + 1, a + k + 1);// sort
bool ok = false;
// 1. color every col
ll res = 0, left = 0;
for (int i = k; i >= 1; --i) {
ll tmp = a[i] / n;
tmp = min(tmp, m - res);// 这里是防止剩余的列数不够
if (tmp <= 1) {
continue;
}
res += 2;
left += tmp - 2;// 多的染料存在left 备用
}
ok |= (res + left) >= m;
// 2. color every row
res = 0;left = 0;
for (int i = k; i >= 1; --i) {
ll tmp = a[i] / m;
tmp = min(tmp, n - res);
if (tmp <= 1) {
continue;
}
res += 2;
left += tmp - 2;
}
ok |= (res + left) >= n;
printf("%s\n", ok ? "YES" : "NO");
}
int main() {
int t;
scanf("%d", &t);
// t = 1;
while (t--) {
solve();
}
}
最后
weixin gongzhonghao搜 对方正在debug,关注下,一起快乐刷题吧~
边栏推荐
猜你喜欢

OneNote tutorial, how to take notes in OneNote?

转:idea中language level设置

Come in now!!!Take you to know the basic data types of C language

GBASE 8s 数据库的智能大对象备份

24小时伦敦金走势图分析

336. Palindromic Pairs

刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)
![LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode](/img/c2/34624c9c7693ba40d0b3724c0db611.png)
LeetCode 593 Valid Squares [Math] HERODING's Road to LeetCode

南华早报 | 助力亚洲最具公信力报章实现AD域自动化管理

Numpy数组处理(二)
随机推荐
leetcode-593:有效的正方形
容器网络硬核技术内幕 (24) 知微知彰,知柔知刚 (上)
【CVPR2022】A Unified Query-based Paradigm for Point Cloud Understanding
24小时伦敦金走势图分析
qt中qstring合并字符串
1. Promise usage in JS, 2. The concept and usage of closures, 3. The difference between the four methods and areas of object creation, 4. How to declare a class
Official announcement!Suzhou Wujiang Development Zone launches electronic labor contract platform
GBASE 8s 数据库的备份创建
刘畊宏男孩女孩看过来!运动数据分析挖掘!(附全套代码和数据集)
数字孪生万物可视 | 联接现实世界与数字空间
新库上线 | CnOpenData租赁和商务服务业工商注册企业基本信息数据
WeChat Mini Program 30 Customizing Templates and Obtaining User Login Credentials
对不起,你很难赚到中年人的钱
惠普服务器硬盘指示灯不亮或显示蓝色
The Ministry of Human Resources and Social Security announced that "database operation administrator" has become a new occupation, and OceanBase participated in the formulation of occupational standar
第3章业务功能开发(线索关联市场活动,动态搜索)
GBASE 8s PAM插入式身份验证模块
The implementation of the flood control project and safety construction project in the flood storage and detention areas in Luluze and Ningjinbo was launched
Come in now!!!Take you to know the basic data types of C language
给pdf添加已作废标识