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 ...
随机推荐
- 如何在main.js中改变vuex中的值?
做登录权限控制的时候, 我通过全局路由守卫来去做权限判断,这样的话可能需要在整个项目加载的初期去做一些诸如 接口请求. vuex修改 之类的问题 其实非常简单,直接如图:
- SRS之播放推流视频
1. 综述 首先,推流直播的配置文件如下: # rtmp.conf listen 1935; max_connections 1000; daemon off; srs_log_tank consol ...
- vue-template-compiler作用
vue-template-compiler的作用是什么: 看起来 template-compiler是给parse函数使用的, 那么parse函数是干什么的呢 先看一下parse的结果: 结论:使用v ...
- ACO 蚁群算法(算法流程,TSP例子解析)
算法 计算机 超级计算 高性能 科学探索 1. 算法背景——蚁群的自组织行为特征 高度结构化的组织——虽然蚂蚁的个体行为极其简单,但由个体组成的蚁群却构成高度结构化的社会组织,蚂蚁社会的成员有分工,有 ...
- MySQL使用order by field()自定义排序
MySQL的自定义排序和Oracle相比,要简单得多. 假设在表v_education的列schoolRecord中,有以下字段:'小学','初中','高中','专科','本科','硕士','博士'. ...
- 【MyEclipse优化】-----如何合理设置MyEclipse中的validation选项
打开eclipse,点击[window]菜单,选择[preferences]选项. 在左侧点击[validation]选项,在右侧可以看到eclipse进行的自动检查都有哪些内容. 将Manual(手 ...
- sql(存储过程,事务,索引,游标,触发器)
1.SqlServer中like '%_%'来匹配下划线: --在sql server的like中下划线类似于通配符%,所以无法使用like '%_%'来匹配下划线 select * from cla ...
- ubuntu下自动获取ip设置
vi /etc/network/interfaces文件为如下内容 wq保存 重启网卡:sudo /etc/init.d/networking restart
- open_basedir restriction in effect,解决php引入文件权限问题 解决方法
如下: 出现问题的原因: 查看问题描述以及资料,发现是php open_basedir 配置的问题,PHP不能引入其授权目录上级及其以上的文件: 一般情况下是不会出现这种问题的,之所以出现这个问题绝大 ...
- css中元素定位
在html中网页可以看成一个立体的空间,一个完整的页面是由很多个页面堆积形成的,如下图所示 CSS中Position属性有四个可选值,它们分别是:static.absolute.fixed.relat ...