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( ...
随机推荐
- C# 发送电子邮件(smtp)
相关享目托管在github: https://github.com/devgis/CSharpCodes
- c++ 队列算法
include using namespace std; #define Maxsize 5 typedef int DataType; typedef struct Queue { DataType ...
- blackarch 安装指南
建议直接 www.blackangle.cn/BlackArchGuide.htm v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#de ...
- mysql:数据库与实例的区别
题记:最近想更深入的了解mysql,所以买了一些书在学习,趁着这个机会开个坑,整理一下一些我认为重要的知识点. 刚工作那会经常能听到组长提到实例这个词,一开始我以为是服务器... 数据库(databa ...
- 「洛谷P1080」「NOIP2012提高组」国王游戏 解题报告
P1080 国王游戏 题目描述 恰逢 \(H\)国国庆,国王邀请\(n\)位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 \( ...
- Django 多表、跨表、聚合、分组查询
前期准备: 创建表 class Book(models.Model): title = models.CharField(max_length=32) price = models.DecimalFi ...
- 2019 年 stackoverflow 网站最受欢迎的 20 个 Python 问题
在最新一期的"Python开发者周刊"(Pycoder's weekly)里,我看到一则有意思的分享,故转出来分享给大家. 该分享来自是一份"python weekly ...
- 【转】Algorithms -离散概率值(discrete)和重置、洗牌(shuffle)算法及代码
离散概率值(discrete) 和 重置\洗牌(shuffle) 算法 及 代码 本文地址: http://blog.csdn.net/caroline_wendy/article/details/1 ...
- 主席树 - 查询某区间第 K 大
You are working for Macrohard company in data structures department. After failing your previous tas ...
- 2018南京现场赛K 随机输出
题目链接:http://codeforces.com/gym/101981/attachments n和m太小,空地联通无环,总步数太大,直接随机输出5w个方向 #include<iostrea ...