CodeForces 790C Bear and Company
来源:互联网 发布:逸夫淘宝秒杀神器 编辑:程序博客网 时间:2024/06/05 14:34
CodeForces 790C Bear and Company
题目描述:
输入一个字符串,你可以进行一些操作,每次操作可以交换两个相邻的字符。问至少进行几次操作,才能使这个字符串不包含子串 VK 。
题解:
我们发现其实字符串中的字符只有三种:V ,K ,和其它的(至于是 A 、 B 、 C 还是什么其实并不影响)。
令
本题
题目链接: vjudge 原网站
代码:
#include <cstdio>#include <cstdlib>#include <cstring>#include <algorithm>using namespace std;#define MAXN 100static int N;static int pos[3][MAXN], num[3][MAXN], dp[MAXN][MAXN][MAXN][2];static char S[MAXN];inline int move(int i, int j, int k, int p){ return max(0, num[0][p] - i) + max(0, num[1][p] - j) + max(0, num[2][p] - k) - 1;}int main(){ scanf("%d%s", &N, S + 1); for (int i = 1; i <= N; i++) { num[0][i] = num[0][i-1]; num[1][i] = num[1][i-1]; num[2][i] = num[2][i-1]; if (S[i] == 'V') pos[0][num[0][i]++] = i; else if (S[i] == 'K') pos[1][num[1][i]++] = i; else pos[2][num[2][i]++] = i; } memset(dp, 0x3f, sizeof(dp)); dp[0][0][0][0] = 0; for (int i = 0; i <= num[0][N]; i++) for (int j = 0; j <= num[1][N]; j++) for (int k = 0; k <= num[2][N]; k++) for (int is_v = 0; is_v < 2; is_v++) { if (i < num[0][N]) dp[i+1][j][k][1] = min(dp[i+1][j][k][1], dp[i][j][k][is_v] + move(i, j, k, pos[0][i])); if (j < num[1][N] && !is_v) dp[i][j+1][k][0] = min(dp[i][j+1][k][0], dp[i][j][k][is_v] + move(i, j, k, pos[1][j])); if (k < num[2][N]) dp[i][j][k+1][0] = min(dp[i][j][k+1][0], dp[i][j][k][is_v] + move(i, j, k, pos[2][k])); } printf("%d\n", min(dp[num[0][N]][num[1][N]][num[2][N]][0], dp[num[0][N]][num[1][N]][num[2][N]][1])); return 0;}
提交记录(AC / Total = 1 / 1):
0 0
- CodeForces 790C Bear and Company
- CodeForces-771D-Bear and Company
- Codeforces Round #405 Div. 1 Bear and Company(DP)
- Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) C. Bear and Company(DP)
- Codeforces 385 C. Bear and Prime Numbers
- CodeForces 385C Bear and Prime Numbers
- Codeforces 385C Bear and Prime Numbers
- Codeforces 385 C Bear and Prime Numbers
- codeforces 385C Bear and Prime Numbers
- CodeForces 574C - Bear and Poker
- Codeforces Round #318 C - Bear and Poker
- CodeForces #318 (div1) C.Bear and Drawing
- Codeforces Round #318-(C. Bear and Poker)
- codeforces 573C. Bear and Drawing
- Codeforces C - Bear and String Distance (贪心)
- codeforces 628C Bear and String Distance
- CodeForces 628C - Bear and String Distance
- codeforces 639C Bear and Polynomials(数学)
- 基于用户投票的排名算法
- iOS经典错误Undefined symbols for architecture XXX:
- 浅析Linux中的零拷贝技术
- 如何快速转载CSDN中的博客
- ImportError: cannot import name webdriver解决方案
- CodeForces 790C Bear and Company
- C# 中的四种访问修饰符
- 第六届蓝桥杯【国赛试题3】关联账户
- 洛谷 P2338 [USACO14JAN]失败的滑雪Bessie Slows Down
- C#微信公众号全攻略(3)--接管所有消息验证部分 C#代码
- eclipse实用快捷键
- OSI七层模型详述
- PHP include()与require()的区别
- bzoj2049 [Sdoi2008]Cave 洞穴勘测