CodeForces 706C Hard problem (水DP)
题意:对于给定的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)的更多相关文章
- CodeForces - 706C Hard problem(dp+字符串)
题意:有n个字符串,只能将其逆转,不能交换位置,且已知逆转某字符串需要消耗的能量,问将这n个字符串按字典序从小到大排序所需消耗的最少能量. 分析:每个字符串要么逆转,要么不逆转,相邻两个字符串进行比较 ...
- Codeforces 706C - Hard problem - [DP]
题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...
- 【动态规划】Codeforces 706C Hard problem
题目链接: http://codeforces.com/contest/706/problem/C 题目大意: n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci( ...
- 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 ...
- CodeForces 706C Hard problem
简单$dp$. $dp[i][0]$:第$i$个串放置完毕,并且第$i$个串不反转,前$i$个串字典序呈非递减的状态下的最小费用. $dp[i][1]$:第$i$个串放置完毕,并且第$i$个串反转,前 ...
- Codeforces 1096D Easy Problem 【DP】
<题目链接> 题目大意: 给你一个字符串,每个字符有权值,问现在删除字符串中的字符使其中没有"hard"的最小代价是多少. 解题分析: 用DP来求解: 转 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- codeforces 711C Coloring Trees(DP)
题目链接:http://codeforces.com/problemset/problem/711/C O(n^4)的复杂度,以为会超时的 思路:dp[i][j][k]表示第i棵数用颜色k涂完后bea ...
随机推荐
- bzoj4025
首先我们要知道,怎么去维护一个是否是二分图 二分图的充要条件:点数>=2且无奇环 重点就是不存在奇环,怎么做呢 考虑随便维护一个图的生成树,不难发现,如果一条边加入后,形成奇环的话就不是二分图 ...
- UVa 10883 (组合数 对数) Supermean
在纸上演算一下就能看出答案是:sum{ C(n-1, i) * a[i] / 2^(n-1) | 0 ≤ i ≤ n-1 } 组合数可以通过递推计算:C(n, k) = C(n, k-1) * (n- ...
- Asp.Net微信登录-手机网站APP应用
要求:公众号必须先认证,认证费用¥300/年,比较黑 一.微信登录核心代码 //核心代码,没判断异常 1.登录页面 protected void Page_Load(object sender, Ev ...
- codevs 1088 神经网络
bfs.语文题. #include<iostream> #include<cstdio> #include<cstring> #include<algorit ...
- RPi 2B Raspbian SD卡内部架构
/***************************************************************************** * RPi 2B Raspbian SD卡 ...
- 【jQuery】鼠标接触按钮后改变图片
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- 【转】iOS开发UITableViewCell的选中时的颜色设置
原文网址:http://mobile.51cto.com/hot-404900.htm 1.系统默认的颜色设置 //无色 cell.selectionStyle = UITableViewCellSe ...
- Nmap / NetCat(nc) / 网络安全工具
nmap - 网络探测工具和安全/端口扫描器 nmap [ <扫描类型> ...] [ <选项> ] { <扫描目标说明> } 描述 Nmap ("Net ...
- Filezilla中文字符文件看不到或显示乱码的解决办法
Filezilla确实是跨平台的好软件,可之前我就在ubuntu下郁闷为什么看坛子FTP里竟然是空的.最近换MAC版的FZ结果还是这样就奇怪了. 后来想Filezilla应该是支持字符集转换的,所以在 ...
- XShell 屏幕锁定的恢复方法(Ctrl+Q)
操作XShell过程中很多时间大家会习惯性的按Ctrl+S进行保存. Ctrl+S在XShell的作用是屏幕锁定,很多朋友会无法操作,会直接把窗口关闭. 解决方法: 快捷键 Ctrl+Q 即能完成解锁 ...