其他分享
首页 > 其他分享> > 2022.4.24

2022.4.24

作者:互联网

Educational Codeforces Round 127 (Rated for Div. 2)

A - String Building

a,b必须同时出现

#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=1e9;
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        string s="c";
        cin >> s;
        s += "c";
        int cnt = 0, f = 0;
        for (int i = 0; i < s.length();i++)
        {
            if(s[i]=='a'&&s[i-1]!='a'&&s[i+1]!='a')
            {
                f = 1;
                break;
            }
        }
        if(f)
        {
            cout << "no\n";
            continue;
        }
        cnt = 0;
        for (int i = 0; i < s.length();i++)
        {
            if(s[i]=='b'&&s[i-1]!='b'&&s[i+1]!='b')
            {
                f = 1;
                break;
            }
        }
        if(f)
        {
            cout << "no\n";
            continue;
        }
        cout << "yes\n";
    }

    return 0;
}

B - Consecutive Points Segment

发现:最多只能有一次间隔为3,且其他间隔必须为1,否则失败,如:1 3 6则失败,2 3 6成功,最多只能有两次间隔为2,且不能有间隔为3,如:1 3 5 7,则失败,2 3 5 7成功。

#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=1e9;
int a[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        int n;
        cin >> n;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        int f = 0,cnt1=0,cnt2=0;
        for (int i = 2; i <= n;i++)
        {
            if(a[i]-a[i-1]>3)
            {
                f = 1;
                break;
            }
            else if(a[i]-a[i-1]==3)
            {
                cnt1++;
                if(cnt1>1||cnt2)
                {
                    f = 1;
                    break;
                }
            }
            else if(a[i]-a[i-1]==2)
            {
                cnt2++;
                if(cnt2>2||cnt1)
                {
                    f = 1;
                    break;
                }
            }
        }
        if (f)
            cout << "NO\n";
        else
            cout << "YES\n";
    }
      
    return 0;
}

C-Dolce Vita

考虑贪心,每次都先买价格最低的,于是按价格从小到大排序,如果枚举天数则会超时,如果枚举买1-n的糖果最多能买多少个则转化为求买1-n种糖果的前缀和,如果买一种,则之后的天数每天价格会加1,买2种则价格每天加2,n种每天加n。因此从1-n枚举买i种糖果的天数再求和。

#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=1e9;
ll a[N],sum[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin >> t;
    while(t--)
    {
        ll n, x;
        cin >> n >> x;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
        }
        sort(a + 1, a + 1 + n);
        for (int i = 1; i <= n;i++)
        {
            sum[i] = sum[i - 1] + a[i];
        }
        ll ans = 0;
        for (int i = 1; i <= n;i++)
        {
            if(sum[i]>x)//超过了就不能买
                continue;
            ll cnt = (x - sum[i]) / i + 1;//后面还能再买几次
            ans += cnt;
        }
        cout << ans<<'\n';
    }
      
    return 0;
}

标签:24,typedef,int,ll,cin,long,2022.4,include
来源: https://www.cnblogs.com/menitrust/p/16188359.html