Squats CodeForces - 424A

Pasha has many hamsters and he makes them work out. Today, n hamsters (n is even) came to work out. The hamsters lined up and each hamster either sat down or stood up.

For another exercise, Pasha needs exactly hamsters to stand up and the other hamsters to sit down. In one minute, Pasha can make some hamster ether sit down or stand up. How many minutes will he need to get what he wants if he acts optimally well?

Input
The first line contains integer n (2 ≤ n ≤ 200; n is even). The next line contains n characters without spaces. These characters describe the hamsters’ position: the i-th character equals ‘X’, if the i-th hamster in the row is standing, and ‘x’, if he is sitting.

Output
In the first line, print a single integer — the minimum required number of minutes. In the second line, print a string that describes the hamsters’ position after Pasha makes the required changes. If there are multiple optimal positions, print any of them.
題意:一個字符串有n個字符,每個字符不是x就是X,現要求讓x和X的個數相等,輸出最小的轉換次數和轉換後的字符串。
思路:統計x和X的個數設爲a,b,c=min(a,b),轉換最小次數爲n/2-a,隨便轉換幾個x或者X然後輸出就行了。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
int main()
{
    int n;
    char c[205];
    int s=0;
    while(cin>>n&&n!=0)
    {
        int a=0,b=0,t=0;
        for(int i=0;i<n;i++)
        {
            cin>>c[i];
            if(c[i]=='x')
                a++;
            else
                b++;
        }
        if(a<n/2)
        {
            t=n/2-a;
            s=t;
            for(int i=0;i<n;i++)
            {
                if(t==0)
                    break;
                if(c[i]=='X')
                {
                    c[i]='x';
                    t--;
                }
            }
        }
        else
        {
            t=n/2-b;
            s=t;
            for(int i=0;i<n;i++)
            {
                if(t==0)
                    break;
                if(c[i]=='x')
                {
                    c[i]='X';
                    t--;
                }
            }
        }
        cout<<s<<endl;
        for(int i=0;i<n;i++)
        {
            cout<<c[i];
        }
        cout<<endl;
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章