Codeforces Round 923 (Div. 3)

文章描述了四个编程问题,涉及字符串中特定字符的操作(如查找B字符的位置、计算连续字符W的数量、比较数组中独特元素等),并提供了相应的C++解决方案,强调了算法和数据结构的应用。
摘要由CSDN通过智能技术生成

A. Make it White

思路:从前向后找到第一次出现'B‘的位置 l ,从后往前找到第一次出现'B'的位置 r ,答案就是 r-l+1.

#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;

string s;
int n;
void solve()
{
    cin >> n;
    cin >> s;
    bool ok = false;
    for (int i = 0; i < s.length(); i++)
    {
        if (s[i] == 'B')
        {
            ok = true;
            break;
        }
    }
    if (ok)
    {
        int l = 0, r = s.length() - 1;
        while (l < s.length() && s[l] == 'W')
        {
            l++;
        }
        while (r >= l && s[r] == 'W')
        {
            r--;
        }
        int res = r - l + 1;
        cout << res << '\n';
    }
    else
    {
        cout << "0" << '\n';
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    // T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

B. Following the String

思路:用一个map储存每个字符出现的频率,然后枚举模拟即可

#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;

int n;
int a[N];
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    map<char, int> mp;
    string res;
    for (int i = 1; i <= n; i++)
    {
        int x = a[i];
        for (char ch = 'a'; ch <= 'z'; ch++)
        {
            if (mp[ch] == x)
            {
                res.push_back(ch);
                mp[ch]++;
                break;
            }
        }
    }
    cout << res << '\n';
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    // T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

C. Choose the Different Ones!

思路:数组A中<=k且只出现在A中的元素数量记作cnta,数组B中<=k且只出现在B中的元素记作cntb,

如果cnta>k/2 或者 cntb > k/2,就输出"NO",否则判断两个数组中的元素是否可以组成1~k中的每个数,如果可以输出"YES“,否则输出"NO".

#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;

int n, m, k;
int a[N];
int b[N];
void solve()
{
    cin >> n >> m >> k;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    for (int i = 1; i <= m; i++)
    {
        cin >> b[i];
    }
    int c = k / 2;
    set<int> sa, sb;
    for (int i = 1; i <= n; i++)
    {
        if (a[i] <= k)
        {
            sa.insert(a[i]);
        }
    }
    for (int i = 1; i <= m; i++)
    {
        if (b[i] <= k)
        {
            sb.insert(b[i]);
        }
    }
    map<int, int> mpa, mpb;
    for (auto it = sa.begin(); it != sa.end(); it++)
    {
        int x = (*it);
        //    cout << "SA " << x << "\n";
        mpa[x] = 1;
    }
    for (auto it = sb.begin(); it != sb.end(); it++)
    {

        int x = (*it);
        //  cout << "SB " << x << '\n';
        mpb[x] = 1;
    }
    int ca = 0, cb = 0;
    for (auto it = sb.begin(); it != sb.end(); it++)
    {
        int x = (*it);
        if (mpa[x] == 0)
        {
            cb++;
        }
    }
    for (auto it = sa.begin(); it != sa.end(); it++)
    {
        int x = (*it);
        if (mpb[x] == 0)
        {
            ca++;
        }
    }
    // cout << "KK " << ca << " " << cb << '\n';
    if (ca > c || cb > c)
    {
        cout << "NO" << '\n';
    }
    else
    {
        map<int, int> mpk;
        for (auto it = sa.begin(); it != sa.end(); it++)
        {
            int x = (*it);
            mpk[x] = 1;
        }
        for (auto it = sb.begin(); it != sb.end(); it++)
        {
            int x = (*it);
            mpk[x] = 1;
        }
        if (mpk.size() != k)
        {
            cout << "NO" << '\n';
        }
        else
        {
            cout << "YES" << '\n';
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    // T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

D. Find the Different Ones!

思路:一开始想的是如何预处理然后O(1)的判断,但是后来发现行不通。(因此一个小时就过去了),但是在思考的过程中,发现这个题可以用二分来解决。(发现的那一瞬间很有成就感)

二分思路:从前向后遍历,当发现a[i] != a[i+1]时将下标 i 加入vector中,因此vector中的元素就是有序的啦。对于要查询的 l 和 r,如果vector是空的,就输出-1,否则二分找到第一个大于等于 l 的下标 i,如果这个下标 i >= r,就输出-1(因为我们要保证 i 和 i+1 都要属于 l 到 r),否则 i 和 i+1 就一定属于 l 到 r.二分前还应该确保vector中至少存在一个 i >= l , 因此就有了代码中的 (v[res] < l )那一段。

#include <bits/stdc++.h>
using namespace std;
// #pragma G++ optimize(2)
#define int long long
#define double long double
#define xiaowen ac
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef long long ll;
const double eps = 1e-8;
const int inf = 0x3f3f3f3f;
const int N = 4e6 + 7;
const int P = 131;

int n, a[N], q;
void solve()
{
    cin >> n;
    for (int i = 1; i <= n; i++)
    {
        cin >> a[i];
    }
    cin >> q;
    vector<int> v;
    for (int i = 1; i + 1 <= n; i++)
    {
        if (a[i] != a[i + 1])
        {
            v.push_back(i);
        }
    }
    while (q--)
    {
        int l, r;
        cin >> l >> r;
        if (v.size() == 0)
        {
            cout << "-1"
                 << " "
                 << "-1" << '\n';
        }
        else
        {
            int res = v.size() - 1;
            int left = 0, right = v.size() - 1;
            if (v[res] < l)
            {
                cout << "-1"
                     << " "
                     << "-1" << '\n';
            }
            else
            {
                while (left <= right)
                {
                    int mid = (left + right) / 2;
                    if (v[mid] < l)
                    {
                        left = mid + 1;
                    }
                    else
                    {
                        right = mid - 1;
                        res = min(res, mid);
                    }
                }
                if (v[res] >= r)
                {
                    cout << "-1"
                         << " "
                         << "-1" << '\n';
                }
                else
                {
                    cout << v[res] << " " << v[res] + 1 << '\n';
                }
            }
        }
    }
}
signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout.tie(nullptr);
    int T;
    cin >> T;
    //  T = 1;
    while (T--)
    {
        solve();
    }
    return 0;
}

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值