【CF706C】Hard problem
Description
Vasiliy is fond of solving different tasks. Today he found one he wasn't able to solve himself, so he asks you to help.
Vasiliy is given n strings consisting of lowercase English letters. He wants them to be sorted in lexicographical order (as in the dictionary), but he is not allowed to swap any of them. The
only operation he is allowed to do is to reverse any of them (first character becomes last, second becomes one before last and so on).
To reverse the i-th string Vasiliy has to spent ci units of energy. He is interested
in the minimum amount of energy he has to spent in order to have strings sorted in lexicographical order.
String A is lexicographically smaller than string B if it is shorter than B (|A| < |B|)
and is its prefix, or if none of them is a prefix of the other and at the first position where they differ character in A is smaller than the character in B.
For the purpose of this problem, two equal strings nearby do not break the condition of sequence being sorted lexicographically.
Input
The first line of the input contains a single integer n (2 ≤ n ≤ 100 000) — the number of strings.
The second line contains n integers ci (0 ≤ ci ≤ 109),
the i-th of them is equal to the amount of energy Vasiliy has to spent in order to reverse the i-th string.
Then follow n lines, each containing a string consisting of lowercase English letters. The total length of these strings doesn't exceed100 000.
Output
If it is impossible to reverse some of the strings such that they will be located in lexicographical order, print - 1. Otherwise, print the minimum total amount of energy Vasiliy has to spent.
Sample Input
2
1 2
ba
ac
1
3
1 3 1
aa
ba
ac
1
2
5 5
bbb
aaa
-1
2
3 3
aaa
aa
-1
Hint
In the second sample one has to reverse string 2 or string 3. To amount of energy required to reverse the string 3 is
smaller.
In the third sample, both strings do not change after reverse and they go in the wrong order, so the answer is - 1.
In the fourth sample, both strings consists of characters 'a' only, but in the sorted order string "aa" should go before string "aaa", thus
the answer is - 1.
【题解】
意思是说每个字符串只能倒转或不倒转。(逆序)
然后要求n个字符串最后为有序的。s[i]>=s[i-1];
每个字符串倒转都需要耗费能量c[i]
设f[i][0]表示第i个字符串不倒转所需要的最少能量。
f[i][1]表示第i个字符串倒转所需要的最少能量。
然后用一个结构体记录每一个字符不倒转的样子和倒转过后的样子。->zhengs,fans;
因为你在处理第I个,如果前i-1个不是升序的。那改变第i个毫无意义。
所以如果f[i][0]==-1或f[i][1] ==-1,则表示这个状态不符合升序的要求。不能由这个状态推出下一个状态。
然后每次把存zhengs和fans的a[]数组的a[i].zhengs->a[i-1].fans,a[i-1].zhengs(对应f[i-1][1],f[i-1][0])进行比较如果满足a[i].zhengs>=后面两个字符则f[i][0]=min(f[i-1][0],f[i-1][1]);前提是f[i-1][0]或f[i-1][1]这个状态存在!
然后a[i].fans->a[i-1].fans,a[i-1].zhengs(对应f[i-1][1],f[i-1][0]).如果满足a[i].fans>=后面两个字符串。则f[i][1]和f[i-1][0]或f[i-1][1]取较小值。同样要求这个状态要存在才行!
【代码】
#include <cstdio>
#include <cstring>
#include <string>
#include <iostream> using namespace std; struct ss
{
string zhengs, fans;//用来存正的序列和反向之后的序列。
}; ss a[100001];
__int64 f[100001][2], c[100001];
int n; int main()
{
//freopen("F:\\rush.txt", "r", stdin);
//freopen("F:\\rush_out.txt", "w", stdout);
scanf("%d", &n);
for (int i = 1; i <= n; i++)//读入对某一个操作需要的能量值。
scanf("%I64d", &c[i]);
char s[100001];
for (int i = 1; i <= n; i++)//读入n个字符串
{
scanf("%s", s);//用scanf读会快一点。
a[i].zhengs = string(s);
a[i].fans = string(s);
int begin = 0, end = a[i].zhengs.size() - 1;
while (begin <= end)//进行反转的过程
{
a[i].fans[begin] = a[i].zhengs[end];
a[i].fans[end] = a[i].zhengs[begin];
begin++; end--;
}
}
memset(f, 255, sizeof(f));//一开始所有的状态都无法到达。
f[1][0] = 0; f[1][1] = c[1];//初值表示前1个字符串,不反转则消耗为0,翻转则消耗为c[1]
for (int i = 2; i <= n; i++)
{
if (a[i].zhengs >= a[i - 1].zhengs && f[i - 1][0] != -1)//根据上面题解的规则写转移就可以了。
f[i][0] = f[i - 1][0];
if (a[i].zhengs >= a[i - 1].fans && f[i - 1][1] != -1)
if (f[i][0] == -1 || f[i][0] > f[i - 1][1])
f[i][0] = f[i - 1][1]; //要注意。一定要f[i-1][x]这个状态存在才能转移。
if (a[i].fans >= a[i - 1].zhengs && f[i - 1][0] != -1)
f[i][1] = f[i - 1][0] + c[i];
if (a[i].fans >= a[i - 1].fans && f[i - 1][1] != -1)
if (f[i][1] == -1 || f[i][1] > f[i - 1][1] + c[i])
f[i][1] = f[i - 1][1] + c[i];
}
__int64 ans = f[n][1];//最后取f[n][1],f[n][0]中的较小值
if (f[n][0] < f[n][1])
ans = f[n][0];//如果较小值是-1,就取f[n][1],f[n][0]中的较大值。
if (ans == -1) //如果最小值是-1,则取最大值。这样就能指向答案了(当然,如果还是-1则答案是-1)
{
__int64 temp = f[n][1];
if (f[n][0] > f[n][1])
temp = f[n][0];
ans = temp;
}
printf("%I64d", ans);
return 0;
}
【CF706C】Hard problem的更多相关文章
- 【Luogu4137】Rmq Problem/mex (莫队)
[Luogu4137]Rmq Problem/mex (莫队) 题面 洛谷 题解 裸的莫队 暴力跳\(ans\)就能\(AC\) 考虑复杂度有保证的做法 每次计算的时候把数字按照大小也分块 每次就枚举 ...
- 【BZOJ2302】[HAOI2011]Problem C(动态规划)
[BZOJ2302][HAOI2011]Problem C(动态规划) 题面 BZOJ 洛谷 题解 首先如果\(m=0\)即没有特殊限制的话,那么就和这道题目基本上是一样的. 然而这题也有属于这题的性 ...
- 【BZOJ4999】This Problem Is Too Simple!(线段树)
[BZOJ4999]This Problem Is Too Simple!(线段树) 题面 BZOJ 题解 对于每个值,维护一棵线段树就好啦 动态开点,否则空间开不下 剩下的就是很简单的问题啦 当然了 ...
- 【BZOJ2298】[HAOI2011]problem a DP
[BZOJ2298][HAOI2011]problem a Description 一次考试共有n个人参加,第i个人说:“有ai个人分数比我高,bi个人分数比我低.”问最少有几个人没有说真话(可能有相 ...
- 【bzoj3339】Rmq Problem
[bzoj3339]Rmq Problem Description Input Output Sample Input 7 50 2 1 0 1 3 21 32 31 43 62 7 Sample ...
- 【BZOJ4999】This Problem Is Too Simple! 离线+树状数组+LCA
[BZOJ4999]This Problem Is Too Simple! Description 给您一颗树,每个节点有个初始值. 现在支持以下两种操作: 1. C i x(0<=x<2 ...
- 【计算几何】FZU Problem 2270 Two Triangles
http://acm.fzu.edu.cn/problem.php?pid=2270 [题意] 给定6到10个点,从中选出6个不同的点组成两个三角形,使其中一个三角形可以通过另一个三角形平移和旋转得到 ...
- 【HDU】6242-Geometry Problem
今天忽然心血来潮打开牛客网尝试了一下一站到底 前四道题都是不到二十分钟切完,然后第五道来了道计算几何 我也不会啊,于是就觉得大力随机也许可行 然鹅被精度卡到崩溃 后来我才知道 保证有解,是保证你的精度 ...
- 【搜索】Partition problem
题目链接:传送门 题面: [题意] 给定2×n个人的相互竞争值,请把他们分到两个队伍里,如果是队友,那么竞争值为0,否则就为v[i][j]. [题解] 爆搜,C(28,14)*28,其实可以稍加优化, ...
随机推荐
- 【习题 7-8 UVA-12107】Digit Puzzle
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 迭代加深搜索. 枚举最大层数.(也即改变的数字个数 然后枚举第一个改哪个数字,第二个改哪个数字.. 一定要注意字典序问题. 每次优先 ...
- 业余学习react 学习记录
http://www.ruanyifeng.com/blog/2015/03/react (阮一峰 react 学习) 1.搭建环境:npm 使用 React npm install -g cnpm ...
- Java经典23种设计模式之行为型模式(二)
本文接着介绍行为型模式里的解释器模式.迭代器模式.中介者模式. 一.解释器模式Interpret 给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示来解释语言的中的句子. 1 ...
- C#程序集引入无效的解决方法
在项目类库中已经引用了相关了类库,生成解决方案也没问题,但是到了后置代码,通过using引用其他类库的时候,再生成解决方案或者生成单个类库,就会报“未能找到类型或命名空间“xxx"(是否缺少 ...
- 3. Vue-router 路由
路由是根据不同的url地址展现不同的内容或页面. 前端路由就是把不同路由对应不同的内容或页面的任务交给前端来做(在单页面应用,大部分页面结构不变,只改变部分内容的使用),之前是通过服务器根据url的不 ...
- scrapy--介绍
Scrapy一个开源和协作的框架,其最初是为了页面抓取所设计的,使用它可以以快速.简单.可扩展的方式从网站中提取所需的数据.但目前Scrapy的用途十分广泛,可用于如数据挖掘.监测和自动化测试等领 ...
- poj 1191 棋盘切割 (压缩dp+记忆化搜索)
一,题意: 中文题 二.分析: 主要利用压缩dp与记忆化搜索思想 三,代码: #include <iostream> #include <stdio.h> #include & ...
- SSMM框架
IDEA搭建SSMM框架(详细过程) 相关环境 Intellij IDEA Ultimate Tomcat JDK MySql 5.6(win32/win64) Maven (可使用Intellij ...
- java——数组
数组是多个同样数据类型数组组合,当中数据类型是不论什么数据类型. 数组变量是引用类型变量,数组能够作为对象,数组中的每个元素相当于对象的成员变量,所以数组元素能够默认初始化.(博客java--变量分类 ...
- hcharts实现堆叠柱形图
<!DOCTYPE > <html> <head> <meta charset="utf-8"><link rel=" ...