Codeforces 761C Dasha and Password(枚举+贪心)
题目链接 Dasha and Password
题目保证一定有解。
考虑到最多只有两行的指针需要移动,那么直接预处理出该行移动到字母数字或特殊符号的最小花费。
然后O(N^3)枚举求最小值即可。
时间复杂度O(N*M+N^3)
#include <bits/stdc++.h> using namespace std; #define REP(i,n) for(int i(0); i < (n); ++i)
#define rep(i,a,b) for(int i(a); i <= (b); ++i)
#define dec(i,a,b) for(int i(a); i >= (b); --i)
#define for_edge(i,x) for(int i = H[x]; i; i = X[i])
#define INF 1 << 30 const int N = + ;
const int M = + ;
const int Q = + ;
const int A = + ; int a[Q][Q], c[Q][], v[Q][]; char s[Q];
int n, m, ans; int main(){ scanf("%d%d", &n, &m);
rep(i, , n){
scanf("%s", s + );
rep(j, , m){
if (s[j] >= '' && s[j] <= '') a[i][j] = ;
else if (s[j] >= 'a' && s[j] <= 'z') a[i][j] = ;
else if (s[j] == '&' || s[j] == '*' || s[j] == '#') a[i][j] = ;
}
} rep(i, , n) rep(j, , ) c[i][j] = INF; rep(i, , n){
rep(j, , m){
v[i][a[i][j]] = ;
c[i][a[i][j]] = min(c[i][a[i][j]], min(abs(j - ), abs(m - j) + ));
}
} ans = INF;
rep(i, , n - ){
rep(j, i + , n - ){
rep(k, j + , n){
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
if (v[i][] && v[j][] && v[k][]) ans = min(ans, c[i][] + c[j][] + c[k][]);
}
}
} printf("%d\n", ans);
return ; }
Codeforces 761C Dasha and Password(枚举+贪心)的更多相关文章
- 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 Round #394 (Div. 2) C. Dasha and Password 暴力
C. Dasha and Password 题目连接: http://codeforces.com/contest/761/problem/C Description After overcoming ...
- D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心
D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...
- 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 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 437C The Child and Toy(贪心)
题目连接:Codeforces 437C The Child and Toy 贪心,每条绳子都是须要割断的,那就先割断最大值相应的那部分周围的绳子. #include <iostream> ...
- 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 #546 (Div. 2) D 贪心 + 思维
https://codeforces.com/contest/1136/problem/D 贪心 + 思维 题意 你面前有一个队列,加上你有n个人(n<=3e5),有m(m<=个交换法则, ...
- 51nod1625(枚举&贪心)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1625 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
随机推荐
- Redis实现之对象(三)
集合对象 集合对象的编码可以是intset或者hashtable,intset编码的集合对象使用整数集合作为底层实现,集合对象包含的所有元素都被保存在整数集合里面.举个栗子,以下代码将创建一个图1-1 ...
- NopCommerce 导航菜单HTML静态处理以提高性能
因网站要快速上线,有时候NopCommerce性能问题一直是困扰我们的最大因素,查找出来需要优化的部分代码进行修改重构是方法之一,我等非主流优化方式只为快速提高程序整体性能. 我以导航菜单为例,列出我 ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- Spring boot 整合jsp、thymeleaf、freemarker
1.创建spring boot 项目 2.pom文件配置如下: <dependencies> <dependency> <groupId>org.springfra ...
- CSU-2221 假装是区间众数(ST表模版题)
题目链接 题目 Description 给定一个非递减数列Ai,你只需要支持一个操作:求一段区间内出现最多的数字的出现次数. Input 第一行两个整数N,Q 接下来一行有N个整数,表示这个序列. 接 ...
- Linux内核使用毫秒延时函数
毫秒延时函数:mdelay() 微妙延时函数:ndelay() #ifndef mdelay #define mdelay(n) (/ (__builtin_constant_p(n) &&a ...
- PAT1038(两个运行超时 未解决
# include<iostream> # include<algorithm> using namespace std; int jishu(int a[],int N,in ...
- 【bzoj2427】[HAOI2010]软件安装 Tarjan+树形背包dp
题目描述 现在我们的手头有N个软件,对于一个软件i,它要占用Wi的磁盘空间,它的价值为Vi.我们希望从中选择一些软件安装到一台磁盘容量为M计算机上,使得这些软件的价值尽可能大(即Vi的和最大).但是现 ...
- 一个 Observation
$n$ 个小球分布在一个圆上,小球的颜色或黑或白.顺时针(或逆时针)遍历这 $n$ 个小球,记录下相邻两小球的颜色,得到 $n$ 个有序颜色对.我们有,(黑,白)和(白,黑)的数目一定相等(可能都是 ...
- BZOJ 4069 [Apio2015]巴厘岛的雕塑 ——贪心
自己首先想了一种方法$f(i)$表示前$i$个最小值为多少. 然而发现位运算并不满足局部最优性. 然后我们可以从高到低贪心的判断,使得每一组的和在一个特定的范围之内. 还要特判最后一个Subtask, ...