codeforces - 864A

题意:给你一些牌,两个人可以在开始之前选一个数字,并且拿走该数字的所有牌,要求是两个人拿数量必须一样,并且最后不允许省牌。

思路:只要这些牌中只有两种牌,并且数目相同,那就可以。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[101] = {0};
int main()
{
    int n;
    cin>>n;
    int i;
    for(i = 1;i <= n;i++)
    {
        cin>>a[i];

    }
    sort(a+1,a+1+n);
    int cnt = 1;
    int f = a[1];
    for(i = 2;i<=n;i++)
    {
        if(a[i]!=f)
        {
            cnt++;
            f = a[i];
        }
    }


    if(cnt!=2)
    {
        cout<<"NO"<<endl;
    }
    else
    {
        sort(a+1,a+1+n);
        if(a[n/2] != a[n/2+1]){
            cout<<"YES"<<endl;
            cout<<a[n/2]<<" "<<a[n/2+1]<<endl;
        }
        else cout<<"NO"<<endl;
    }
}

CodeForces - 758B

题意: 一串字符串,由RBYG!五种字符组成,分别代表红 蓝 黄 绿 和坏掉的灯。问你各种颜色的灯需要几个才能修好这串灯。

思路:这道题的关键就是找出四个灯的相对顺序,剩下的就是遍历了,我们知道在一个循环中,每个颜色的灯的相对位置是确定的,循环周期是四,那么我们只要用4去除得到的余数就是在一个循环中,这个灯所在的位置。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int a[101] = {0};
int main()
{
    char a[4];
    string t;
    cin>>t;
    int i;
    for(i = 0;i < t.length();i++)
    {
        if(t[i]!='!')
        {
            a[i%4] = t[i];
        }
    }
    map<char,int> s = {{'R',0},{'B',0},{'Y',0},{'G',0}};
    for(i = 0;i < t.length();i++)
    {
        if(t[i]=='!')
        {
            s[a[i%4]]++;
        }
    }
    cout<<s['R']<<" "<<s['B']<<" "<<s['Y']<<" "<<s['G']<<endl;
}

CodeForces - 758A

题意:有n个公民,请问国王需要发多少钱来让大家保证平均。

题意:求每个数字与最大值的差然后加和就ok。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
map<int ,int >a;
int main()
{
    int n;
    cin>>n;
    int a[n];
    for(int i = 0;i < n;i++)
        cin>>a[i];
    sort(a,a+n);
    if(n==1)
        cout<<0<<endl;
    else
    {
        int m = a[n-1];
        int sum = 0;

        for(int i = 0;i < n-1;i++)
        {
            sum+=m-a[i];
        }
        cout<<sum<<endl;
    }
}

CodeForces - 864B

题意:数一下最长的小写字符串里字母的个数,如果没有小写字母那就看大写。

思路:暴力,用set来存储,最后取一下最大值。

#include<bits/stdc++.h>
using namespace std;
set<char > str;
int main()
{
    int n;
    cin>>n;
    string s;
    cin>>s;
    int res = 0;
    for(int i = 0;i < s.length();i++)
    {
        if(s[i] >= 'A'&&s[i] <= 'Z')
        {
            res = max(res,(int)str.size());
            str.clear();
        }
        else
        {
            str.insert(s[i]);
        }
    }
    res = max(res,(int)str.size());
    cout<<res<<endl;

}