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,也可以不操作,求最大连续子段和. 思路:比赛时觉得 ...
随机推荐
- iOS基础CGAffineTransform的简单使用
CoreGraphics框架中的CGAffineTransform类可用于设定UIView的transform属性,控制视图的缩放.旋转和平移操作: 另称放射变换矩阵,可参照线性代数的矩阵实现方式0. ...
- 使用 github.io 免费建站
/*************************************************************************** * 使用 github.io 免费建站 * 说 ...
- mysql mac启动
设置别名 alias mysql=/usr/local/mysql/bin/mysql alias mysqladmin=/usr/local/mysql/bin/mysqladmin 修改密码 su ...
- php redis扩展
安装redis扩展,一定要弄清楚自己的php版本 echo phpinfo(); 查看php信息. 页面搜索Compiler,可以获取自己的VC版本
- Android按键事件传递流程(二)
5 应用层如何从Framework层接收按键事件 由3.2和4.5.4节可知,当InputDispatcher通过服务端管道向socket文件描述符发送消息后,epoll机制监听到了I/O事件, ...
- IconRes提供免费高质量的Material风格android官方图标库
连接地址: http://www.iconres.com/android/index.php
- requirejs之demo (转)
具体的理论就不讲了,可以参考 http://www.ruanyifeng.com/blog/2012/10/javascript_module.html http://www.ruanyifeng.c ...
- RequireJS进阶(三) 转
进阶的前面两篇讲述了r.js如何通过命令行把所有的模块压缩为一个js文件或把所有的css压缩为一个css文件.其中包括一些压缩配置参数的使用. 但以上两种方式有几个问题 1.通过命令手动配置压缩选项显 ...
- RDoc
RDoc - Ruby Documentation System home github.com/rdoc/rdoc rdoc docs.seattlerb.org/rdoc bugs github. ...
- PHP Header 缓存 --- Header 参数说明
1. Accept:告诉WEB服务器自己接受什么介质类型,*/* 表示任何类型,type/* 表示该类型下的所有子类型,type/sub-type. 2. Accept-Charset: 浏览器申 ...