Codeforces Round 910 (Div. 2)

Codeforces Round 910 (Div. 2)

文章目录

  • Codeforces Round 910 (Div. 2)
    • A
    • B
    • C
    • D

A

模拟

#include 

using namespace std;
const int N=1e5+10;
char s[N];

void solve(){
    int n , k , cb = 0 , ans = 0;
    cin >> n >> k;
    for(int i = 1 ; i <= n ; i ++){
        cin >> s[i];
        if(s[i] == 'B')cb ++;
    }
    if(cb == k){
        cout << "0\n";
        return ;
    }else{
        cout << "1\n";    
    }
    while(cb < k){
        for(int i = 1 ; i <= n ; i ++){
            if(s[i] == 'A'){
                cb ++;
                if(cb == k){
                    cout << i << " B" << endl;
                    return ;
                }
            }
        }
    }
    while(cb > k){
           for(int i = 1 ; i <= n ; i ++){
            if(s[i] == 'B'){
                cb --;
                if(cb == k){
                    cout << i << " A" << endl;
                    return ;
                }
            }
        }     
    }
}

int main()
{
    int T;
    cin >> T;
    while(T --){
        solve();
    }
    return 0;
}

B

最后一个不能分解,依次往前计算

#include 

using namespace std;
const int N = 2e5 + 10;
int a[N];

void solve()
{

    int64_t n, res = 0;
    cin >> n;
    for (int i = 1; i <= n; i++)
        cin >> a[i];
    for (int i = n - 1; i >= 1; i--)
    {
        if (a[i] > a[i + 1])
        {
            res += ceil(1.0 * a[i] / a[i + 1]) - 1;
            a[i] = a[i] / ceil(1.0 * a[i] / a[i + 1]);
        }
    }
    cout << res << endl;
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

C

一半红一半蓝,当多出奇数时非法

#include 

using namespace std;
const int N = 2e5 + 10;
int a[N];

void solve()
{
    int n , m , k;
    cin >> n >> m >> k;
    if(n+m-2>k||(k-(n+m-2))%2){
        cout << "No\n";
        return ;
    }
    cout<<"YES"<<endl;
        for(int i=1;i<=n;i++){
            int t=1;
            for(int j=1;j<m;j++){
                if(t){
                    cout<<"R ";
                }else{
                    cout<<"B ";
                }
                t^=1;
            }
            cout<<endl;
        }
        for(int i=1;i<n;i++){
            for(int j=1;j<=m;j++){
                if(j<=2&&i==1){
                    cout<<"B ";
                }else{
                    if((i+j)%2==0){
                        cout<<"R ";
                    }else{
                        cout<<"B ";
                    }
                }
            }
            cout<<endl;
        }
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

D

将每对a,b抽象为线段
相隔时会增加,找相隔最大的

#include 

using namespace std;
const int N = 2e5 + 10;
int a[N] , b[N];

void solve()
{
    long long ans=0 , n;
    cin>>n;
    for(int i=1;i<=n;i++)cin>>a[i];
    for(int i=1;i<=n;i++)cin>>b[i];
    for(int i=1;i<=n;i++)
    ans+=abs(a[i]-b[i]);
    int maxn=0,minn=1e18;
    //找到最小的右端点和最大的左端点
    for(int i=1;i<=n;i++)
    {
        maxn=max(maxn,min(a[i],b[i]));
        minn=min(minn,max(a[i],b[i]));
    }
    cout<<max(ans,ans+2*(maxn-minn))<<'\n';
}

int main()
{
    int T;
    cin >> T;
    while (T--)
    {
        solve();
    }
    return 0;
}

你可能感兴趣的:(算法,c++,算法,数据结构,开发语言)