跳到正文

题解

题解:P15042 [UOI 2022 II Stage] 双色图形

FishDeity约 3 分钟
原文
文章信息与版本

文章信息

发布
2026年1月29日
收录
2026年2月10日
更新
2026年11月14日
篇幅
约 3 分钟
题目
P15042

版本

  1. v1当前

题解:P15042 [UOI 2022 II Stage] 双色图形

贪心考虑最优解

我们不妨假定 w<bw<b,我们考虑发挥每个白色格子的最大价值,也就是一个白色格子的周围的黑色格子都被选取,形如:

代码
.B.B.B.B.
BWBWBWBWB
.B.B.B.B.

分析无解情况

当即使每个白色格子周围的黑色格子都被选取,依旧还需要选取黑色格子,则无解。

显然的,此时限制为 w≤3b+1w\leq 3b+1,但是注意到当 ww 或 bb 为 00 时需要特判。

分析有解情况

ww 与 bb 相等

当 ww 与 bb 相等,可以一次输出 WB。

实现
C++
if(w==b){
  printf("1 %lld\n",2*w);
  for(LL i=1;i<=w;i++)printf("WB");
}

ww 与 bb 不等

当 ww 与 bb 不等,我们依旧不妨设 w<bw<b。

按照贪心策略,尽可能将白色方格附近的每个方格都选取。

ACcode

C++
#include<bits/stdc++.h>
using namespace std;
#define LL long long
int main(){
	LL w,b;
	scanf("%lld%lld",&w,&b);
	if(w==0){
		if(b==1)printf("B");
		else printf("-1");
	}
	else if(b==0){
		if(w==1)printf("W");
		else printf("-1");
	}
	else{
		if(w==b){
			printf("1 %lld\n",2*w);
			for(LL i=1;i<=w;i++)printf("WB");
		}
		else if(w>b){
			if(w>b*3+1){
				printf("-1");
			}
			else{
				LL temp=2*b+1;
				printf("3 %lld\n",temp);
				w-=b+1;
				for(LL i=1;i<=min(w,b);i++)printf(".W");
				for(LL i=1;i<=temp-2*min(w,b);i++)printf(".");
				printf("\n");
				for(LL i=1;i<=b;i++)printf("WB");
				printf("W\n");
				for(LL i=1;i<=max(0LL,w-b);i++)printf(".W");
				for(LL i=1;i<=temp-2*max(0LL,w-b);i++)printf(".");
			}
		}
		else{
			swap(w,b);
			if(w>b*3+1){
				printf("-1");
			}
			else{
				LL temp=2*b+1;
				printf("3 %lld\n",temp);
				w-=b+1;
				for(LL i=1;i<=min(w,b);i++)printf(".B");
				for(LL i=1;i<=temp-2*min(w,b);i++)printf(".");
				printf("\n");
				for(LL i=1;i<=b;i++)printf("BW");
				printf("B\n");
				for(LL i=1;i<=max(0LL,w-b);i++)printf(".B");
				for(LL i=1;i<=temp-2*max(0LL,w-b);i++)printf(".");
			}
		}
	}
	return 0;
}
原文