CodeForces - 706C Hard problem(dp+字符串)
题意:有n个字符串,只能将其逆转,不能交换位置,且已知逆转某字符串需要消耗的能量,问将这n个字符串按字典序从小到大排序所需消耗的最少能量。
分析:每个字符串要么逆转,要么不逆转,相邻两个字符串进行比较,从而可得4个状态转移方程。
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 100000 + 10;
const int MAXT = 10000 + 10;
using namespace std;
LL c[MAXN];
string s[MAXN][2];
LL dp[MAXN][2];
int main(){
int n;
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%I64d", &c[i]);
}
string tmp;
for(int i = 0; i < n; ++i){
cin >> tmp;
s[i][0] = tmp;
reverse(tmp.begin(), tmp.end());
s[i][1] = tmp;
}
memset(dp, LL_INF, sizeof dp);
dp[0][0] = 0, dp[0][1] = c[0];
bool ok = true;
for(int i = 1; i < n; ++i){
if(s[i][0] >= s[i - 1][0]){
dp[i][0] = min(dp[i][0], dp[i - 1][0]);
}
if(s[i][1] >= s[i - 1][0]){
dp[i][1] = min(dp[i][1], dp[i - 1][0] + c[i]);
}
if(s[i][0] >= s[i - 1][1]){
dp[i][0] = min(dp[i][0], dp[i - 1][1]);
}
if(s[i][1] >= s[i - 1][1]){
dp[i][1] = min(dp[i][1], dp[i - 1][1] + c[i]);
}
if(dp[i][0] == LL_INF && dp[i][1] == LL_INF){
ok = false;
break;
}
}
if(ok){
printf("%I64d\n", min(dp[n - 1][0], dp[n - 1][1]));
}
else{
printf("-1\n");
}
return 0;
}
CodeForces - 706C Hard problem(dp+字符串)的更多相关文章
- Codeforces 706C - Hard problem - [DP]
题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- CodeForces 706C Hard problem (水DP)
题意:对于给定的n个字符串,可以花费a[i] 将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少. 析:很明显的水DP,如果不是水DP,我也不会做.... 这个就要二维,d[2][max ...
- 【动态规划】Codeforces 706C Hard problem
题目链接: http://codeforces.com/contest/706/problem/C 题目大意: n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci( ...
- Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏
C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...
- CodeForces 706C Hard problem
简单$dp$. $dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用. $dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前 ...
- Codeforces 706 C. Hard problem (dp)
题目链接:http://codeforces.com/problemset/problem/706/C 给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci. 经过操作后是否能满 ...
- CodeForces 176B Word Cut dp
Word Cut 题目连接: http://codeforces.com/problemset/problem/176/C Description Let's consider one interes ...
- Codeforces 191A - Dynasty Puzzles - [DP]
题目链接:https://codeforces.com/problemset/problem/191/A 题意: 给出 $n$ 个小写字母组成的字符串,两个字符串如果前者的最后一个字母与后者的首字母相 ...
随机推荐
- OpenCV数字识别
输入命令: conda install opencv 返回信息:
- SQL 笔记1,left join,group by,having
表:XS,XK,CJ left join 表1 on 表1.字段=表2.字段 group by 分组条件 order by 排序条件 asc正序(小到大),desc倒序 having 跟条件类似whe ...
- [Codeforces #615 div3]1294E Obtain a Permutation
Before the Beginniing 本文为 Clouder 原创文章,原文链接为Click,转载时请将本段放在文章开头显眼处.如进行了二次创作,请明确标明. 由本人转载于博客园. 题意分析 C ...
- Masonry与UITableView+FDTemplateLayoutCell搭配使用
打个小广告:本人开发了一个宠物相关的App,欢迎大家下载体验~ 下载二维码: 进入正文: 之前发过一篇博客,也是对这两个的练习使用,但是之后遇到些问题,所以删除重写了.抱歉 Masonry是一款轻量级 ...
- Spring JMSTemplate 与 JMS 原生API比较
博客分类: JMS Spring 2.x JMSUtil与Spring JmsTemplate的对比 Author:信仰 Date:2012-4-20 未完待续,截止日期2012-4-20 从以下 ...
- 攻防世界web新手区(3)
xff_referer:http://111.198.29.45:43071 打开网址,显示出这个页面: X-Forwarded-For:简称XFF头,它代表客户端,也就是HTTP的请求端真实的IP, ...
- ubuntu 环境下pycharm的 安装与激活教程
1. 基本安装: 1.1 打开Ubuntu的应用市场,并在搜索栏搜索pycharm,结果如下图所示 1.2 选择pro版本进行安装,结果如下图所示: 1.3打开安装后的pycharm,如果出现下图所示 ...
- 序列化sys随笔补充
sys是在和python解释器做交互 sys.path----模块查找的顺序 sys.path.append() sys.path.insert()sys.argv---只能在终端执行 sys.arg ...
- GNS3 模拟icmp记录路由
路由配置: icmp记录路由抓取出接口的IP地址,最多可以抓取9个.ip协议头中的options为40个字节 R1 : conf t int f0/0 no shutdown ip add 192.1 ...
- 怎样管理Exchange Server 2013资源邮箱
1. exchange资源邮箱介绍 这次将介绍Exchange Server 2013的资源邮箱相关内容. Exchange Server 2013的资源邮箱包含两类,其一为“会议室邮箱”,另一类是“ ...