题解 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);
}

原文地址:https://www.cnblogs.com/BrianPeng/p/12245908.html

时间: 2024-08-30 15:29:47

题解 CF1196E Connected Component on a Chessboard的相关文章

[LintCode] Find the Connected Component in the Undirected Graph

Find the Connected Component in the Undirected Graph Find the number connected component in the undirected graph. Each node in the graph contains a label and a list of its neighbors. (a connected component (or just component) of an undirected graph i

[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 label and a list of its neighbors. (a connected set of a directed graph is a subgraph in which any two vertices are connected by direct edge path.) Exam

[email&#160;protected] Strongly Connected Component

Strongly Connected Components A directed graph is strongly connected if there is a path between all pairs of vertices. A strongly connected component (SCC) of a directed graph is a maximal strongly connected subgraph. For example, there are 3 SCCs in

[HDU6271]Master of Connected Component

[HDU6271]Master of Connected Component 题目大意: 给出两棵\(n(n\le10000)\)个结点的以\(1\)为根的树\(T_a,T_b\),和一个拥有\(m(m\le10000)\)个结点的图\(G\).\(T_a,T_b\)的每一个结点上都有一个信息,表示\(G\)中的一条边\((u_i,v_i)\).对于\(i\in[1,n]\),询问从\(T_a\)和\(T_b\)上分别取出链\(1\sim i\),将链上的信息所表示的边加入\(G\)中后,\(G

Connected Component in Undirected Graph

Description Find connected component in undirected graph. Each node in the graph contains a label and a list of its neighbors. (A connected component of an undirected graph is a subgraph in which any two vertices are connected to each other by paths,

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 contains a label and a list of its neighbors. (a weak connected component of a directed graph is a maximum subgraph in which any two vertices are conne

lintcode 容易题:Find the Connected Component in the Undirected Graph 找出无向图汇总的相连要素

题目: 找出无向图汇总的相连要素 请找出无向图中相连要素的个数. 图中的每个节点包含其邻居的 1 个标签和 1 个列表.(一个无向图的相连节点(或节点)是一个子图,其中任意两个顶点通过路径相连,且不与超级图中的其它顶点相连.) 样例 给定图: A------B C \ | | \ | | \ | | \ | | D E 返回 {A,B,D}, {C,E}.其中有 2 个相连的元素,即{A,B,D}, {C,E} 解题: 广度优先+递归,写不出来,程序来源 Java程序: /** * Defini

HDU 6271 Master of Connected Component(2017 CCPC 杭州 H题,树分块 + 并查集的撤销)

题目链接  2017 CCPC Hangzhou Problem H 思路:对树进行分块.把第一棵树分成$\sqrt{n}$块,第二棵树也分成$\sqrt{n}$块.    分块的时候满足每个块是一个连通块,那么每个块就有一个共同的祖先. 把询问按照第一个点被第一棵树的哪个祖先管辖和第二个点被第二棵树的哪个祖先管辖,分成$n$类. 每一类询问一起处理,处理完后用可撤销并查集恢复到之前的状态. 每一类询问之间依次转移,每次转移,移动次数不会超过$\sqrt{n}$次. 最后总时间复杂度$O(n^{

Codeforces-1196E

题目描述: You are given two integers bb and ww. You have a chessboard of size 109×109109×109 with the top left cell at (1;1)(1;1), the cell (1;1)(1;1) is painted white. Your task is to find a connected component on this chessboard that contains exactly b