传送门

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

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

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

可以证明如果需要 $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. 基于Redis的分布式锁安全性分析-转

    基于Redis的分布式锁到底安全吗(上)?  2017-02-11 网上有关Redis分布式锁的文章可谓多如牛毛了,不信的话你可以拿关键词“Redis 分布式锁”随便到哪个搜索引擎上去搜索一下就知道了 ...

  2. 遍历 ArrayList与Map集合

    import java.util.ArrayList; import java.util.Iterator; import java.util.List; /** * 遍历 ArrayList * @ ...

  3. BeanFactory和ApplicationContext的区别+部分Spring的使用

    BeanFactory和ApplicationContext的区别 ApplicationContext 方式加载:创建容器的同时 容器初始化,容器所有的bean创建完毕   Spring容器中获取一 ...

  4. excel中如何设置只打印第一页

    在打印表格时,怎样设置只打印第一页呢,操作很简单,下面,小编说下操作方法.   方法/步骤     打开要打印的工作表, 再点击“文件”   弹出的页面中,在左侧这里,点击“打印”   在右边弹出与打 ...

  5. mintUI 移动UI框架入门

    入门地址: http://mint-ui.github.io/#!/zh-cn 下载依赖cd到项目目录下, 下载我们用的UI框架: 分为全局引入和按需引入 全局引入: npm install mint ...

  6. 代码实现:获取一个文本上每个字符出现的次数,将结果写在times.txt上

    package com.loaderman.test; import java.io.BufferedReader; import java.io.BufferedWriter; import jav ...

  7. javascript中ClassName属性的详解与实例

    在javascritp中,我们可以通过style属性可以控制元素的样式,从而实现行为层通过DOM的style属性去干预显示层显示的目标,但是这种方法是不好的,而且为了实现通过DOM脚本设置的样式,你不 ...

  8. 在Linux上安装Python3.7.1

    一.安装依赖环境 输入命令:yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readlin ...

  9. 2019.11.18【每天学点SAP小知识】Day4 - ABAP 7.40新语法 FOR

    "今天学习一下FOR的语法,常用的2个语法. FOR wa|<fs> IN itab [INDEX INTO idx] [cond] "FOR i = … [THEN ...

  10. IPV6测试方法

    终端 dig +nocmd + nostats 你的域名 AAAA: 查看Got answer 如果 status的状态是NO ERROR 那就是支持IPV6 就没啥问题. 如果status 的状态是 ...