C. Dasha and Password 预处理 + dp
http://codeforces.com/contest/761/problem/C
对于每一个字符串,可以预处理出其到达数字,字母,和特殊符号所需的最小步数。
然后就是在n个东西中,选出数字、字母和特殊符号,至少一个,所需的最少步数。
因为第一个选了数字的话,它就不能选字母的了,然后比赛的时候发现这就是二分图,然后就上了km的模板。(太笨了我)
虽然过了,但是明显正解不是这个。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
char str[maxn][maxn];
int e[ + ][ + ]; int match[maxn];//match[col] = row
int vx[maxn], vy[maxn];
int fx[maxn], fy[maxn];
int dfs (int u, int m) {
vx[u] = ;
int i;
for (i = ; i <= m; i++) { //筛选n个 导航员 col的值
if (vy[i] == && fx[u] + fy[i] == e[u][i]) {
vy[i] = ;
if (match[i] == || dfs(match[i], m)) {
match[i] = u; // match[col]=row;
return ;//搭配成功
}
}
}
return ;//我找不到啊,后面,就会执行km
}
void do_km(int n, int m) { //
int i, j;
int d = -inf;
for (i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
if (vx[i] == ) {
for (j = ; j <= m; j++) { //对他进行遍历导航员 col的值
if (!vy[j]) {
if (d < (fx[i] + fy[j] - e[i][j])) {
d = fx[i] + fy[j] - e[i][j];
}
}
}
}
}
for (i = ; i <= n; i++) {
if (vx[i] == ) {
fx[i] -= d;
vx[i] = ; //请0
}
if (vy[i] == ) { //
fy[i] += d;
vy[i] = ; //情0
}
}
return ;
}
int anskm(int n, int m) {
memset(vx, , sizeof(vx));
memset(vy, , sizeof(vy));
memset(fx, , sizeof(fx));
memset(fy, , sizeof(fy));
memset(match, , sizeof(match));
//km算法的一部分,先初始化fx,fy
for (int i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
fy[i] = ;
fx[i] = inf; //无穷小
for (int j = ; j <= m; j++) { //遍历每一个导航员 col的值
if (fx[i] > e[i][j]) { //默契值
fx[i] = e[i][j];
}
}
}
for (int i = ; i <= n; i++) { //遍历每一个驾驶员 row的值
memset(vx, , sizeof(vx));
memset(vy, , sizeof(vy));
while (!dfs(i, m)) {//如果他找不到搭配,就实现km算法
do_km(n, m);//km完后,还是会对这个想插入的节点进行dfs的,因为他还没搭配嘛
}
}
int ans = ;
for (int i = ; i <= m; i++) //遍历导航员,col的值
ans += e[match[i]][i];//输入的row是驾驶员,col是导航员
//match[i]:导航员i和驾驶员match[i]搭配了 match[col]=row;
return ans;
} void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
e[i][j] = ;
}
}
for (int i = ; i <= n; ++i) {
scanf("%s", str[i] + );
int a = , b = , c = ;
int len = m;
for (int j = ; str[i][j]; ++j) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, j - );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, j - );
} else {
c = min(c, j - );
}
}
for (int j = len; j >= ; j--) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, len - j + );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, len - j + );
} else {
c = min(c, len - j + );
}
}
e[i][] = a;
e[i][] = b;
e[i][] = c;
}
// for (int i = 1; i <= n; ++i) {
// for (int j = 1; j <= n; ++j) {
// cout << e[i][j] << " ";
// }
// cout << endl;
// }
cout << anskm(n, n) - (n - ) * << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
km
正解因该是一个dp
dp[i][2][2]][2]表示在前i个字符串中,选出了数字(0 or 1)、字母、特殊符号所需的最小步数。
转移
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c]); //保留上次的最小值。
然后对于每一个a、b、c
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a - 1][b][c] + e[i][1]);之类的。
一开始还担心会不会使得同一个字符串,既选了数字,也选了字母。
然后是不会的,因为需要选数字的时候,是从dp[!now][a - 1][b][c]转移过来的,也就是从上一唯的b、c转过来。所以不会有重复。
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = + ;
char str[maxn][maxn];
int e[ + ][ + ];
int dp[][][][];
void work() {
int n, m;
scanf("%d%d", &n, &m);
for (int i = ; i <= ; ++i) {
for (int j = ; j <= ; ++j) {
e[i][j] = ;
}
}
for (int i = ; i <= n; ++i) {
scanf("%s", str[i] + );
int a = , b = , c = ;
int len = m;
for (int j = ; str[i][j]; ++j) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, j - );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, j - );
} else {
c = min(c, j - );
}
}
for (int j = len; j >= ; j--) {
if (str[i][j] >= '' && str[i][j] <= '') {
a = min(a, len - j + );
} else if (str[i][j] == '#' || str[i][j] == '*' || str[i][j] == '&') {
b = min(b, len - j + );
} else {
c = min(c, len - j + );
}
}
e[i][] = a;
e[i][] = b;
e[i][] = c;
}
memset(dp, 0x3f, sizeof dp);
int now = ;
dp[now][][][] = ;
for (int i = ; i <= n; ++i) {
now = !now;
for (int a = ; a <= ; ++a) {
for (int b = ; b <= ; ++b) {
for (int c = ; c <= ; ++c) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c]);
if (a) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a - ][b][c] + e[i][]);
// break;
}
if (b) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b - ][c] + e[i][]);
// break;
}
if (c) {
dp[now][a][b][c] = min(dp[now][a][b][c], dp[!now][a][b][c - ] + e[i][]);
// break;
}
}
}
}
}
cout << dp[now][][][] << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
C. Dasha and Password 预处理 + dp的更多相关文章
- Codeforces Round #394 (Div. 2) C. Dasha and Password(简单DP)
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces 761C Dasha and Password(枚举+贪心)
题目链接 Dasha and Password 题目保证一定有解. 考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费. 然后O(N^3)枚举求最小值即可. 时间 ...
- Div.2 C. Dasha and Password
C. Dasha and Password time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- Codeforces Round #394 (Div. 2) C. Dasha and Password —— 枚举
题目链接:http://codeforces.com/problemset/problem/761/C C. Dasha and Password time limit per test 2 seco ...
- codeforces 761 C. Dasha and Password(多维dp)
题目链接:http://codeforces.com/contest/761/problem/C 题意:给出n行的字符串每一列都从第一个元素开始可以左右移动每一行字符串都是首位相连的. 最后问最少移动 ...
- BZOJ-1587|前缀和 预处理 dp||叶子合并leaves
叶子合并leaves Description 在一个美丽的秋天,丽丽每天都经过的花园小巷落满了树叶,她决定把树叶堆成K堆,小巷是笔直的 共有N片树叶(树叶排列也是笔直的),每片树叶都有一个重量值,并且 ...
- 2013-2014 ACM-ICPC, NEERC, Southern Subregional Contest Problem H. Password Service dp
Problem H. Password Service 题目连接: http://www.codeforces.com/gym/100253 Description Startups are here ...
随机推荐
- CxImage的编译及简单使用举例
1. 从http://sourceforge.net/projects/cximage/下载最新的CxImage 702源代码. 2. 解压缩后,以管理员身份打开CxImageFull_vc10. ...
- 使用RNN解决句子对匹配问题的常见网络结构
/* 版权声明:能够随意转载,转载时请标明文章原始出处和作者信息 .*/ author: 张俊林 除了序列标注问题外,句子对匹配(Sentence Pair Matching)问题也是NLP中非经常见 ...
- 鸟哥的Linux私房菜-----12、学习使用Shell scripts
- URL传参中文乱码的一种解决方法
中文乱码是由于,发送和接收方使用的编码解码格式不一致导致,以下是关于url传参解决中文乱码的一种方法,最后根据各种编码格式尝试解码,发现正确的解码格式 string strQueryString = ...
- Codeforces Beta Round #22 (Div. 2 Only) E. Scheme dfs贪心
E. Scheme To learn as soon as possible the latest news about their favourite fundamentally new ope ...
- 内存溢出-jvisualvm排查问题
先来一段能够内存溢出的程序 public static void main(String[] args) { List<Object> list = new ArrayList<&g ...
- 使用linux内核hrtimer高精度定时器实现GPIO口模拟PWM,【原创】
关键词:Android linux hrtimer 蜂鸣器 等待队列 信号量 字符设备 平台信息:内核:linux3.4.39 系统:android/android5.1平台:S5P4418 作 ...
- 安装linux各种桌面环境
1.安装kde ①添加 KDE SC 4.11 库 打开一个终端窗口,在终端窗口中输入如下命令: sudo add-apt-repository ppa:kubuntu-ppa/backports 回 ...
- YTU 2438: 三人三鬼
2438: 三人三鬼 时间限制: 1 Sec 内存限制: 128 MB 提交: 35 解决: 9 题目描述 目标是将东岸的3人3鬼通过一只小船转移到西岸,希望以尽可能少的摆渡次数. 船的容量有限, ...
- lucene DocValues——本质是为通过docID查找某field的值 看图
Why DocValues? The standard way that Solr builds the index is with an inverted index. This style bui ...