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 ...
随机推荐
- [CSP-S模拟测试]:凉宫春日的忧郁(乱搞)
题目传送门(内部题101) 输入格式 第一行输入一个整数$T$,表示数据组数. 接下来$T$行,每行两个数$X,Y$,表示$T$组数据. 输出格式 输出共有$T$行,对于每一组数据,如果$X^Y\le ...
- [洛谷P3941]:入阵曲(前缀和+桶)
题目传送门 题目背景 丹青千秋酿,一醉解愁肠.无悔少年枉,只愿壮志狂. 题目描述 小$F$很喜欢数学,但是到了高中以后数学总是考不好.有一天,他在数学课上发起了呆:他想起了过去的一年.一年前,当他初识 ...
- 关于java中对list集合中的数据按照某一个属性进行分组
有的时候,我们需要在java中对集合中的数据进行分组运算. 例如:Bill对象有money(float)和type(String)属性,现有个集合List<Bill>,需要按照Bill的t ...
- flask第三篇 request
每个框架中都有处理请求的机制(request),但是每个框架的处理方式和机制是不同的 为了了解Flask的request中都有什么东西,首先我们要写一个前后端的交互 基于HTML + Flask 写一 ...
- python 异常和弹出框
import tkinter.messagebox try: fileContent = open("abnormal.txt") fileContent.close() prin ...
- jenkins安装以及自由风格的项目搭建(jenkins+tomcat+svn)
jenkins提供了直接通过war包启动以及通过tomcat容器启动的启动方法,这里使用tomcat来启动jenkins,这也是我觉得更稳定的方法. 提前搭建好tomcat环境,这里我使用的版本是:A ...
- 创建DevExtreme应用程序
如果你从头开始一个项目,那就使用DevExtreme Angular 模板.生成的项目带导航菜单以及在响应布局中的几个对应的示例视图. 你可以使用 DevExtreme CLI 生成应用程序: npx ...
- Break 和 Continue 标签 kotlin(12)
Break 和 Continue 标签 在 Kotlin 中任何表达式都可以用标签(label ) 来标记. 标签的格式为标识符后跟 @ 符 号,例如: abc@ . fooBar@ 都是有效的标签( ...
- python正则表达式解析(re)
正则表达式的使用方法主要有4种: re.search(进行正则匹配), re.match(从头开始匹配) re.findall(找出所有符合条件的字符列表) re.split(根据条件进行切分) ...
- 本地oracle可以通过localhost连接,无法通过ip地址连接解决方法,oracle远程连接配置
Oracle11g安装后只有本地可以连接,远程无法连接,而且本地只能配置成localhost配置成IP地址也无法连接. 这是因为安装oracle的时候没有配置远程的监听,默认的监听是localhost ...