Solution: 题解 CF1196E Connected Component on a Chessboard
感觉这题还可以
因为总空间比输入数量 不知高到哪里去了 ,所以完全不需要考虑放不下的问题
从贪心的角度考虑,如果要使相差数量巨大的\(b\)和\(w\)能够成功放下来,应该使这些方块尽量分散(似乎有点抽象)
来一发图解
作者因为太懒于是决定直接以B表示黑色,W表示白色
假设有一组方块拼成了一个正方形,如图
BWB
WBW
BWB
那么在不改变白块数量的情况下,最多还能加\(4\)个黑块,分别连在四个白块旁边
但是如果拉成直线,如图
BWBWBWBWB
发现可以在两边总共加\(8\)个黑块了,因为每个白块都可以再接\(2\)个黑块
所以拉成直线是最优方法(我有一个绝妙的证明但是这里写不下)
现在回到题目
如果\(b=w\),那么非常简单,直接画条直线
现在考虑\(b\not=w\)
不失一般性,直接假设\(b>w\),反正如果\(b<w\)的话整体右移一格即可
先画直线
直线会包含所有白块
为了尽量消耗黑块,让直线的两个端点都是黑块
比如说如果\(b>w=3\),那么直线应该长这个亚子
BWBWBWB
这样就先消耗掉\(w+1\)个黑块
对于剩下的\(b-w-1\)个黑块,就让它们分置在两边的总共\(2w\)个空间里面
所以如果\(b-w-1\leqslant2w\)那么就是可行的,否则不能
对于后面的具体处理方法就请大家自己思考吧,具体请见代码
Time complexity: \(O(\sum b+\sum w)\)
Memory complexity: \(O(1)\)
\(288\)ms / \(8.00\)KB
//This program is written by Brian Peng.
#pragma GCC optimize("Ofast","inline","no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define Rd(a) (a=read())
#define Gc(a) (a=getchar())
#define Pc(a) putchar(a)
int read(){
register int x;register char c(getchar());register bool k;
while(!isdigit(c)&&c^'-')if(Gc(c)==EOF)exit(0);
if(c^'-')k=1,x=c&15;else k=x=0;
while(isdigit(Gc(c)))x=(x<<1)+(x<<3)+(c&15);
return k?x:-x;
}
void wr(register int a){
if(a<0)Pc('-'),a=-a;
if(a<=9)Pc(a|'0');
else wr(a/10),Pc((a%10)|'0');
}
signed const INF(0x3f3f3f3f),NINF(0xc3c3c3c3);
long long const LINF(0x3f3f3f3f3f3f3f3fLL),LNINF(0xc3c3c3c3c3c3c3c3LL);
#define Ps Pc(' ')
#define Pe Pc('\n')
#define Frn0(i,a,b) for(register int i(a);i<(b);++i)
#define Frn1(i,a,b) for(register int i(a);i<=(b);++i)
#define Frn_(i,a,b) for(register int i(a);i>=(b);--i)
#define Mst(a,b) memset(a,b,sizeof(a))
#define File(a) freopen(a".in","r",stdin),freopen(a".out","w",stdout)
int q,b,w,s;
signed main(){
Rd(q);
while(q--){
Rd(b),Rd(w);
if(b==w){
puts("YES");
Frn1(i,1,b<<1)Pc('1'),Ps,wr(i),Pe;
}else{
b<w?(swap(b,w),s=2):s=1;
if((b-=w+1)>w<<1){puts("NO");continue;};
puts("YES");
Frn1(i,0,w<<1)Pc('2'),Ps,wr(s+i),Pe;
if(b<=w)Frn0(i,0,b)Pc('1'),Ps,wr(s+(i<<1|1)),Pe;
else{
Frn0(i,0,w)Pc('1'),Ps,wr(s+(i<<1|1)),Pe;
b-=w;
Frn0(i,0,b)Pc('3'),Ps,wr(s+(i<<1|1)),Pe;
}
}
}
exit(0);
}
Solution: 题解 CF1196E 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个格子要连接在一起,不需要成环).问你可不可以找出来,如果可 ...
- Codeforces 1196E. Connected Component on a Chessboard
传送门 注意到棋盘可以看成无限大的,那么只要考虑如何构造一个尽可能合法的情况 不妨假设需要的白色格子比黑色格子少 那么容易发现最好的情况之一就是白色排一排然后中间黑的先连起来,剩下黑色的再全部填白色周 ...
- [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 ...
- [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 ...
- 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 ...
- algorithm@ Strongly Connected Component
Strongly Connected Components A directed graph is strongly connected if there is a path between all ...
- [HDU6271]Master of Connected Component
[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m( ...
随机推荐
- codeforces 161D 点分治
传送门:https://codeforces.com/problemset/problem/161/D 题意: 求树上点对距离恰好为k的点对个数 题解: 与poj1741相似 把点分治的模板改一下即可 ...
- CP策略含有中文字符提交失败故障解决
硬件平台:CP5600 系统版本:R80.10 补丁版本:TAKE103 故障现象:提交新增策略失败,日志显示 if the problem persists contact Checkpoint S ...
- Windows 服务安装与卸载 (通过 Sc.exe)
1. 安装 新建文本文件,重命名为 ServiceInstall.bat,将 ServiceInstall.bat 的内容替换为: sc create "Verity Platform De ...
- mapper的配置文件
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE mapperPUBLIC "-// ...
- linux中inode的理解
一.inode是什么? 理解inode,要从文件储存说起. 文件储存在硬盘上,硬盘的最小存储单位叫做"扇区"(Sector).每个扇区储存512字节(相当于0.5KB). 操作系统 ...
- Alibaba Cloud Toolkit 使用心得(IDEA版)
一.安装插件 确保 IntelliJ IDEA 在 2018.1 或更高版本 打开 Settings - Plugins 搜索安装 Alibaba Cloud Toolkit 二.配置环境 Deplo ...
- 0014 标签显示模式:display(重点)
目标: 理解 标签的三种显示模式 三种显示模式的特点以及区别 理解三种显示模式的相互转化 应用 实现三种显示模式的相互转化 2.1 什么是标签显示模式 什么是标签的显示模式? 标签以什么方式进行显示, ...
- JVM执行引擎
1.概述 执行引擎是jvm核心组成部分之一,建立在物理器,硬件和操作系统层面之上,引擎在执行代码时会有解释执行和编译执行两种选择,输入字节码文件,字节码解析输出结果. 2.栈帧 栈帧是用于支持虚拟机进 ...
- Mysql的SQL优化指北
概述 在一次和技术大佬的聊天中被问到,平时我是怎么做Mysql的优化的?在这个问题上我只回答出了几点,感觉回答的不够完美,所以我打算整理一次SQL的优化问题. 要知道怎么优化首先要知道一条SQL是怎么 ...
- JWT实现分布式Session
JWT是什么 JWT一看就是简称,它的全称JSON Web Token,从字面上我们看出 1.数据是JSON格式 2.用于Web应用 3.是一个Token,也就是一个令牌方式 看看官方的说明,它定义了 ...