Solution: 题解 CF1196E Connected Component on a Chessboard
感觉这题还可以
因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题
从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点抽象)
来一发图解
作者因为太懒于是决定直接以B表示黑色,W表示白色
假设有一组方块拼成了一个正方形,如图
BWB
WBW
BWB
那么在不改变白块数量的情况下,最多还能加\(4\)个黑块,分别连在四个白块旁边
但是如果拉成直线,如图
BWBWBWBWB
发现可以在两边总共加\(8\)个黑块了,因为每个白块都可以再接\(2\)个黑块
所以拉成直线是最优方法(我有一个绝妙的证明但是这里写不下)
现在回到题目
如果\(b=w\),那么非常简单,直接画条直线
现在考虑\(b\not=w\)
不失一般性,直接假设\(b>w\),反正如果\(b<w\)的话整体右移一格即可
先画直线
直线会包含所有白块
为了尽量消耗黑块,让直线的两个端点都是黑块
比如说如果\(b>w=3\),那么直线应该长这个亚子
BWBWBWB
这样就先消耗掉\(w+1\)个黑块
对于剩下的\(b-w-1\)个黑块,就让它们分置在两边的总共\(2w\)个空间里面
所以如果\(b-w-1\leqslant2w\)那么就是可行的,否则不能
对于后面的具体处理方法就请大家自己思考吧,具体请见代码
Time complexity: \(O(\sum b+\sum w)\)
Memory complexity: \(O(1)\)
\(288\)ms / \(8.00\)KB
//This program is written by Brian Peng.
#pragma GCC optimize("Ofast","inline","no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define Rd(a) (a=read())
#define Gc(a) (a=getchar())
#define Pc(a) putchar(a)
int read(){
register int x;register char c(getchar());register bool k;
while(!isdigit(c)&&c^'-')if(Gc(c)==EOF)exit(0);
if(c^'-')k=1,x=c&15;else k=x=0;
while(isdigit(Gc(c)))x=(x<<1)+(x<<3)+(c&15);
return k?x:-x;
}
void wr(register int a){
if(a<0)Pc('-'),a=-a;
if(a<=9)Pc(a|'0');
else wr(a/10),Pc((a%10)|'0');
}
signed const INF(0x3f3f3f3f),NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL),LNINF(0xc3c3c3c3c3c3c3c3LL);
#define Ps Pc(' ')
#define Pe Pc('\n')
#define Frn0(i,a,b) for(register int i(a);i<(b);++i)
#define Frn1(i,a,b) for(register int i(a);i<=(b);++i)
#define Frn_(i,a,b) for(register int i(a);i>=(b);--i)
#define Mst(a,b) memset(a,b,sizeof(a))
#define File(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
int q,b,w,s;
signed main(){
Rd(q);
while(q--){
Rd(b),Rd(w);
if(b==w){
puts("YES");
Frn1(i,1,b<<1)Pc('1'),Ps,wr(i),Pe;
}else{
b<w?(swap(b,w),s=2):s=1;
if((b-=w+1)>w<<1){puts("NO");continue;};
puts("YES");
Frn1(i,0,w<<1)Pc('2'),Ps,wr(s+i),Pe;
if(b<=w)Frn0(i,0,b)Pc('1'),Ps,wr(s+(i<<1|1)),Pe;
else{
Frn0(i,0,w)Pc('1'),Ps,wr(s+(i<<1|1)),Pe;
b-=w;
Frn0(i,0,b)Pc('3'),Ps,wr(s+(i<<1|1)),Pe;
}
}
}
exit(0);
}
Solution: 题解 CF1196E Connected Component on a Chessboard的更多相关文章
- Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard(思维,构造)
E. Connected Component on a Chessboard time limit per test2 seconds memory limit per test256 megabyt ...
- Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard
传送门 题意: 给你一个黑白相间的1e9*1e9的棋盘,你需要从里面找出来由b个黑色的格子和w个白色的格子组成的连通器(就是你找出来的b+w个格子要连接在一起,不需要成环).问你可不可以找出来,如果可 ...
- Codeforces 1196E. Connected Component on a Chessboard
传送门 注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况 不妨假设需要的白色格子比黑色格子少 那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周 ...
- [LintCode] Find the Weak Connected Component in the Directed Graph
Find the number Weak Connected Component in the directed graph. Each node in the graph contains a ...
- [LintCode] Find the Connected Component in the Undirected Graph
Find the Connected Component in the Undirected Graph Find the number connected component in the undi ...
- Connected Component in Undirected Graph
Description Find connected component in undirected graph. Each node in the graph contains a label an ...
- Find the Weak Connected Component in the Directed Graph
Description Find the number Weak Connected Component in the directed graph. Each node in the graph c ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [HDU6271]Master of Connected Component
[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...
随机推荐
- Team Foundation Server 2015使用教程【7】:权限为读取器的团队成员连接tfs及checkin操作
- Android程序分析环境(搭建步骤略)
1:安装JDK JDK(Java Development Kit) 是 Java 语言的软件开发工具包(SDK).没有JDK的话,无法编译Java程序. 2:安装Android SDK Androi ...
- Android 最新架构
1.Android系统架构 最新的Android系统分为五层六区(域): Linux内核层(Linux Kernel):Android系统基于Linux2.6内核,这一层为Android设备各种硬件提 ...
- Nmap基本使用
Nmap Network Mapper 一款开源免费的网络发现和安全审计工具. 用途 列举网络主机清单 监控主机或服务运行状况 管理服务升级调度 检测目标主机是否在线 检测 ...
- springboot 动态修改定时任务
1.静态定时 1)启动类加上注解@EnableScheduling @EnableAsync @EnableScheduling @SpringBootApplication @MapperScan( ...
- jquery中报错Uncaught ReferenceError: $ is not defined的解决办法
jquery中报错提示为:Uncaught ReferenceError: $ is not defined 这个错误的原因就是你没有引入jquery库文件或者引入的路径不对造成的
- System类StringBuilder小结
- 【题解】剪纸条(dp)
[题解]剪纸条(dp) HRBUST - 1828 网上搜不到题解?那我就来写一篇吧哈哈哈 最优化问题先考虑\(dp\),设\(dp(i)\)表示将前\(i\)个字符(包括\(i\))分割成不相交的回 ...
- MATLAB实例:PCA(主成成分分析)详解
MATLAB实例:PCA(主成成分分析)详解 作者:凯鲁嘎吉 - 博客园 http://www.cnblogs.com/kailugaji/ 1. 主成成分分析 2. MATLAB解释 详细信息请看: ...
- RAID阵列
• 廉价冗余磁盘阵列– Redundant Arrays of Inexpensive Disks– 通过硬件/软件技术,将多个较小/低速的磁盘整合成一个大磁盘– 阵列的价值:提升I/O效率.硬件级别 ...