感觉这题还可以

因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题

贪心的角度考虑,如果要使相差数量巨大的\(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的更多相关文章

  1. 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 ...

  2. Codeforces Round #575 (Div. 3) E. Connected Component on a Chessboard

    传送门 题意: 给你一个黑白相间的1e9*1e9的棋盘,你需要从里面找出来由b个黑色的格子和w个白色的格子组成的连通器(就是你找出来的b+w个格子要连接在一起,不需要成环).问你可不可以找出来,如果可 ...

  3. Codeforces 1196E. Connected Component on a Chessboard

    传送门 注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况 不妨假设需要的白色格子比黑色格子少 那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周 ...

  4. [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 ...

  5. [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 ...

  6. Connected Component in Undirected Graph

    Description Find connected component in undirected graph. Each node in the graph contains a label an ...

  7. 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 ...

  8. algorithm@ Strongly Connected Component

    Strongly Connected Components A directed graph is strongly connected if there is a path between all ...

  9. [HDU6271]Master of Connected Component

    [HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...

随机推荐

  1. oracle解除被锁定的表的状态

      select b.owner,b.object_name,a.session_id,a.locked_mode,c.serial#,c.sid||','||c.serial# from v$loc ...

  2. CentOS7.6部署k8s环境

    CentOS7.6部署k8s环境 测试环境: 节点名称 节点IP 节点功能 K8s-master 10.10.1.10/24 Master.etcd.registry K8s-node-1 10.10 ...

  3. Visio数据视觉化处理

    形状数据的查看的两种方式 定义形状数据:右键单击数据窗口 打勾的代表可以显示 其他没有打勾的就必须要在开发模式才会显示出来 开发模式就是打开开发工具面板 注意其中类型的设置 类型与格式是一一对应的 不 ...

  4. VS2017+QT5.11.2+SeetaFace1.0/SeetaFace2.0的简单实现

    SeetaFace开源引擎GitHub地址:https://github.com/seetaface/SeetaFaceEngine SeetaFace2开源引擎GitHub地址:https://gi ...

  5. 010 Ceph RGW对象存储

    一.对象存储 1.1 介绍 通过对象存储,将数据存储为对象,每个对象除了包含数据,还包含数据自身的元数据 对象通过Object ID来检索,无法通过普通文件系统操作来直接访问对象,只能通过API来访问 ...

  6. Visual Studio 2017 安装心得

    既然VS2017已经发布了,就想安装一下试试,先卸载VS2015, 网上有个完全卸载的东东,https://github.com/Microsoft/VisualStudioUninstaller/r ...

  7. 【题解】Comet OJ Round 70 简要题解

    [题解]Comet OJ Round 70 简要题解 A 将放在地上的书按照从小到大排序后,问题的本质就变成了合并两个序列使得字典序最小.可以直接模拟归并排序.直接用循环和std::merge实现这个 ...

  8. $Poj1037\ A\ Decorative\ Fence$ 计数类$DP$

    Poj  AcWing Description Sol 这题很数位$DP$啊, 预处理$+$试填法 $F[i][j][k]$表示用$i$块长度不同的木板,当前木板(第$i$块)在这$i$块木板中从小到 ...

  9. HelloTalk 基于 OpenResty 的全球化探索之路

    2019 年 12 月 14 日,又拍云联合 Apache APISIX 社区举办 API 网关与高性能服务最佳实践丨Open Talk 广州站活动,HelloTalk, Inc. 后台技术负责人李凌 ...

  10. day2(使用list和tuple)

    list list是一种有序的集合 >>>aaa = ['abc','bob','tracy'] >>>aaa ['abc','bob','tracy'] len( ...