传送门

注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况

不妨假设需要的白色格子比黑色格子少

那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周围

可以证明如果需要 $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的更多相关文章

  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. Solution: 题解 CF1196E Connected Component on a Chessboard

    感觉这题还可以 因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题 从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点 ...

  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. algorithm@ Strongly Connected Component

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

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

  7. [HDU6271]Master of Connected Component

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

  8. Connected Component in Undirected Graph

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

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

随机推荐

  1. java web过滤器防止未登录进入界面

    import java.io.IOException; import javax.servlet.Filter; import javax.servlet.FilterChain; import ja ...

  2. linux vmware 安装步骤

    一.下载vmware软件 二.下载centos镜像文件 三.安装步骤 以上相当于于硬件设备已经准备ok,接下来安装软件

  3. 【黑马JavaSE】1.2.算术\赋值\比较\逻辑\三元运算符、方法入门、JShell编译器

    文章目录 1_1_6_05_算术运算符_四则与取模运算 1_1_6_06_算术运算符_加号的多种 1_1_6_07_算术运算符_自增自减运算 1_1_6_08_赋值运算符 这里挺关键的,为什么一个by ...

  4. oc 基本语法 类 静态变量 常量

    // // ReViewClass.h // hellowWorld // 本类是oc复习练手类 // Created by hongtao on 2018/3/26. // Copyright © ...

  5. 网络通信框架之volley

    介绍 我们平时在开发Android应用的时候不可避免地都需要用到网络技术,而多数情况下应用程序都会使用HTTP协议来发送和接收网络数据.Android系统中主要提供了两种方式来进行HTTP通信,Htt ...

  6. MVC和MVP区别

    从这幅图可以看到,我们可以看到在MVC里,View是可以直接访问Model的!从而,View里会包含Model信息,不可避免的还要包括一些业务逻辑. 在MVC模型里,更关注的Model的不变,而同时有 ...

  7. springboot实现内存缓存

    题记:实现缓存大部分可以使用redis实现,简单.便捷,redis在针对应用部署多服务器是很好的,但如果针对单一服务器,内存缓存更好. 1.创建CacheLoader.java import java ...

  8. AWS EC2 外网不能访问的坑

    概述 今天我在 AWS EC2 上配置并启动了 nginx,但是通过外网不能访问,查了一下资料终于解决了,记录下来供以后开发时参考,相信对其它人也有用. 外网访问不了的原因 外网访问不了的原因不外乎有 ...

  9. Ironic 裸金属实例的部署流程

    目录 文章目录 目录 逻辑架构 部署架构 前提条件 部署流程 iSCSI Deploy UML PXE Deploy Driver Direct Deploy UML IPA Deploy Drive ...

  10. Access denied for user XX

    解决方法:将pom.xml中的mysql-connector-java降低版本(直接去maven仓库复制:https://mvnrepository.com/),比如: <!-- https:/ ...