• Educational Codeforces Round 130 (Rated for Div. 2) A--C


    Educational Codeforces Round 130 (Rated for Div. 2)

    A. Parkway Walk

    题目描述

    求走完所有距离所需最小的补充能量数

    题目分析

    用所需总能量数减去初始能量即可

    code
    #include
    
    using namespace std;
    
    int n, m, k, t;
    
    void solve()
    {
        int sum = 0;
        cin >> n >> m;
    
        for(int i = 0; i < n; i ++)
        {
            int u;
            cin >> u;
            sum += u;
        }
        if(sum <= m)puts("0");
        else cout << sum - m << "\n";
    }
    
    int main()
    {
        cin >> t;
        while(t --) solve();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    B. Promo

    题目描述

    如果顾客购买了至少 x 件商品,最便宜的y件商品都是免费的。对于每次询问求各科可以免费获得商品的最大价值

    题目分析

    用贪心的思维则我们尽可能选择大的,这样能使得其中y个较小的值最大化。对于所免费获得的价值,我们可以用前缀和来计算,从而达到降低时间复杂度的目的

    code
    #include
    
    using namespace std;
    
    const int N = 2e5 + 10;
    typedef long long ll;
    
    int n, m, k, t;
    ll a[N], s[N];
    
    bool cmp(int a, int b)
    {
        return a > b;
    }
    
    int main()
    {
        cin >> n >> t;
    
        for(int i = 1; i <= n; i ++)cin >> a[i];
        sort(a + 1, a + n + 1, cmp);
        for(int i = 1; i <= n; i ++)
        {
            s[i] = s[i - 1] + a[i];
            //cout << s[i] << "---\n";
        }
    
        while(t --)
        {
            int x, y;
            cin >> x >> y;
            cout << s[x] - s[x-y] << "\n";
        }
    
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    C. awoo’s Favorite Problem

    题目描述

    问字符串a能否通过两种操作变成b: 操作1(将 ab 变成 ba) 操作2(将 bc 变成 cb)

    题目分析

    两个操作的共性都是通过b来改变字母的相对位置,我们首先可以去掉两个字符串当中的b,若得到的字符串不相等则一定无法完成变换

    此外根据观察可以发现,a的位置只能向后移动,c的位置只能向前。所以在字符串a中的字符a位置一定小于等于在字符串b 中的,字符c的情况相反

    通过以上两部分判断即可

    code
    #include
    
    using namespace std;
    
    const int N = 1e5 + 10;
    
    int n, m, k, t;
    
    void solve()
    {
        int a[N] = {0}, b[N] = {0}, a1[N] = {0}, b1[N] = {0};
        int co1 = 0, co2 = 0, co3 = 0, co4 = 0;
        string s, r, s1, r1;
        s1.clear(), r1.clear();
        cin >> n >> s >> r;
    
        for(int i = 0; i < n; i ++)
        {
            if(s[i] != 'b') s1 += s[i];
            if(s[i] == 'a') a[co1 ++] = i;
            if(s[i] == 'c') b[co2 ++] = i;
    
            if(r[i] != 'b') r1 += r[i];
            if(r[i] == 'a') a1[co3 ++] = i;
            if(r[i] == 'c') b1[co4 ++] = i;
        }
        //cout << s1 << "--" << r1 << "\n";
    
        if(s1 != r1) puts("NO");
        else
        {
            for(int i = 0; i < co1; i ++)
            {
                if(a[i] > a1[i])
                {
                    puts("NO");
                    return;
                }
            }
            for(int i = 0; i < co2; i ++)
            {
                if(b[i] < b1[i])
                {
                    puts("NO");
                    return;
                }
            }
            puts("YES");
        }
    }
    
    int main()
    {
        cin >> t;
        while(t --) solve();
        return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
  • 相关阅读:
    程序员的工作内容
    C++新特性 Type Alias, noexcept, override, final
    python-爬虫(可直接使用)
    【中秋赏码】Java程序员用中文编程教你做月饼。
    JAVA中的“抽象接口”
    Rockland一抗丨视紫红质抗体解决方案
    PFSK152 3BSE018877R1 有源滤波器的定义
    python使用numpy的savetxt函数将数据内容写入指定文本文件中
    22python生命周期的一种案例
    【弃坑xdm】docker容器作为开发环境,更加灵活可靠
  • 原文地址:https://blog.csdn.net/m0_60610120/article/details/126087459