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的更多相关文章

  1. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  2. Codeforces Round #367 (Div. 2) C. Hard problem

    题目链接:Codeforces Round #367 (Div. 2) C. Hard problem 题意: 给你一些字符串,字符串可以倒置,如果要倒置,就会消耗vi的能量,问你花最少的能量将这些字 ...

  3. 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 ...

  4. Codeforces Round #367 (Div. 2) B. Interesting drink (模拟)

    Interesting drink 题目链接: http://codeforces.com/contest/706/problem/B Description Vasiliy likes to res ...

  5. Codeforces Round #367 (Div. 2) A. Beru-taxi (水题)

    Beru-taxi 题目链接: http://codeforces.com/contest/706/problem/A Description Vasiliy lives at point (a, b ...

  6. 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)\) 上有出租车,每 ...

  7. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset

    题目链接:Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset 题意: 给你一些操作,往一个集合插入和删除一些数,然后?x让你找出与x异或后的最大值 ...

  8. 「专题训练」Hard problem(Codeforces Round #367 Div. 2 C)

    题意与分析 题意:给出\(n\)个字符串,可以反转任意串,反转每个串都有其对应的花费\(c_i\).经过操作后是否能满足字符串\(\forall i \in [1,n] \text{且} i \in ...

  9. Codeforces Round #367 (Div. 2) D. Vasiliy's Multiset(01字典树求最大异或值)

    http://codeforces.com/contest/706/problem/D 题意:有多种操作,操作1为在字典中加入x这个数,操作2为从字典中删除x这个数,操作3为从字典中找出一个数使得与给 ...

随机推荐

  1. Apache的https协议配置

    一.http协议和https协议的传输格式 http:文本格式的协议 https:二进制格式的协议 二.x509.3证书格式: 证书格式的版本号 证书序列号 证书签名算法 证书颁发者 有效期 持有者的 ...

  2. C++虚函数、赋值兼容原则

    #include <iostream.h> class A { public: void f1() { cout << "a" << endl; ...

  3. 论文笔记之:From Facial Parts Responses to Face Detection: A Deep Learning Approach

    From Facial Parts Responses to Face Detection: A Deep Learning Approach ICCV 2015 从以上两张图就可以感受到本文所提方法 ...

  4. centos 编译swoole

    /usr/include/php/ext/pcre/php_pcre.h:45: error: expected '=', ',', ';', 'asm' or '__attribute__' bef ...

  5. jquery下ie的margin-left ----bug 以及parseInt方法bug

    ie下使用jquery的方法css('margin-left')可能会出现'auto'----从而使结果不可计算,即便使用parseInt()方法也不行 因为parseInt()方法的bug是如果参数 ...

  6. Android TextView内容过长加省略号,点击显示全部内容

    在Android TextView中有个内容过长加省略号的属性,即ellipsize,用法如下: 在xml中:android:ellipsize="end"    省略号在结尾an ...

  7. vb.net 动态调用api

    Imports System Imports System.Runtime.InteropServices Public Class DllInvoke Public Sub New(ByVal DL ...

  8. PHP实现动态规划背包问题

    有一堆货物,有各种大小和价值不等的多个物品,而你只有固定大小的背包,拿走哪些能保证你的背包带走的价值最多 动态规划就是可以记录前一次递归过程中计算出的最大值,在之后的递归期间使用,以免重复计算. &l ...

  9. 数据恢复-extundelete

    http://extundelete.sourceforge.net/options.html 误删除/usr/share目录因此考虑恢复目录过程如下:1.选用extundelete软件来进行恢复,源 ...

  10. linux中Zabbix邮件报警设置配置步骤

    使用外部邮箱账号发送报警邮件设置 配置Zabbix服务端外部邮箱 vi /etc/mail.rc #编辑,添加以下信息 set from=xxx@163.com smtp=smtp.163.com s ...