Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
Description
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string s that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move.
Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string s.
What is the minimum possible number of moves Limak can do?
The first line of the input contains an integer n (1 ≤ n ≤ 75) — the length of the string.
The second line contains a string s, consisting of uppercase English letters. The length of the string is equal to n.
Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".
4
VKVK
3
5
BVVKV
2
7
VVKEVKK
3
20
VKVKVVVKVOVKVQKKKVVK
8
5
LIMAK
0
In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:
- Swap two last letters. The string becomes "VKKV".
- Swap first two letters. The string becomes "KVKV".
- Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK".
In the second sample, there are two optimal sequences of moves. One is "BVVKV" → "VBVKV" → "VVBKV". The other is "BVVKV" → "BVKVV" → "BKVVV".
In the fifth sample, no swaps are necessary.
题意:给一个字符串,我们可以移动其中的字符,使得里面字符没有VK的最少次数是多少
解法:如果我们移动K,那么就没必要再选V移动了
dp[i][j][z][v]表示当前有i个V,j个K,z个其他字符,0表示当前字符不是V,1表示当前是V时,转移的最小代价
我们处理一下V,K的位置关系,然后转移就行,具体代码可以看明白
cmd函数计算当前转移需要增加的代价
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; #define MAXN 100
int n;
char s[MAXN];
int dp[MAXN][MAXN][MAXN][];
int pos[][MAXN],num[][MAXN];
inline int cmd(int i, int j, int k, int p)
{
return max(, num[][p] - i) + max(, num[][p] - j) + max(, num[][p] - k) - ;
} void init()
{
for(int i=;i<MAXN;i++)
{
for(int j=;j<MAXN;j++)
{
for(int z=;z<MAXN;z++)
{
for(int x=;x<;x++)
{
dp[i][j][z][x]=;
}
}
}
}
dp[][][][]=;
}
int main()
{
init();
scanf("%d%s",&n,s+);
for(int i=;i<=n;i++)
{
num[][i]=num[][i-];
num[][i]=num[][i-];
num[][i]=num[][i-];
if(s[i]=='V')
{
pos[][num[][i]++]=i;
}
else if(s[i]=='K')
{
pos[][num[][i]++]=i;
}
else
{
pos[][num[][i]++]=i;
}
}
int a=num[][n];
int b=num[][n];
int c=num[][n];
for(int i=;i<=a;i++)
{
for(int j=;j<=b;j++)
{
for(int z=;z<=c;z++)
{
for(int v=;v<;v++)
{
if(i<a)
{
dp[i+][j][z][]=min(dp[i+][j][z][],dp[i][j][z][v]+cmd(i,j,z,pos[][i]));
}
if(j<b)
{
dp[i][j+][z][]=min(dp[i][j+][z][],dp[i][j][z][]+cmd(i,j,z,pos[][j]));
}
if(z<c)
{
dp[i][j][z+][]=min(dp[i][j][z+][],dp[i][j][z][v]+cmd(i,j,z,pos[][z]));
}
}
}
}
}
printf("%d\n", min(dp[a][b][c][], dp[a][b][c][]));
return ;
}
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E的更多相关文章
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
如果某个位置i是Y,直接直到i+m-1为止填上新的数字. 如果是N,直接把a[i+m-1]填和a[i]相同即可,这样不影响其他段的答案. 当然如果前面没有过Y的话,都填上0就行了. #include& ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
Description In the army, it isn't easy to form a group of soldiers that will be effective on the bat ...
随机推荐
- linux进程间通信消息队列:msgsnd: Invalid argument
今天写了个消息队列的小测试程序结果send端程序总是出现:msgsnd: Invalid argument,搞了半个小时也没搞明白,后来查资料发现我将(st_msg_buf.msg_type = 0; ...
- NAND flash坏区
计算容量 厂家所说的4G指的是4 000 000 000字节,是按1000进制计算的,而电脑是按照1024进制计算的,所以标称为4G的NAND Flash理论容量是4 000 000 000 / 10 ...
- Appium basic UI check cases_from sample
@Test public void testUIComputation() throws Exception { // populate text fields with values populat ...
- MVC Hidden用法
@Html.Hidden("DataSeriID",ViewBag.DataSeriID as string) 第一个参数相当于生成的ID值,后面的参数是String类型的数据,V ...
- CentOS笔记-用户和用户组管理
Linux系统是一个多用户多任务的分时操作系统,任何一个要使用系统资源的用户,都必须首先向系统管理员申请一个账号,然后以这个账号的身份进入系统. 1.添加新的用户账号使用useradd命令,其语法如下 ...
- monitor and move the log content to our big data system
Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...
- Handler有何作用?怎样使用?
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/u012974916/article/details/24580405 一 Handler作用和概念 ...
- min-width 和 @media screen
min-width可以容器设置最小宽度,低于改宽度时,会自动加上滚动条,支持ie7及ie7+: @media only screen and (min-width: /*最小宽度(要加单位px)*/) ...
- RK3288 GPIO 输出问题【转】
本文转载自:http://m.blog.csdn.net/jiangdou88/article/details/50158673 #define GPIO_BANK0 (0 ...
- .Net线程池ThreadPool导致内存高的问题分析
最近写了一个WinFrom程序.此程序侦听TCP端口,接受消息处理,然后再把处理后的消息,利用线程池通过WebService发送出去(即一进一出). 在程序编写完成后,进行压力测试.用Fiddler提 ...