Codeforces 1196E. Connected Component on a Chessboard
注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况
不妨假设需要的白色格子比黑色格子少
那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周围
可以证明如果需要 $w$ 个白色格子,那么黑色格子的数量不能超过 $3w+1$
证明:首先 $w=1$ 时显然
然后考虑下一个白色格子扩展,显然从一边延伸出去,然后又多了 $3$ 个可以放黑色的位置,这样一路扩展显然可以放 $3w+1$ 个黑色的位置
然后考虑证明没有更好的方案,显然除了第一次白色可以提供四个黑色位置以外,之后放扩展白色时,至少要有其中一边和原本的黑色相邻,那么每次扩展多就只能多 $3$ 个合法位置
证明完毕
然后直接按构造的搞就行了,注意一下细节就行
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
inline int read()
{
int x=,f=; char ch=getchar();
while(ch<''||ch>'') { if(ch=='-') f=-; ch=getchar(); }
while(ch>=''&&ch<='') { x=(x<<)+(x<<)+(ch^); ch=getchar(); }
return x*f;
}
int Q,b,w;
inline bool pd(int x,int y) { return (x+y)&; }
int main()
{
Q=read();
while(Q--)
{
b=read(),w=read();
if(b>*w+||w>*b+) { printf("NO\n"); continue; }
printf("YES\n"); int px,py,l;
if(w>=b) px=py=;
else px=,py=;
l=py;
while(w||b)
{
if(pd(px,py))
{
if(!b) { py--; break; }
b--;
}
else
{
if(!w) { py--; break; }
w--;
}
printf("%d %d\n",px,py);
py++;
}
for(int i=l;i<=py;i++)
{
if(pd(px,i)&&w)
{
printf("%d %d\n",px-,i); w--;
if(w) printf("%d %d\n",px+,i),w--;
}
if((!pd(px,i))&&b)
{
printf("%d %d\n",px-,i); b--;
if(b) printf("%d %d\n",px+,i),b--;
}
}
if(b||w) printf("%d %d\n",px,py+);
}
return ;
}
Codeforces 1196E. 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个格子要连接在一起,不需要成环).问你可不可以找出来,如果可 ...
- Solution: 题解 CF1196E Connected Component on a Chessboard
感觉这题还可以 因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题 从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点 ...
- [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 ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [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 ...
- [HDU6271]Master of Connected Component
[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...
- 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 ...
随机推荐
- HDU 5115 Dire Wolf ——(区间DP)
比赛的时候以为很难,其实就是一个区间DP= =..思路见:点我. 区间DP一定要记住先枚举区间长度啊= =~!因为区间dp都是由短的区间更新长的区间的,所以先把短的区间更新完.. 代码如下: #inc ...
- js移动端触屏事件
移动端触屏滑动的效果其实就是图片轮播,在PC的页面上很好实现,绑定click和mouseover等事件来完成.但是在移动设备上,要实现这种轮播的效果,就需要用到核心的touch事件.处理touch事件 ...
- cocos creator发布win10全屏缩放问题
当前2.2.1版本cocos creator发布的win32版本不是全屏的, https://forum.cocos.org/t/win-exe/80429 https://blog.csdn.net ...
- JQUERY的$(function(){})和window.onload=function(){}的区别
在Jquery里面,我们知道入口函数有两种写法:$(function(){}) 和$(document).ready(function(){}) 作用类似于传统JavaScript中的window.o ...
- SQLite 版本引发的 Python 程序调用问题
问题 在跑 OpenStack functional 功能测试的时候有两个用例过不去. nova.tests.functional.db.test_resource_provider.Resource ...
- 小D课堂 - 零基础入门SpringBoot2.X到实战_第11节 Logback日志框架介绍和SpringBoot整合实战_45、SpringBoot2.x日志讲解和Logback配置实战
笔记 2.SpringBoot2.x日志讲解和自定义Logback配置实战 简介:讲解SpringBoot2.x整合Logback配置实战 1.官网介绍:https://docs.spring ...
- com.jniwrapper.util.ProcessorInfo
MyEclipse 9.x Crack报错 com.jniwrapper.util.ProcessorInfo usercode or systemid is empty! 卸载JDK9,改成JDK8
- python配置主机名
.准备hosts模板 mkdir -p /k8s/profile cat >/k8s/profile/hosts<<EOF 192.168.0.91 test1 192.168.0. ...
- nginx主配置文件实例
1.修改配置文件 重要:修改配置文件使用虚拟机,否则怎么配置都不生效,添加如下用户 [root@host-10-1-1-161 html]# ll /etc/nginx/nginx.conf -rw- ...
- 【JVM学习笔记】打破双亲委托机制的例子
Tomcat也有自己的类加载器,比如Servlet,这些类加载器就改变了双亲委托模型的默认机制 (该主题有待深入)