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$ 个小写字母组成的字符串,两个字符串如果前者的最后一个字母与后者的首字母相 ...
随机推荐
- 常用的HBase命令
进入HBase shell:hbase shell 退出HBase shell:exit 查看HBase中所有的表:list 查看某个表中的记录总数:count 'table name' 查看某个表中 ...
- Day9 - K - Yue Fei's Battle HDU - 5136
Yue Fei is one of the most famous military general in Chinese history.He led Southern Song army in t ...
- 「NOIP2011」Mayan游戏
传送门 Luogu 解题思路 爆搜,并考虑几个剪枝. 不交换颜色相同的方块(有争议,但是可以过联赛数据 \(Q \omega Q\)) 左边为空才往左换 右边不为空才往右换 因为对于两个相邻方块,右边 ...
- linux下FTP的工具和使用以及rpmReadSignature failed错误
安装rpm文件时提示rpmReadSignature failed 错误 2011-09-23 11:04 现象: [root@localhost share]# rpm -ivh syslog- ...
- NO24 第三关--企业面试题
[考试目的] 1.学生课后复习及预习情况. 2.未来实际工作中做人做事能力. 3.沟通及口头表达能力. [口头表达技能考试题] 1.描述linux的开机到登陆界面的启动过程(记时2分钟) *****L ...
- uni-app小程序组建
(1)新建组建:编辑器右击 新建组建 (2)传值 <template> <view class="myRankingList"> <block v-f ...
- SpringCloud实战——(1)创建SpringCloud项目
首先创建一个SpirngCloud工程,并添加公用依赖. <?xml version="1.0" encoding="UTF-8"?> <pr ...
- 前端轻量级、简单、易用的富文本编辑器 wangEditor 的基本用法
1.富文本编辑器市面上有很多,但是综合考虑之后wangEditor是最易用的框架,推荐使用 首先进入官网 http://www.wangeditor.com 基本是2中方式引入: 使用CDN://un ...
- other#nginx配置
#user nobody; worker_processes ; #error_log logs/error.log; #error_log logs/error.log notice; #error ...
- 从零开始学C++(1 变量和基本类型)
接下来的几篇文章介绍C++的基础知识点. C++是一种静态数据类型语言,它的类型检查发生在编译时.因此,编译器必须知道程序中每一个变量对应的数据类型. 数据类型是程序的基础:它告诉我们数据的意义以及我 ...