Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏
1 second
256 megabytes
standard input
standard output
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.
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.
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.
- 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
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个字符串,要求通过逆置字符串的操作是所有字符串呈字典序,每次逆置消耗费用a[i],问最小花费
每个字符串只有两个操作,变或不变,开2维数组记录花费即可;
- #include <cstdio>
- #include <algorithm>
- #include <cstring>
- #include <cmath>
- #include <string>
- #include <iostream>
- using namespace std;
- #define inf 0x3f3f3f3f
- long long dp[100005][2];
- int a[100005];
- string s[100005];
- int n;
- int main()
- {
- while(~scanf("%d",&n))
- {
- for(int i=1;i<=n;i++)
- scanf("%d",&a[i]);
- for(int i=1;i<=n;i++)
- cin>>s[i];
- memset(dp,4557430888798830399,sizeof(dp));
- dp[0][0]=dp[0][1]=0;
- for(int i=1;i<=n;i++)
- {
- if(s[i]>=s[i-1])
- dp[i][0]=min(dp[i][0],dp[i-1][0]);
- reverse(s[i-1].begin(),s[i-1].end());
- if(s[i]>=s[i-1])
- dp[i][0]=min(dp[i][0],dp[i-1][1]);
- reverse(s[i-1].begin(),s[i-1].end());
- reverse(s[i].begin(),s[i].end());
- if(s[i]>=s[i-1])
- dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]);
- reverse(s[i-1].begin(),s[i-1].end());
- if(s[i]>=s[i-1])
- dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);
- reverse(s[i-1].begin(),s[i-1].end());
- reverse(s[i].begin(),s[i].end());
- }
- if(dp[n][0]==4557430888798830399&&dp[n][1]==4557430888798830399)
- printf("-1\n");
- else
- printf("%lld\n",min(dp[n][0],dp[n][1]));
- }
- return 0;
- }
Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏的更多相关文章
- Labeling Balls 分类: POJ 2015-07-28 19:47 10人阅读 评论(0) 收藏
Labeling Balls Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11893 Accepted: 3408 Descr ...
- POJ3320 Jessica's Reading Problem 2017-05-25 19:55 38人阅读 评论(0) 收藏
Jessica's Reading Problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12346 Accep ...
- HDU6029 Happy Necklace 2017-05-07 19:11 45人阅读 评论(0) 收藏
Happy Necklace Time Limit: ...
- hdu 1053 (huffman coding, greedy algorithm, std::partition, std::priority_queue ) 分类: hdoj 2015-06-18 19:11 22人阅读 评论(0) 收藏
huffman coding, greedy algorithm. std::priority_queue, std::partition, when i use the three commente ...
- hdu 1036 (I/O routines, fgets, sscanf, %02d, rounding, atoi, strtol) 分类: hdoj 2015-06-16 19:37 32人阅读 评论(0) 收藏
thanks to http://stackoverflow.com/questions/2144459/using-scanf-to-accept-user-input and http://sta ...
- Power Strings 分类: POJ 串 2015-07-31 19:05 8人阅读 评论(0) 收藏
Time Limit:3000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status Practice POJ ...
- 多校赛3- Painter 分类: 比赛 2015-07-29 19:58 3人阅读 评论(0) 收藏
D - Painter Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit Status P ...
- 滑雪 分类: POJ 2015-07-23 19:48 9人阅读 评论(0) 收藏
滑雪 Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 83276 Accepted: 31159 Description Mich ...
- Let the Balloon Rise 分类: HDU 2015-06-19 19:11 7人阅读 评论(0) 收藏
Let the Balloon Rise Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Oth ...
随机推荐
- HTML实现文件拖动上传
在大型企业的开发过程中,很多比较有趣而实际的功能往往都是让大家望而却步,我给大家带来一个百度云盘和360云盘的HTML5多文件拖动上传技术: 1:记得导入:common-fileupload.jar包 ...
- js判断第二个日期比第一个日期大
如何用脚本判断用户输入的的字符串是下面的时间格式2004-11-21 必须要保证用户的输入是此格式,并且是时间,比如说月份不大于12等等,另外我需要用户输入两个,并且后一个要比前一个晚,只允许用JAV ...
- spring boot 配置 freemarker
1.springboot 中自带的页面渲染工具为thymeleaf 还有freemarker 这两种模板引擎 简单比较下两者不同, 1.1freemaker 优点 freemarker 不足:thym ...
- maven package install deploy
1.maven package:打包到本项目,一般是在项目target目录下. 如果a项目依赖于b项目,打包b项目时,只会打包到b项目下target下,编译a项目时就会报错,因为找不到所依赖的b项目, ...
- chrome input去除黄色背景色
input:-webkit-autofill { -webkit-box-shadow: 0 0 0px 1000px white inset; border: 1px solid #CCC!impo ...
- luoguP1080 国王游戏 (贪心+高精度)
题目链接:https://www.luogu.org/problemnew/show/P1080 参考:https://www.luogu.org/problemnew/solution/P1080 ...
- Python locals() 函数
Python locals() 函数 Python 内置函数 描述 locals() 函数会以字典类型返回当前位置的全部局部变量. 对于函数, 方法, lambda 函式, 类, 以及实现了 __c ...
- Codeforces Round #533 (Div. 2)
C: 题意: 有n个整数ai,数列a有两个神奇的性质.1.所有的整数都在[l,r]范围内.2.这n个数的和能被3整除.现在给出l和r,和个数n,问你有多少种方法构造出数列a,方案数mod1e9+7. ...
- 转)MySQL日期与时间函数
-- MySQL日期时间处理函数 -- 当前日期:2017-05-12(突然发现今天512,是不是会拉防空警报) SELECT NOW() FROM DUAL;-- 当前日期时间:2017-05-12 ...
- Linux网络编程---htons函数的使用
htons是将整型变量从主机字节顺序转变成网络字节顺序, 就是整数在地址空间存储方式变为高位字节存放在内存的低地址处. htonl就是把本机字节顺序转化为网络字节顺序所谓网络字节顺序(大尾顺序)就是指 ...