Codeforces 706 C. Hard problem (dp)

时间:2021-07-08 13:21:12

题目链接: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)的更多相关文章

  1. codeforces 706C C&period; Hard problem&lpar;dp&rpar;

    题目链接: C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standar ...

  2. codeforces C&period; Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  3. codeforces 721C (拓排 &plus; DP)

    题目链接:http://codeforces.com/contest/721/problem/C 题意:从1走到n,问在时间T内最多经过多少个点,按路径顺序输出. 思路:比赛的时候只想到拓排然后就不知 ...

  4. Codeforces 543D&period; Road Improvement &lpar;树dp &plus; 乘法逆元&rpar;

    题目链接:http://codeforces.com/contest/543/problem/D 给你一棵树,初始所有的边都是坏的,要你修复若干边.指定一个root,所有的点到root最多只有一个坏边 ...

  5. Codeforces 467C&period; George and Job &lpar;dp&rpar;

    题目链接:http://codeforces.com/contest/467/problem/C 求k个不重叠长m的连续子序列的最大和. dp[i][j]表示第i个数的位置个序列的最大和. 前缀和一下 ...

  6. CodeForces 163A Substring and Subsequence dp

    A. Substring and Subsequence 题目连接: http://codeforces.com/contest/163/problem/A Description One day P ...

  7. Codeforces 834D The Bakery【dp&plus;线段树维护&plus;lazy】

    D. The Bakery time limit per test:2.5 seconds memory limit per test:256 megabytes input:standard inp ...

  8. codeforces&num;1154F&period; Shovels Shop (dp)

    题目链接: http://codeforces.com/contest/1154/problem/F 题意: 有$n$个物品,$m$条优惠 每个优惠的格式是,买$x_i$个物品,最便宜的$y_i$个物 ...

  9. Educational Codeforces Round 63-D(基础DP)

    题目链接:https://codeforces.com/contest/1155/problem/D 题意:给定n个数,可以选择一段连续子段将其乘x,也可以不操作,求最大连续子段和. 思路:比赛时觉得 ...

随机推荐

  1. web字体详解&commat;font-face

    一:字体的下载(http://www.dafont.com/new.php) 二:选择需要的字体并下载( Download ) 三:下载后并解压 四:获取@font-face所需要字体的格式.eot, ...

  2. java7新特新(一) Try-with-resources &lpar;TWR&rpar;

    Try-with-resources (TWR) 在处理IO的代码中,我们会使用大量的try...catch()...finally...语法,其中会在finally进行IO的close操作,写过py ...

  3. 09day1

    词编码 模拟 [问题描述] 一个发送机可以通过一条隧道发送一些以二进制代码组成的单词.在其尽头的接受机可以使用特殊技术恢复到最初的单词.每个单词最初都由0和1组成.所有的单词最初长度都为n(4< ...

  4. 写一个自己定义进度颜色和圆形转动的ProgressBar&lpar;具体介绍&rpar;

    先上图: 我们得自己定义ProgressBar的样式 <span style="white-space:pre"> </span><style nam ...

  5. ubuntu 系统开机执行脚本设置

    在ubuntu 系统中常常有一些操作需要开机时手动去执行,有一些固定的脚本文件可以通过改写启动项脚本让系统启动时自动执行 方法: 编辑/etc/下的rc.local脚本,把对应的需要执行的脚本写在ex ...

  6. 获取代理电脑的https证书方法

    1.打开fiddler,tools->fiddler options 勾选check for certificate revocation 2.手机打开浏览器 输入fiddler所在电脑ip及端 ...

  7. Win7 开启显示快速启动工具栏,发送到快速启动右键菜单

    开启Win7快速启动栏 许多网友一定记得在 Windows 7 之前的 Windows 系统都有个快速启动(quick launch)区域. 比如 IE 浏览器.Windows Media Playe ...

  8. Bootstrap变形记

    bootstrap审美疲劳了,想个招换换样子,THINKING... 变形 >>> 哈,不用改已有代码,添加我的Harley.js即可,有空在玩... 真实好久不玩博客园了,200字 ...

  9. 初始化列表initializer&lowbar;list

    初始化列表定义在<initializer_list>,初始化列表简化了参数数量可变的函数的编写,初始化列表的所有的元素都应该是同一种数据类型 由于定义了列表中允许的类型,所以初始化列表是安 ...

  10. hdu 1520 树形DP基础

    http://acm.hdu.edu.cn/showproblem.php?pid=1520 父节点和子节点不能同时选. http://blog.csdn.net/woshi250hua/articl ...