Educational Codeforces Round 42 (Rated for Div. 2) B. Students in Railway Carriage

It was complicated at first.

it's actually really easy. Just scan it one by one, prioritize the larger number each time, and then alternately arrange it.

#include <iostream>
#include <bits/stdc++.h>
using namespace std;
intmain()
{   char s[200005];
    int tot [100005] = {0};
    int n,a,b;
    cin>>n>>a>>b;
    int i,j;
    for(i=0;i<n;i++)
    {
        cin>>s[i];
    }
    int flag;//flag=1, put a, otherwise put b
    if(a>b) flag=1;
    else flag=0;
    int ans=0;
    for(i=0;i<n;i++)
    {
        if(s[i]=='.'&&a>0&&b>0)
        {
            if(flag==1)
            {
                a--;
                years++;
                flag=0;
            }
            else
            {
                b--;
                years++;
                flag=1;
            }
        }
        else if(s[i]=='.'&&a>0&&b==0)
        {
            a--;
            i++;
            years++;
        }
        else if(s[i]=='.'&&a==0&&b>0)
        {
            b--;
            i++;
            years++;
        }
        else if(s[i]=='*')//If * is encountered, re-compare the size of ab
        {
            if(a>b) flag=1;
            else flag=0;
        }
    }
    printf("%d\n",ans);
    return 0;
}

  

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324475091&siteId=291194637