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 ...
随机推荐
- Linux 编译安装Boost
linux平台下要编译安装除gcc和gcc-c++之外,还需要两个开发库:bzip2-devel 和python-devel,因此在安装前应该先保证这两个库已经安装: #yum install gcc ...
- C++中防止STL中迭代器失效——map/set等关联容器——vector/list/deque等序列容器—如何防止迭代器失效—即erase()的使用
序列性容器::(vector和list和deque) erase迭代器不仅使所有指向被删元素的迭代器失效,而且使被 删元素之后的所有迭代器失效,所以不能使用erase(iter++)的方 式, ...
- 利用wxpython编写GUI
首先建立一个简单的布局合理的图形界面,但是其中按下按键没有事情发生 #encoding=utf-8 __author__ = 'heng' #编写一个GUI import wx app = wx.Ap ...
- 常用近百个js代码汇总
//檢查空串 function isEmpty(str){ )) return (true); else return(false); } //檢查是否未數字 function isDigit(the ...
- CocoaPoda在iOS开发中的使用
CocoaPoda在iOS开发中的使用 CocoaPods 简介 CocoaPods是iOS开发中不可避免的依赖管理第三方的工具,能简化一些第三方库文件需要添加编译参数及依赖库的繁复工作 CocoaP ...
- [IT学习]Python pandas 学习
今天学习pandas来处理数据,结果用python 3.5.0的shell来调试,总是报错. 报错中包含如下字样: Traceback (most recent call last): File &q ...
- 5.2【Linux 内核网络协议栈源码剖析】socket 函数剖析 ☆☆☆
深度剖析网络协议栈中的 socket 函数,可以说是把前面介绍的串联起来,将网络协议栈各层关联起来. 应用层 FTP SMTP HTTP ... 传输层 TCP UDP 网络层 IP ICMP ARP ...
- YTU 1005: 渊子赛马
1005: 渊子赛马 时间限制: 1000 Sec 内存限制: 64 MB 提交: 338 解决: 49 题目描述 赛马是一古老的游戏,早在公元前四世纪的中国,处在诸侯割据的状态,历史上称为&qu ...
- Java对象与对象引用变量的理解
Java对象及对象引用 首先定义一个简单的类: class User{ int userId; String userName; } 我们在创建对象时,通常会写: User user = new Us ...
- RDA GUI
创建服务:“sysapp_table.h” / "dvb_guiobj_table.h" / "atv_guiobj_table.h" “服务”与“回调” ...