其他分享
首页 > 其他分享> > 2022.6.9

2022.6.9

作者:互联网

Codeforces Round #797 (Div. 3)

A - Print a Pedestal

#include<bits/stdc++.h>
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--)
    {
        int n;
        cin >> n;
        int cnt = n / 3;
        int mod = n % 3;
        if(mod==0)
        {
            cout << cnt  <<' '<< cnt + 1 << ' '<< cnt -1 << '\n';
        }
        else if(mod==1)
        {
            cout << cnt  <<' '<< cnt + 2 << ' '<< cnt-1 <<'\n';
        }
        else
        {
            cout << cnt +1 << ' '<<cnt + 2 << ' ' <<cnt - 1 << '\n';
        }
    }
      
    return 0;
}

B - Array Decrements

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=5e4+10,INF=1e9;
int a[N], b[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n;
        cin>>n;
        int mmax = 0;
        int pos = 0;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
            if(a[i]>mmax)
            {
                mmax = a[i];
                pos = i;
            }
        }
        for (int i = 1; i <= n;i++)
        {
            cin >> b[i];
        }
        if(mmax>=b[pos])
        {
            int f = 0;
            int res = mmax - b[pos];
            for (int i = 1; i <= n;i++)
            {
                a[i] -= res;
                if(a[i]<0)
                    a[i] = 0;
                if(a[i]!=b[i])
                {
                    f = 1;
                    break;
                }
            }
            if(f)
                cout << "NO\n";
            else
                cout << "YES\n";
        }
        else
            cout << "NO\n";
    }
      
    return 0;
}

C - Restoring the Duration of Tasks

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e9;
int a[N], b[N],ans[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];
        }
        for (int i = 1; i <= n;i++)
        {
            cin >> b[i];
        }
        ans[1] = b[1] - a[1];
        for (int i = 2; i <= n;i++)
        {
            if(a[i]<b[i-1])
            {
                ans[i] = b[i] - b[i - 1];
            }
            else
            {
                ans[i] = b[i] - a[i];
            }
        }
        for (int i = 1; i <= n ;i++)
        {
            cout << ans[i] << " \n"[i == n];
        }
    }
      
    return 0;
}

D - Black and White Stripe

用双指针做的但是麻烦了,直接区间+前缀和即可
双指针代码

#include <bits/stdc++.h>
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--)
    {
        int n, k;
        cin >> n >> k;
        string s;
        cin >> s;
        vector<int> pos;
        for (int i = 0; i < s.length(); i++)
        {
            if (s[i] == 'B')
            {
                pos.push_back(i + 1);
            }
        }
        if (pos.size() == 1)
        {
            cout << k - 1 << '\n';
            continue;
        }
        if (pos.size() == 0)
        {
            cout << k << '\n';
            continue;
        }
        pos.push_back(INF);
        int ans = INF;
        int j = 0;
        for (int i = 1; i < pos.size(); i++)
        {
            if((pos[i] - pos[j] + 1) > k)
            {
                int res = (k - (i - j));
                ans = min(res, ans);
                //cout << i << ' ' << j << '\n';
                j++;
            }
        }
        cout << ans << '\n';
    }

    return 0;
}

前缀和代码

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int N=2e5+10,INF=1e9;
char a[N];
int sum[N];
int main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    int t;
    cin>>t;
    while(t--)
    {
        int n, k;
        cin >> n >> k;
        for (int i = 1; i <= n;i++)
        {
            sum[i] = 0;
        }
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            if (a[i] == 'B')
                sum[i] = 1;
        }
        for (int i = 1; i <= n;i++)
        {
            sum[i] += sum[i - 1];
        }
        int ans = INF;
        for (int i = 1; i + k - 1<= n;i++)
        {
            int res = k - (sum[i + k - 1] - sum[i - 1]);
            ans = min(ans, res);
        }
        cout << ans << '\n';
    }
    return 0;
}

E. Price Maximization

每次都找k的倍数或者与k的倍数接近且比k的倍数大的数,利用二分或双指针

#include<bits/stdc++.h>
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, k;
        cin >> n >> k;
        ll ans = 0;
        multiset<int> st;
        for (int i = 1; i <= n;i++)
        {
            cin >> a[i];
            ans += a[i] / k;
            a[i] %= k;
            st.insert(a[i]);
        }
        while(!st.empty())
        {
            int now = *st.begin();
            st.erase(st.begin());
            auto it = st.lower_bound(k - now);
            if(it!=st.end())
            {
                st.erase(it);
                ans++;
            }
        }
        cout << ans << '\n';
    }
      
    return 0;
}

标签:typedef,int,cin,long,st,ans,2022.6
来源: https://www.cnblogs.com/menitrust/p/16358900.html