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. 國王遊戲(2012年NOIP全国联赛提高组)

    题目描述 Description 恰逢 H 国国庆,国王邀请 n 位大臣来玩一个有奖游戏.首先,他让每个大臣在左.右手上面分别写下一个整数,国王自己也在左.右手上各写一个整数.然后,让这 n位大臣排成 ...

  2. 卸载linux自带版本JDK

    1)卸载系统自带的jdk版本:    查看自带的jdk:    #rpm -qa|grep gcj    可能看到如下类似的信息:    libgcj-4.1.2-44.el5    java-1.4 ...

  3. POJ1419 Graph Coloring(最大独立集)(最大团)

                                                               Graph Coloring Time Limit: 1000MS   Memor ...

  4. poj3159 最短路(差分约束)

    题意:现在需要分糖果,有n个人,现在有些人觉得某个人的糖果数不能比自己多多少个,然后问n最多能在让所有人都满意的情况下比1多多少个. 这道题其实就是差分约束题目,根据题中给出的 a 认为 b 不能比 ...

  5. kuangbin_ShortPath P (HDU 4725)

    很有挑战的一题 直接暴力建图的话毫无疑问O(n^2)会TLE 每层虚拟一个点又会让没有点的层也能连过去 参考kuangbin菊苣的方法每层用了两个虚拟点 n+i*2-1 是入口 n+i*2 是出口 然 ...

  6. pyCharm使用

    1.修改文件和代码模板 修改文件头 2.显示行号

  7. 如何用 matlab 在图片上绘制矩形框 和 添加文字 ?

    如何给图像添加矩形框?以及添加想要输入的文字 ? 案例程序,如下所示: clc; close all; clear all;image = imread('/home/wangxiao/Picture ...

  8. sed详解

    1. Sed简介 sed 是一种在线编辑器,它一次处理一行内容.处理时,把当前处理的行存储在临时缓冲区中,称为“模式空间”(pattern space),接着用sed命令处理缓冲区中的内容,处理完成后 ...

  9. Comparison of B-Tree and Hash Indexes

    Understanding the B-tree and hash data structures can help predict how different queries perform on ...

  10. nginx反向代理原理和配置讲解

    最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式 ...