Black and white stones(UVALive 6822)

Description

Shagga and Dolf like to play a game with stones, each of which is either black or white. At the beginning
of the game, Dolf arranges all the stones in a single line from left to right. Then, Shagga’s goal is to
reorder the stones so that all the black stones are to the left of all the white stones. To do this, he
can choose any pair of stones of different color and swap their positions, paying A coins to Dolf in the
process. However, if the two stones whose positions he is swapping are adjacent, Dolf must give him a
refund of B coins, meaning that the operation will effectively cost Shagga only A − B coins.
Shagga is not very bright, so he hasn’t realized yet that he will only loose coins while playing this
game. However, he is aware of his limitations, so he knows that if he played optimally he would loose
fewer coins than he is loosing right now, with his strategy of randomly choosing the stones he swaps in
each movement. Therefore, he wants to know the minimum number of coins he will have to pay Dolf
in order to get to the desired arrangement of stones, and is threatening to feed you to the goats if you
don’t help him.

Input

The input contains several test cases; each test case is formatted as follows. The first line contains two
integers A and B (0 ≤ B < A ≤ 106
), representing respectively the cost of swapping two stones and
the value of the refund when swapping adjacent stones. The second line contains a non-empty string
S of at most 5000 characters. The i-th character of S indicates the color of the i-th stone, from left to
right, in the initial arrangement of the stones. The character is either the uppercase letter ‘B’ or the
uppercase letter ‘W’, indicating respectively a black or a white stone.

Output

For each test case in the input, output a line with an integer representing the minimum number of coins
Shagga will have to pay Dolf in order to arrange the stones such that all the black ones are to the left
of all the white ones.

Sample Input

2 1
BWWB
5 3
WBWWBWBWBWBBBWWBBB
1000000 0
W

Sample Output

2
27
0

题解:

有黑白两种石头,用最小的代价将黑的全部放到前面,白的放到后面,贪心即可。注意,要从中间往两边找,这样才能保证取得最小值。

代码如下:
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<string>
#include<vector>
#include<cmath>
#include<stack>
#include<queue>
#include<map>
#include<set>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define maxn 27
#define N 100000000
#define INF 0x3f3f3f3f
#define mod 1001113
#define e  2.718281828459045
#define eps 1.0e18
#define PI acos(-1)
#define lowbit(x) (x&(-x))
#define read(x) scanf("%d",&x)
#define put(x) prllf("%d\n",x)
#define memset(x,y) memset(x,y,sizeof(x))
#define Debug(x) cout<<x<<" "<<endl
#define lson i << 1,l,m
#define rson i << 1 | 1,m + 1,r
#define ll long long
//std::ios::sync_with_stdio(false);
//cin.tie(NULL);
//const int maxn=;
using namespace std;

char a[5555];
int main()
{
    ll m,n;
    while(cin>>m>>n)
    {
        memset(a,0);
        cin>>a;
        int l=strlen(a),k=0;
        for(int i=0;i<l;i++)
        {
            if(a[i]=='B')
                k++;
        }
        ll sum=0;
        for(int i=k-1;i>=0;i--)
        {
            int minn=INF,s=i;
            if(a[i]=='W')
            {
                for(int j=k;j<l;j++)
                {
                    if(a[j]=='B')
                    {
                        minn=min(min((j-i)*(m-n),m),minn);
                        s=j;
                        break;
                    }
                }
                swap(a[i],a[s]);
                sum+=minn;
                //cout<<a<<endl;
            }
        }
        cout<<sum<<endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值