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. 转贴:sudo apt-get install 可以安装的一些软件

    Ubuntu 下的一些软件安装sudo apt-get install virtualbox#华主席推荐 2007年年度最佳软件,最佳编辑选择奖得主.....sudo apt-get install ...

  2. Java跟C.C++相互调用

    //由于诸多误解,我对函数注解说明下,这2个参数谁才是真正的皇帝,谁代替谁了//首先.这个函数是我自己为说明问题写的,由于和头文件写在一起,故此加入这个宏,//不然c++编译器报告类型不对,你懂得// ...

  3. C++@语句块

    #include <iostream> using namespace std; int main() { { int x=1; cout << x << endl ...

  4. windows API

    Process and Thread Functions https://msdn.microsoft.com/en-us/library/windows/desktop/ms684847(v=vs. ...

  5. redis模块

    http://www.cnblogs.com/melonjiang/p/5342505.html http://www.django-china.cn/topic/1054/ 1.连接方式 redis ...

  6. shell脚本实例-系统监控

    shell脚本监控网站并实现邮件.短信报警shell进程监控脚本(发送邮件报警)Shell脚本监控服务器在线状态和邮件报警的方法 http://www.jbxue.com/jb/shell/ 11. ...

  7. ibm硬件知识点

    ibm http://www-03.ibm.com/systems/storage/disk/storwize_v3700/index.html Current software level: Ver ...

  8. wikioi 1201 最小数和最大数

    /*==================================================== 1201 最小数和最大数 题目描述 Description 输入n个数,n<=100 ...

  9. CSS之column语法

    columns column-width:[<length>|auto] 定义每栏的宽度 column-span:1|all 1:只在本栏中显示:all:横跨所有栏目并定位在栏目的Z轴之上 ...

  10. Dom之标签增删操作

    dom操作:THML新增子标签 a标签(appendChild) <!DOCTYPE html><html lang="en"><head> & ...