Codeforces 706 C. Hard problem (dp)
题目链接:http://codeforces.com/problemset/problem/706/C
给你n个字符串,可以反转任意一个字符串,反转每个字符串都有其对应的花费ci。
经过操作后是否能满足字符串str[i]>=str[i-1],能就输出最小花费,不能输出-1。
dp[i][0] 表示不反转i的最小花费(str[i] >= str[i - 1] || str[i] >= reverse(str[i - 1]))
dp[i][1] 则表示反转i的最小花费...
初始dp[1][0] = 0, dp[1][1] = c[1]
要是dp[i][0/1]等于-1 就不能转移了
代码写的有点糟糕,还是太渣...
//#pragma comment(linker, "/STACK:102400000, 102400000")
#include <algorithm>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>
#include <string>
#include <vector>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int N = 1e5 + ;
string str[N];
LL num[N], inf = 1e15;
LL dp[N][]; int main()
{
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i = ; i <= n; ++i)
cin >> num[i];
for(int i = ; i <= n; ++i)
cin >> str[i];
int ok = ;
memset(dp, -, sizeof(dp));
dp[][] = , dp[][] = num[];
for(int i = ; i <= n; ++i) {
string str1 = str[i - ]; //未反转
reverse(str[i - ].begin(), str[i - ].end());
string str2 = str[i]; //未反转
reverse(str[i].begin(), str[i].end());
if(dp[i - ][] == - && dp[i - ][] == -) {
ok = -; //不行了
break;
}
if(dp[i - ][] != -) {
if(str2 >= str1) {
dp[i][] = dp[i - ][];
}
if(str[i] >= str1) {
dp[i][] = dp[i - ][] + num[i];
}
}
if(dp[i - ][] != -) {
if(str2 >= str[i - ]) {
dp[i][] = min(dp[i - ][], dp[i][] == - ? inf : dp[i][]);
}
if(str[i] >= str[i - ]) {
dp[i][] = min(dp[i - ][] + num[i], dp[i][] == - ? inf : dp[i][]);
}
}
str[i] = str2; //赋值未反转
}
if(ok == -) {
cout << - << endl;
}
else if(dp[n][] != - && dp[n][] != -) {
cout << min(dp[n][], dp[n][]) << endl;
}
else if(dp[n][] != -) {
cout << dp[n][] << endl;
}
else if(dp[n][] != -) {
cout << dp[n][] << endl;
}
else {
cout << - << endl;
}
return ;
}
Codeforces 706 C. Hard problem (dp)的更多相关文章
- codeforces 706C C. Hard problem(dp)
题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...
- codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)
题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...
- codeforces 721C (拓排 + DP)
题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...
- Codeforces 543D. Road Improvement (树dp + 乘法逆元)
题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...
- Codeforces 467C. George and Job (dp)
题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...
- CodeForces 163A Substring and Subsequence dp
A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...
- Codeforces 834D The Bakery【dp+线段树维护+lazy】
D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces#1154F. Shovels Shop (dp)
题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...
- Educational Codeforces Round 63-D(基础DP)
题目链接:https://codeforces.com/contest/1155/problem/D 题意:给定n个数,可以选择一段连续子段将其乘x,也可以不操作,求最大连续子段和. 思路:比赛时觉得 ...
随机推荐
- 51nod1244 莫比乌斯函数之和
推公式.f[n]=1-∑f[n/i](i=2...n).然后递归+记忆化搜索.yyl说这叫杜教筛?时间复杂度貌似是O(n 2/3)的? #include<cstdio> #include& ...
- QCon 2015 阅读笔记 - 移动开发最佳实践
所有ppt下载地址:http://pan.baidu.com/s/1mg9o4TM 下面是移动开发实践部分的阅读笔记. 移动开发网络性能优化实践 - 陈浩然 (携程) 携程是非常标准的移动App架构, ...
- 原型设计工具Axure 建模工具powerdesigner
Licensee:IloveyouAxure Key:UChpuxwbDW6eAIaAf9UujEFSBwN3vpEz9snHvlCQVJGQy4p7WrCyKLLvV5QLvqva 注册用户名: ...
- 【英语】Bingo口语笔记(69) - 脏话的表达
- 备忘录 - numpy基本方法总结
一.数组方法 创建数组:arange()创建一维数组:array()创建一维或多维数组,其参数是类似于数组的对象,如列表等 反过来转换则可以使用numpy.ndarray.tolist()函数,如a. ...
- mysql 查看所有存储过程
转载地址:http://zhuixue.iteye.com/blog/375353 查询数据库中的存储过程 方法一: select `name` from mysql.proc where db = ...
- Oracle buffer cache与相关的latch等待事件
buffer cache与相关的latch等待事件 1.buffer cache 2.latch:cache buffers lru chain 3.latch:cache buffers chain ...
- Error accessing PRODUCT_USER_PROFILE
1.问题现象再现1)创建用户secSQL> create user sec identified by sec; User created. 2)授权SQL> grant connect, ...
- dword word byte 相互转换 .xml
pre{ line-height:1; color:#800080; background-color:#d2c39b; font-size:16px;}.sysFunc{color:#627cf6; ...
- eclipse 恢复被删除的文件
即使你在项目中删除了某一文件,该文件的相关信息仍会保存在本地历史记录中.这就使得你可以恢复那些在项目或文件夹中已被删除的文件.如果恢复某一被删除的文件,则首先在Navigator视图中选择该文件先前所 ...