Codeforces Round #367 (Div. 2) Hard problem
Hard problem
题意:
有n个字符串,对第i个字符串进行反转操作代价为ci。
要使n个字符串按照字典序从小到大排列,最小的代价是多少。
题解:
反转就是reverse操作,比如说45873反转之后只能是37845,不能是别的,当时就这没有理解好,所以没继续去想,其实可以假设这样,之后来一发的,如果当时这样不就对了嘛。
一行只有反转或不反转,要求最小代价,有道01背包的意思,所以就要dp试试,dp[i]代表第i的串的最小代价,但是怎么继续推呢?,没有办法,就再多开一维,另一维只要代表2个状态就好,用0表示没有反转,1表示已经反转了,那么定义就如下了:
dp[i][0]代表前i个串有序且第i个串不反转的代价
dp[i][1]代表前i个串有序且第i个串反转的代价
初始化 dp[0][0]=0,dp[0][1] = c[0]
然后当字典序满足有小到大时进行递推
代码:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
#define cle(a,val) memset(a,(val),sizeof(a))
const ll LINF = 0x3f3f3f3f3f3f3f3f;
const int MAXN = 100000 + 5 ;
int n, c[MAXN];
ll dp[MAXN][2];
string s[MAXN];
void ini()
{
for(int i = 0; i < MAXN; i++)
for(int j = 0; j < 2; j++)
dp[i][j] = LINF;
dp[0][0] = 0;
dp[0][1] = c[0];
}
void Solve()
{
cin >> n;
for(int i = 0; i < n; i++)
cin >> c[i];
for(int i = 0; i < n; i++)
cin >> s[i];
ini();
for(int i = 1; i < n; i++)
{
string s1 = s[i - 1];
reverse(s1.begin(), s1.end());
string s2 = s[i];
reverse(s2.begin(), s2.end());
if(s[i] >= s[i - 1]) dp[i][0] = min(dp[i][0], dp[i - 1][0]);
if(s[i] >= s1) dp[i][0] = min(dp[i][0], dp[i - 1][1] );
if(s2 >= s[i - 1]) dp[i][1] = min(dp[i][1], dp[i - 1][0] + c[i]);
if(s2>=s1) dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
}
if(dp[n-1][0] != LINF || dp[n-1][1] != LINF)
{
cout << min(dp[n-1][0], dp[n-1][1]) << endl;
}
else
{
puts("-1");
}
}
int main()
{
Solve();
return 0;
}
Codeforces Round #367 (Div. 2) Hard problem的更多相关文章
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #367 (Div. 2) C. Hard problem
题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset (0/1-Trie树)
Vasiliy's Multiset 题目链接: http://codeforces.com/contest/706/problem/D Description Author has gone out ...
- Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)
Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...
- Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)
Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...
- Codeforces Round #367 (Div. 2) (A,B,C,D,E)
Codeforces Round 367 Div. 2 点击打开链接 A. Beru-taxi (1s, 256MB) 题目大意:在平面上 \(n\) 个点 \((x_i,y_i)\) 上有出租车,每 ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset
题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...
- 「专题训练」Hard problem(Codeforces Round #367 Div. 2 C)
题意与分析 题意:给出\(n\)个字符串,可以反转任意串,反转每个串都有其对应的花费\(c_i\).经过操作后是否能满足字符串\(\forall i \in [1,n] \text{且} i \in ...
- Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)
http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...
随机推荐
- 1-3-1 关于API
主要内容:API函数及其相关内容的介绍.Windows编程相关基础知识介绍 1.API函数的概念 <1>API(Application Programming interface),即应用 ...
- nginx下的rewrite
一.正则表达式匹配,其中: * ~ 为区分大小写匹配 * ~* 为不区分大小写匹配 * !~和!~*分别为区分大小写不匹配及不区分大小写不匹配 二.文件及目录匹配,其中: * -f和!-f用来判断是否 ...
- HDU-1561 The more, The Better (树形DP+分组背包)
题目大意:给出一片森林,总共有n个点,并且都有权值.从中选出m个,使权值和最大.其中,选某个节点之前必须先选其父节点. 题目分析:给所有的树都加一个共同的权值为0的根节点,使森林变成一棵树.定义状态d ...
- 复利计算器4.0 【java版】
import java.util.Scanner; public class FuLi { public static void main(String[] args) { ; Scanner sca ...
- EXTJS 5.0 资料
http://blog.csdn.net/sushengmiyan/article/category/2435029
- Measuring PostgreSQL Checkpoint Statistics
Checkpoints can be a major drag on write-heavy PostgreSQL installations. The first step toward ident ...
- linux日志处理logrotate使用
摘录自:http://linux008.blog.51cto.com/2837805/555829 内容在这里做个备份,以便以后查看: 使用logrotate管理nginx日志文件 2011-04- ...
- browser-ua
故事还得从头说起,最初的主角叫NCSA Mosaic,简称Mosaic(马赛克),是1992年末位于伊利诺伊大学厄巴纳-香槟分校的国家超级计算机应用中心(National Center for Sup ...
- 【转】弹出可拖动的DIV层提示窗口
来源:www.divcss5.com <html> <head> <meta http-equiv="Content-Type" content=&q ...
- Linux- Bond
---------------check NIC----------------- ①mii-tool 命令 ②ethtool 命令 -i 参数,显示网卡的驱动信息, -S 参数,底层工作状态信息③d ...