Codeforces edu 161 (div2)

Problem - A - Codeforces

思维题,判断c字符串的每一位是否都能在相对应的a字符串或者b字符串里面 找到;

如果都能找到的话就输出 NO;否则输出YES;

#include <bits/stdc++.h>
using namespace std;

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int k;
    cin >> k;
    
    while(k --)
    {
        int len;
        cin >> len;
        string a , b, c;
        cin >> a >> b >> c;
        
        bool st = false;;
        for(int i = 0 ; i < len ; i ++)
        {
            if(c[i] == a[i])
            {
                
                continue;
            }
            else if(c[i] == b[i])
            {
                continue;
            }
            else 
            {
                st = true;
                break;
            }
        }
        if(st) cout <<"YES" << endl;
        else cout << "NO" << endl;
    }
    return 0;
    
}

Problem - B - Codeforces

涉及了组合数的问题;

拼成的三角形只有两种情况:

1.三个一样长度的 (也就是说如果某个长度的木棒 数量 >= 3 就有Cm3中选择 m个里面选择3个)

2.两个一样长度的 + 比他们小的任何一个长度的

要学会组合数的写法;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int len[N];
int b[N];
bool st[N];

LL cmb(LL n, LL m)
{
    LL ans = 1;
    for(LL i =1 ; i <= m;i ++)
    {
        ans = ans * (n-m+i)/i;
    }
    return ans;
}

int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int k;
    cin >> k;
    
    while(k --)
    {
        bool st[N];
        int n;
        cin >> n;
        for(int i = 1 ; i <= n ; i ++) st[i] = false;
        
        map<int , int>p;
        map<int , int>num;
        
        for(int i = 1;  i <= n ; i ++) cin >> len[i];
        sort(len + 1 , len + n + 1);
        
        
        for(int i = 1 ; i <= n ; i ++)  p[len[i]] ++; //统计每个长度有多少个
        //for(int i = 1 ; i <= n ; i ++)
        //{
            //if(st[len[i]] == false)
            //{
                //num[len[i]] += p[len[i-1]];
                //st[len[i]] = true;
            //}
        //}
        
        LL res = 0;
        LL m = 0;
        for(auto t : p)
        {
            if(t.second >= 3)
            {
                res += cmb(t.second,3); //对应第一种情况;
            }
            res += m * cmb(t.second,2); //对应第二种情况
            m += t.second;
        }
        
        cout << res << endl;
    }
    return 0;
    
}

Problem - C - Codeforces

本题,涉及到前缀和还有后缀和的一些 方法;同时要用map存一下每个点对应的最近城市是哪一个点;

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N = 1e6 + 10;
int a[N];
int b[N]; //前缀和
int c[N]; //后缀和
int main()
{
    std::ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    
    int k;
    cin >> k;
    
    while(k --)
    {
        int n;
        cin >> n;
        map<int , int>p; //存每一个地方的最近城市;
        
        for(int i = 1; i <= n ; i ++) 
        {
            cin >> a[i];
            b[i] = 0;
            c[i] = 0;
        }
        
        for(int i = 1 ; i <= n ; i ++)
        {
            if(i == 1) p[a[i]] = i + 1;
            else if(i == n) p[a[i]] = i - 1;
            else 
            {
                if(abs(a[i] - a[i - 1]) > abs(a[i] - a[i + 1])) p[a[i]] = i + 1;
                else p[a[i]] = i - 1;
            }
        }
        //前缀和;
        //如果前一个点的最近距离是后面这个点,那么钱就只需要花费1元;
        //否则就需要abs(a[i] - a[i - 1]) 元;
        for(int i = 2; i <= n ; i ++)
        {
            if(p[a[i - 1]] == i) b[i] += b[i - 1] + 1;
            else b[i] += b[i - 1] + abs(a[i] - a[i - 1]);
        }
        
        //后缀和;
        //如果后面这一个点的最近距离是前面这个点;那么从后面到前面化的钱就需要1元 并加上前面的费用;
        //否则就需要abs(a[i + 1] - a[i]) 元
        for(int i = n - 1; i >= 1 ; i --)
        {
            if(p[a[i + 1]] == i) c[i] += c[i + 1] + 1;
            else c[i] += c[i + 1] + abs(a[i] - a[i + 1]);
        }
        
        int q;
        cin >> q;
        
        while(q --)
        {
            LL ans = 0;
            int st , ed;
            cin >> st >> ed;
            if(ed > st)
            cout << b[ed] - b[st] << endl;
            else
            cout << c[ed] - c[st] << endl;
        }
    } 
    return 0;
    
}
posted @ 2024-01-19 14:59  不过是过客  阅读(9)  评论(0编辑  收藏  举报