Codeforces 626B Cards 【BFS or 讨论】

本博客探讨了一个涉及B、G、R三种颜色卡片的博弈问题,参与者通过特定规则交换卡片直到只剩一张,分析并预测最终卡片的颜色。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

B. Cards
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Catherine has a deck of n cards, each of which is either red, green, or blue. As long as there are at least two cards left, she can do one of two actions:

  • take any two (not necessarily adjacent) cards with different colors and exchange them for a new card of the third color;
  • take any two (not necessarily adjacent) cards with the same color and exchange them for a new card with that color.

She repeats this process until there is only one card left. What are the possible colors for the final card?

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 200) — the total number of cards.

The next line contains a string s of length n — the colors of the cards. s contains only the characters 'B', 'G', and 'R', representing blue, green, and red, respectively.

Output

Print a single string of up to three characters — the possible colors of the final card (using the same symbols as the input) in alphabetical order.

Examples
input
2
RB
output
G
input
3
GRG
output
BR
input
5
BBBBB
output
B
Note

In the first sample, Catherine has one red card and one blue card, which she must exchange for a green card.

In the second sample, Catherine has two green cards and one red card. She has two options: she can exchange the two green cards for a green card, then exchange the new green card and the red card for a blue card. Alternatively, she can exchange a green and a red card for a blue card, then exchange the blue card and remaining green card for a red card.

In the third sample, Catherine only has blue cards, so she can only exchange them for more blue cards.

题意:有若干个R、B、G颜色的球,你可以选择两个颜色不一样的球换成一个其他颜色的球,或者使用两个颜色相同的球,换成一个该颜色的球。问你最后剩余的球可能是哪种颜色。


思路:BFS 或者 讨论。


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF (2000000000+10)
#define eps 1e-8
#define MAXN (60000+10)
#define MAXM (600000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
char str[210];
struct Node{
    int b, g, r;
};
bool vis[210][210][210]; bool B, G, R;
bool judge(Node a)
{
    if(vis[a.b][a.g][a.r]) return false;
    vis[a.b][a.g][a.r] = true;
    return true;
}
void BFS(int sa, int sb, int sc)
{
    Node now, next; queue<Node> Q;
    now.b = sa, now.g = sb, now.r = sc; Q.push(now);
    while(!Q.empty())
    {
        now = Q.front(); Q.pop();
        if((now.b == 0) + (now.g == 0) + (now.r == 0) > 1)
        {
            if(now.b) B = true;
            if(now.g) G = true;
            if(now.r) R = true;
            continue;
        }
        if(now.b == 0){next = now; next.b++; next.g--; next.r--; if(judge(next)) Q.push(next);}
        else if(now.g == 0){next = now; next.g++; next.b--; next.r--; if(judge(next)) Q.push(next);}
        else if(now.r == 0){next = now; next.r++; next.b--; next.g--; if(judge(next)) Q.push(next);}
        else
        {
            next = now; next.b++; next.g--; next.r--; if(judge(next)) Q.push(next);
            next = now; next.g++; next.b--; next.r--; if(judge(next)) Q.push(next);
            next = now; next.r++; next.b--; next.g--; if(judge(next)) Q.push(next);
        }
        if(now.b == 0)
        {
            next = now; if(next.g > 1) {next.g--; if(judge(next)) Q.push(next);}
            next = now; if(next.r > 1) {next.r--; if(judge(next)) Q.push(next);}
        }
        else if(now.g == 0)
        {
            next = now; if(next.b > 1) {next.b--; if(judge(next)) Q.push(next);}
            next = now; if(next.r > 1) {next.r--; if(judge(next)) Q.push(next);}
        }
        else if(now.r == 0)
        {
            next = now; if(next.g > 1) {next.g--; if(judge(next)) Q.push(next);}
            next = now; if(next.b > 1) {next.b--; if(judge(next)) Q.push(next);}
        }
        else
        {
            next = now; if(next.b > 1) {next.b--; if(judge(next)) Q.push(next);}
            next = now; if(next.g > 1) {next.g--; if(judge(next)) Q.push(next);}
            next = now; if(next.r > 1) {next.r--; if(judge(next)) Q.push(next);}
        }
    }
}
int main()
{
    int n; Ri(n);
    Rs(str); B = G = R = false;
    int numb = 0, numg = 0, numr = 0;
    for(int i = 0; i < n; i++)
    {
        numb += (str[i] == 'B');
        numg += (str[i] == 'G');
        numr += (str[i] == 'R');
    }
    CLR(vis, false);
    BFS(numb, numg, numr);
    if(B) printf("B");
    if(G) printf("G");
    if(R) printf("R");
    printf("\n");
    return 0;
}


AC代码:


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <string>
#define INF (2000000000+10)
#define eps 1e-8
#define MAXN (60000+10)
#define MAXM (600000+10)
#define Ri(a) scanf("%d", &a)
#define Rl(a) scanf("%lld", &a)
#define Rf(a) scanf("%lf", &a)
#define Rs(a) scanf("%s", a)
#define Pi(a) printf("%d\n", (a))
#define Pf(a) printf("%.2lf\n", (a))
#define Pl(a) printf("%lld\n", (a))
#define Ps(a) printf("%s\n", (a))
#define W(a) while((a)--)
#define CLR(a, b) memset(a, (b), sizeof(a))
#define MOD 1000000007
#define LL long long
#define lson o<<1, l, mid
#define rson o<<1|1, mid+1, r
#define ll o<<1
#define rr o<<1|1
#define PI acos(-1.0)
#pragma comment(linker, "/STACK:102400000,102400000")
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
char str[210];
int main()
{
    int n; Ri(n); bool B, G, R;
    Rs(str); B = G = R = false;
    int numb = 0, numg = 0, numr = 0;
    for(int i = 0; i < n; i++)
    {
        numb += (str[i] == 'B');
        numg += (str[i] == 'G');
        numr += (str[i] == 'R');
    }
    int v = (numb == 0) + (numg == 0) + (numr == 0);
    if(v == 1)
    {
        if(numb == 0)
        {
            B = true;
            if(numg == numr && numg == 1) {}
            else if(numg == 1 || numr == 1)
            {
                G = numg == 1;
                R = numr == 1;
            }
            else
            {
                G = true;
                R = true;
            }
        }
        else if(numg == 0)
        {
            G = true;
            if(numb == numr && numb == 1) {}
            else if(numb == 1 || numr == 1)
            {
                B = numb == 1;
                R = numr == 1;
            }
            else
            {
                B = true;
                R = true;
            }
        }
        else if(numr == 0)
        {
            R = true;
            if(numb == numg && numg == 1) {}
            else if(numb == 1 || numg == 1)
            {
                G = numg == 1;
                B = numb == 1;
            }
            else
            {
                G = true;
                B = true;
            }
        }
    }
    else if(v == 2)
    {
        B = numb > 0;
        G = numg > 0;
        R = numr > 0;
    }
    else{B = G = R = true;}
    if(B) printf("B");
    if(G) printf("G");
    if(R) printf("R");
    printf("\n");
    return 0;
}


### 关于 Floor 和 Ceil 函数在编程中的实现 Floor 和 Ceil 是两个常见的数学函数,在许多算法竞赛平台(如 Codeforces)上经常被用于解决涉及整数除法、取模运算等问题。以下是它们的定义以及如何在程序设计中实现这些功能。 #### 定义 - **Floor Function**: 对实数向下取整,返回不大于该数值的最大整数[^1]。 - **Ceil Function**: 对实数向上取整,返回不小于该数值的最小整数[^2]。 #### 实现方法 大多数现代编程语言都提供了内置库来支持 floor 和 ceil 的计算: ##### 使用标准库 在 C++ 中可以利用 `<cmath>` 库中的 `floor` 和 `ceil` 方法完成相应操作: ```cpp #include <iostream> #include <cmath> int main() { double num = 3.7; std::cout << "Floor value: " << static_cast<int>(std::floor(num)) << "\n"; // 输出 3 std::cout << "Ceil value: " << static_cast<int>(std::ceil(num)) << "\n"; // 输出 4 } ``` 然而需要注意的是,当处理负数时,这两个函数的行为可能不符合直觉。例如,`floor(-2.5)` 返回 `-3` 而不是 `-2`,因为它是严格意义上的“下限”。 如果目标是在没有浮点误差的情况下仅针对正整数执行类似的逻辑,则可以通过简单的算术表达式手动模拟这两种行为而无需调用任何外部库函数: - 手动实现 Floor: 当 a / b 结果为非零余数时保持原样;否则减去一个小量使得最终结果总是趋向更小的方向移动直到达到下一个较低的整数位置为止。 - 手动实现 Ceil: 如果存在剩余部分则增加到下一更高的整数值上去;如果没有多余的部分,那么它本身已经是天花板高度了. 具体代码如下所示: ```python def manual_floor(a, b): return (a // b) def manual_ceil(a, b): if a % b == 0: return a // b else: return (a // b) + 1 ``` 上述 Python 版本展示了如何通过基本运算符构建自己的版本而不依赖额外模块的帮助[^3]. ### 示例应用案例分析 考虑到实际应用场景下的复杂度需求,这里选取几个典型例子加以说明其用途所在之处及其重要性何在? 比如 CF 圆形 #701(Division Two)-Problem C(Floor And Mod), 这道题目要求我们找到满足特定条件的一系列数字组合方案数目统计工作当中需要用到大量的地板除法规律推导过程才能得出正确结论出来. 同样还有像CF Problem D(编号1469)-Ceiling Division Operations [^2], 此处探讨的就是怎样快速有效地把初始状态转变成为期望结束形态所经历过的最少次数动作规划策略研究方向上面去了.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值