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

· · 题解

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

贪心考虑最优解

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

.B.B.B.B.
BWBWBWBWB
.B.B.B.B.

分析无解情况

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

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

分析有解情况

w 与 b 相等

当 w 与 b 相等,可以一次输出 WB。

实现

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

w 与 b 不等

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

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

ACcode

#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;
}