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 题意:中文题诶- 思路:枚举+贪心 一开始写的行和列同时 ...
随机推荐
- HUD:3746-Cyclic Nacklace(补齐循环节)
Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Pro ...
- [Poj3133]Manhattan Wiring (插头DP)
Description 题目大意:给你个N x M(1≤N, M≤9)的矩阵,0表示空地,1表示墙壁,2和3表示两对关键点.现在要求在两对关键点之间建立两条路径,其中两条路径不可相交或者自交(就是重复 ...
- Java多线程-yield(),sleep()以及wait()的区别
从操作系统的角度讲,os会维护一个ready queue(就绪的线程队列).并且在某一时刻cpu只为ready queue中位于队列头部的线程服务.但是当前正在被服务的线程可能觉得cpu的服务质量不够 ...
- mysql 分组查询前n条数据
今天去面试,碰到一道面试题: 有一个学生成绩表,表中有 表id.学生名.学科.分数.学生id .查询每科学习最好的两名学生的信息: 建表sql: CREATE TABLE `stuscore` ( ` ...
- python-生成器迭代器及递归调用
生成器是一个可迭代的对象,它的执行会记住上一次返回时在函数体中的位置.对生成器第二次(或第 n 次)调用跳转至该函数上次执行位置继续往下执行,而上次调用的所有局部变量都保持不变. 生成器的特点:1.生 ...
- 与Python的初次见面
一.Python的介绍 python的创始人为吉多.范罗苏姆.1989年的圣诞期间,吉多.范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. 二.Python是 ...
- jupyter-notebook快捷键的使用
jupyter-notebook快捷键的使用 工具有个键盘图标可以看所有快捷键 Esc + F 在代码中查找.替换 Esc + O 在cell和输出结果间切换. Shift + J 或 Shift + ...
- MySql数据库 - 1.安装
下载: 官网:www.mysql.com 打开官网之后依次点击:DOWNLOADS - Windows - MySql Installer MySql Installer 包含的功能,使用C#连接数据 ...
- 监视网络接口TCP状态信息数据有多种工具或命令。下面举例一些:
nstat命令 nstat kernel ======= ss -s == netstat -i netstat -s ip -s link sar -n DEV 1
- Chrome DevTools & performance optimization
Chrome DevTools & performance ptimization https://www.bing.com https://developers.google.com/web ...