题意:对于给定的n个字符串,可以花费a[i]  将其倒序,问是否可以将其排成从大到小的字典序,且花费最小是多少。

析:很明显的水DP,如果不是水DP,我也不会做。。。。

这个就要二维,d[2][maxn],d[0][i]表示第 i 个不反转是最小花费,d[1][i]表示第 i 个反转最小花费,那么剩下的就很简单了么,

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <stack>
using namespace std; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f;
const LL LNF = 100000000000000000;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 1e5 + 5;
const int mod = 1e9 + 7;
const char *mark = "+-*";
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, 1, 0, -1};
int n, m;
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
inline LL Max(LL a, LL b){ return a < b ? b : a; }
inline LL Min(LL a, LL b){ return a > b ? b : a; }
int a[maxn];
vector<string> v1;
vector<string> v2;
LL d[2][maxn]; int main(){
while(scanf("%d", &n) == 1){
for(int i = 0; i < n; ++i) scanf("%d", &a[i]);
string s;
v1.clear(); v2.clear();
for(int i = 0; i < n; ++i){
cin >> s;
v1.push_back(s);
reverse(s.begin(), s.end());
v2.push_back(s);
}
fill(d[0], d[0]+n, LNF);
fill(d[1], d[1]+n, LNF);
d[0][0] = 0, d[1][0] = a[0];
for(int i = 1; i < n; ++i){
if(v1[i-1] <= v1[i]) d[0][i] = Min(d[0][i], d[0][i-1]);
if(v1[i-1] <= v2[i]) d[1][i] = Min(d[1][i], d[0][i-1]+a[i]);
if(v2[i-1] <= v1[i]) d[0][i] = Min(d[0][i], d[1][i-1]);
if(v2[i-1] <= v2[i]) d[1][i] = Min(d[1][i], d[1][i-1]+a[i]);
if(d[1][i] == LNF && d[0][i] == LNF) break;
}
LL ans = Min(d[0][n-1], d[1][n-1]);
printf("%I64d\n", ans == LNF ? -1 : ans);
}
return 0;
}

CodeForces 706C Hard problem (水DP)的更多相关文章

  1. CodeForces - 706C Hard problem(dp+字符串)

    题意:有n个字符串,只能将其逆转,不能交换位置,且已知逆转某字符串需要消耗的能量,问将这n个字符串按字典序从小到大排序所需消耗的最少能量. 分析:每个字符串要么逆转,要么不逆转,相邻两个字符串进行比较 ...

  2. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  3. 【动态规划】Codeforces 706C Hard problem

    题目链接: http://codeforces.com/contest/706/problem/C 题目大意: n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci( ...

  4. Codeforces 706C Hard problem 2016-09-28 19:47 90人阅读 评论(0) 收藏

    C. Hard problem time limit per test 1 second memory limit per test 256 megabytes input standard inpu ...

  5. CodeForces 706C Hard problem

    简单$dp$. $dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用. $dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前 ...

  6. Codeforces 1096D Easy Problem 【DP】

    <题目链接> 题目大意: 给你一个字符串,每个字符有权值,问现在删除字符串中的字符使其中没有"hard"的最小代价是多少. 解题分析: 用DP来求解:        转 ...

  7. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

  8. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

  9. codeforces 711C Coloring Trees(DP)

    题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...

随机推荐

  1. LinuxShell算术运算

    Bash shell 的算术运算有四种方式:1:使用 expr 外部程式 加法 r=`expr 4 + 5`echo $r注意! '4' '+' '5' 这三者之间要有空白r=`expr 4 * 5` ...

  2. 转:ASP.NET MVC中Unobtrusive Ajax的妙用

    Unobtrusive Javascript有三层含义:一是在HTML代码中不会随意的插入Javsscript代码,只在标签中加一些额外的属性值,然后被引用的脚本文件识别和处理:二是通过脚本文件所增加 ...

  3. Hadoop configration类分析

    configration这个类是分析hadoop源代码一个很好地入口. 先从需求说起.对于一个大型的文件系统,基于配置文件可以增强灵活性.congfigration类就是为了管理配置文件的. 配置文件 ...

  4. Tomcat 映射虚拟目录

    设置虚拟目录映射一般有两种用途: (1)把整个web应用映射到tomcat中: 如一个testapp的web应用的路径是/opt/testapp,则通过虚拟目录映射可以将其映射到tomcat(weba ...

  5. kendo grid输入框验证方法

    $("#grid").kendoGrid({ dataSource: dataSrc, //toolbar: ["save", "取消"], ...

  6. LeetCode Factorial Trailing Zeroes (阶乘后缀零)

    题意:如标题 思路:其他文章已经写过,参考其他. class Solution { public: int trailingZeroes(int n) { <? n/: n/+trailingZ ...

  7. BZOJ3540: [Usaco2014 Open]Fair Photography

    3540: [Usaco2014 Open]Fair Photography Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 72  Solved: 29 ...

  8. validator的验证

    通常喜欢这么写验证 <form method="post" data-ajax="false" action="/Shppping/PlaceO ...

  9. WdatePicker的一些用法

    在选择日期之后,再执行自己的另一个函数   onpicked: function () { LoadData(); }  <input type="text" class=& ...

  10. spring aop expression简单说明

    <aop:config> <aop:pointcut id="userDAO" expression="execution(public * cn.da ...