2022.3.23

Codeforces Round #764 (Div. 3)

A. Plus One on the Subset

从最小的变成最大的,也就是最大值和最小值之差。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin >> n;
        int mmax = -1, mmin = 1e9;
        for (int i = 1; i <= n;i++)
        {
            int x;
            cin >> x;
            mmin = min(mmin, x);
            mmax = max(mmax, x);
        }
        cout << mmax - mmin << '\n';
    }
    return 0;
}

B - Make AP

让他变成等差数列但是注意是不能改变a,b,c的顺序的,先判断abc原本是不是等差数列,如果不是再判断,如果公差大于b的话并且a+c能够整除2b的话说明是可以构造的(等差数列性质,a1+a3=2a2),否则不行,如果公差小于等于b的话,那么久看看a和c能不能再构成一个等差数列,正的反的都可以。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=1e5+10,INF=1e8;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int a, b, c;
        cin>>a >> b >> c;
        if(a+c==2*b)
            cout << "YES" << '\n';
        else 
        {
            if(2*b<a+c)
            {
                if((a+c)%(2*b)==0)
                    cout << "YES" << '\n';
                else
                    cout << "NO" << '\n';
            }
            else 
            {
                if((2*b-c)%a==0)
                {
                    cout << "YES" << '\n';
                }
                else if((2*b-a)%c==0)
                {
                    cout << "YES" << '\n';
                }
                else
                    cout << "NO" << '\n';
            }
        }
    }
    return 0;
}

C. Division by Two and Permutation

最终是要构成全排列,我们把当前大于n的数不断除2直到小于n,并且记录当前可以用于构成全排列的数的数量。如果当某个位置的数有多个的话只保留一个,并让多出来的/2,构成其他的,如果到最后每个位置都有的话说明成功。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=50+10,INF=1e8;
int a[N],cnt[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        memset(cnt, 0, sizeof cnt);
        int n;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        for (int i = 1; i <= n;i++)
        {
            while(a[i]>n)
            {
                a[i] /= 2;
            }
            cnt[a[i]]++;
        }
        int f = 0;
        for (int i = n; i >= 1;i--)
        {
            if(cnt[i]==0)
            {
                f = 1;
                break;
            }
            while(cnt[i]>1)
            {
                cnt[i]--;
                cnt[i / 2]++;
            }
        }
        if(f)
            cout << "NO" << '\n';
        else
            cout << "YES" << '\n';
    }
    return 0;
}

D - Palindromes Coloring

我们先统计一下每个字母的个数,看看有多少对字母,通过这些字母可以构成回文串,但是漏看了剩下的落单的字母个数,因为我们要使当前的长度最小的回文串的长度尽可能的大,如果剩下来的字母的数量足够让每个字符串的长度都+1的话就可以实现了

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e8;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        memset(a, 0, sizeof a);
        string s;
        int n, k;
        cin >> n >> k;
        cin >> s;
        for (int i = 0; i < s.size();i++)
        {
            a[s[i]-'a']++;
        }
        int cnt1=0,cnt2=0;
        for (int i = 0; i < 26;i++)
        {
            cnt1 += a[i] / 2;
            if(a[i]&1)
                cnt2++;
        }
        int ans = cnt1 / k;
        if((cnt1-ans*k)*2+cnt2>=k)
            cout << ans * 2 + 1 << '\n';
        else cout << ans * 2 << '\n';
    }
    return 0;
}
posted @ 2022-03-23 23:27  menitrust  阅读(25)  评论(0编辑  收藏  举报