当前位置: 首页 > news >正文

【CF】Day30——Codeforces Round 824 (Div. 2) C + Codeforces Round 825 (Div. 2) BC1

C. Phase Shift

题目:

思路:

好题,值得多看

 这题我们看题目就能想到一个很显然的做法,那就是贪心地把每一个字母换成最前面的没使用过的字母

但是这样直接写是有问题的,因为题目说了最后要让所有的字母成一个换,比如如果是ba,如果我们换成ab,那么就会有一个a->b->a的环了,这显然不符合题意

所以对于每一个还未替换的字符,我们从a~z中选一个最先的最符合的字符,他要满足以下条件:

①.这个字符还没选过

②.这个字符不与现在这个字符相同

③.选完之后不能构成环,如果构成了,那只能是构成长度为26的环

确定好后按照题意模拟即可

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"

bool check(char a, char b,const map<char, char>& mp2)
{
    vector<int> vis(26, 0);
    map<char, char> mp = mp2;
    mp[a] = b;
    int hasc = 0;
    int ccnt = 0;
    while (mp[a])
    {
        if (vis[a - 'a'])
        {
            hasc = true;
            break;
        }
        vis[a - 'a'] = 1;
        ccnt++;
        a = mp[a];
    }
    if (hasc && ccnt < 26)
    {
        return false;
    }
    return true;
}

void solve()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    map<char, char> mp;
    vector<bool> used(26, 0);
    for (int i = 0; i < n; i++)
    {
        if (mp[s[i]])
        {
            continue;
        }
        else
        {
            for (int j = 0; j < 26; j++)
            {
                int t = 'a' + j;
                if (used[j] || s[i] == t)
                {
                    continue;
                }
                if (check(s[i], t, mp))
                {
                    used[j] = true;
                    mp[s[i]] = t;
                    break;
                }
            }
        }
    }
    for (int i = 0; i < n; i++)
    {
        cout << mp[s[i]];
    }
    cout << endl;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}


B. Playing with GCD

题目:

思路:

想难了,不过挺不错的

 一个显然的构造方法是 b[i] 取a[i] 和 a[i-1] 的lcm,因为我们要保证 gcd 恰好等于 a[i],所以最大只能取这个,同时 b[i] 与两个 a 都有关系,所以也只能这样取,否则如果是k*lcm,那么就会多个系数,这样最后可能就不会等于 a[i] 了

最后我们再检查一下 b[i] 是不是真的都符合即可

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"

int gcd(int a, int b)
{
    return !b ? a : gcd(b, a % b);
}

int lcm(int a,int b)
{
    return a * b / gcd(a, b);
}

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n+2,1),b(n+2,1);
    for (int i = 1; i <= n+1; i++)
    {
        if(i <= n)
        cin >> a[i];
        b[i] = lcm(a[i], a[i - 1]);
    }
    for (int i = 1; i <= n; i++)
    {
        if (gcd(b[i],b[i+1]) != a[i]) 
        {
            no;
            return;
        }
    }
    yes;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

C1. Good Subarrays (Easy Version)

题目:

思路:

双指针题

这道题由于求的是一个区间l~r,那么我们就可以来观察一下这个区间有没有什么特性

一个显然的特性是,如果l~r符合,那么l+1~r肯定也符合

代码:

#include <iostream>
#include <algorithm>
#include<cstring>
#include<cctype>
#include<string>
#include <set>
#include <vector>
#include <cmath>
#include <queue>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <stack>
#include <memory>
using namespace std;
#define int long long
#define yes cout << "YES\n"
#define no cout << "NO\n"

void solve()
{
    int n;
    cin >> n;
    vector<int> a(n);
    for (int i = 0; i < n; i++)
    {
        cin >> a[i];
    }
    int l = 0, r = 0,res = 0;
    while (l < n)
    {
        while (r < n && a[r] >= r-l+1)
        {
            r++;
        }
        res += r - l;
        l++;
    }
    cout << res << endl;
}

signed main()
{
    cin.tie(0)->sync_with_stdio(false);
    int t = 1;
    cin >> t;
    while (t--)
    {
        solve();
    }
    return 0;
}

相关文章:

  • 数字内容体验的核心价值是什么?
  • 【前后端】npm包mysql2的使用__nodejs mysql包升级版
  • 四、TorchRec的推理优化
  • RAG(检索增强生成)系统,提示词(Prompt)表现测试(数据说话)
  • 如何在AMD MI300X 服务器上部署 DeepSeek R1模型?
  • c++关键字new
  • CExercise_07_1指针和数组_2数组元素的逆序数组逆序(指针版 reverse_by_ptr 和下标版 reverse_arr)
  • NVIDIA H100 vs A100:新一代GPU架构性能对比分析
  • 第五章:5.1 ESP32物联网应用 - MQTT协议深度教程
  • 大厂文章阅读
  • 速盾:高防CDN节点对收录有影响吗?
  • 深入解析 Microcom:嵌入式串口调试利器
  • 代码随想录算法训练营Day27 | Leetcode 56. 合并区间、738.单调递增的数字、968.监控二叉树
  • C++中的设计模式
  • Linux中的Vim与Nano编辑器命令详解
  • 第8课:多智能体系统评估与迭代
  • 【模板】缩点
  • GPU算力优化
  • 敏感词过滤算法
  • swift菜鸟教程1-5(语法,变量,类型,常量,字面量)
  • 柳州网站开发/什么是seo关键词
  • 有什么做外贸的网站/短视频seo营销系统
  • 温州网站制作企业/百度明星人气排行榜
  • 企业网络安全设计方案/seo推广策略
  • 远程it外包服务/企业网站seo平台
  • 直播网站开发步骤/云南优化公司