Regionals 2014 >> Latin America 6822 - Black a
来源:互联网 发布:淘宝客白菜群软件 编辑:程序博客网 时间:2024/05/18 02:14
6822
Black and white stones
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
Black and white stones
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
题意:把所有的黑色(B)移到左边、白色(W)移到右边的最小花费。每交换,花费为a,若交换的为相邻位置则只需花费a-b
#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <cmath>#include <vector>using namespace std;typedef long long ll;#define MAX 20005#define PI acos(-1.0)ll min(ll a,ll b){if(a > b) return b;else return a;}ll f[50005]; //在此位置前(包含自己)有多少个B ll cnt[50005]; //在第i个B前有多少个W int main(){ll a,b;while(~scanf("%lld%lld", &a, &b)){string s;cin >> s;ll len = (ll)s.size();ll tot = 0;b = a-b;memset(f,0,sizeof(f));memset(cnt,0,sizeof(cnt));int n = 1;for(ll i = 0;i < len;i++){if(s[i] == 'B'){f[i] = 1;cnt[n] = tot;n ++;}else{cnt[n] = ++tot;}}n --;for(ll i = 1;i < len;i++) f[i] += f[i-1];for(ll i = 1;i <= n;i++) cnt[i] += cnt[i-1];ll mmin = b*cnt[n];for(ll i = 0;i < len;i++){if(s[i] == 'W'){ll tmp = i+1-f[i]; //在此i前有多少个W(包括自己) if(n-tmp-f[i] < 0) break; //注意跳出 //cout << mmin << " " << tmp*a << " " << b << " " << cnt[n-tmp]-cnt[f[i]] << " " << n-tmp-f[i] << endl;mmin = min(mmin,tmp*a+b*(cnt[n-tmp]-cnt[f[i]]-tmp*(n-tmp-f[i])));}}printf("%lld\n", mmin);}return 0;}/*4 3BWWWWWBWBWBWWBWBWBWWWB6 2BWWBWBWWWBWBBBBW*/
0 0
- Regionals 2014 >> Latin America 6822 - Black a
- Regionals 2014 >> Latin America
- Regionals 2014 >> Latin America >> 6823 - Counting substhreengs
- Regionals 2014 >> Latin America >> 6828 - Help cupid
- ICPC 6828 Help cupid(Regionals 2014 >> Latin America)
- 20130902组队赛Regionals 2012, Latin America
- 130902组队赛-Regionals 2012, Latin America
- 20130926组队赛-Regionals 2011, Latin America
- 组队赛130926 Regionals 2011, Latin America
- Regionals 2014 >> North America - Greater NY >> 7097 - Growing Rectangular Spiral
- Regionals 2014 >> North America - Greater NY >> 7094 - Happy Happy Prime Prime
- Regionals 2013 :: North America - Greater NY
- Latin America 2010 _problem D_digits count
- 130825组队赛-Regionals 2012, North America - East Central NA
- 20130827组队赛Regionals 2012, North America - Greater NY
- 130827组队赛Regionals 2012, North America - Greater NY
- Regionals 2012, North America - Greater NY 解题报告
- 20131002组队赛-Regionals 2011, North America - Rocky Mountain
- JAVA 基础笔记之一 char类型变量能否保存一个汉字
- java 构造方法
- 天声人語 20150903
- 日经春秋 20150903
- SLURM 节点状态总是drained问题
- Regionals 2014 >> Latin America 6822 - Black a
- 使用FragmentTabhost代替Tabhost onCreatView 每次都被调用
- [水题]杭电1004
- 【HDU2444】【匈牙利算法】【二分匹配】【求最大匹配】【染色】
- GUI
- 使用@Controller注解为什么要配置<mvc:annotation-driven />
- dhcpd .conf的配置
- Fragment(碎片)-------让界面在平板上一样炫丽美观
- CC2541 OAD无线升级遇到问题总结