题目链接: Hard problem

题意:每个字符串可以选择反转或者不反转,给出反转每个字符串的代价,问使最少的代价使得这个字符串序列成字典序。

dp[i][j] = x : 第一维是第i个字符串,第二维表示当前字符串是否反转。x表示当前状态下使前i个字符串成字典序的最小代价。

感觉可以做的dp,然后我没写出来。T_T

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <string>
#define maxn 100100
#include <algorithm>
#define inf 1e20
#define LL long long
using namespace std; LL dp[maxn][2];
LL a[maxn]; string s[maxn];
string ss[maxn]; int main() {
//freopen("in.cpp", "r", stdin);
int n;
while(~scanf("%d", &n)) {
for (int i=1; i<=n; ++i) {
scanf("%I64d", &a[i]);
}
for (int i=1; i<=n; ++i) {
cin >> s[i];
ss[i] = s[i];
reverse(ss[i].begin(), ss[i].end());
} dp[1][0] = 0;
dp[1][1] = a[1]; for (int i=2; i<=n; ++i) {
dp[i][0] = inf;
dp[i][1] = inf;
if (s[i] >= s[i-1]) dp[i][0] = min(dp[i][0], dp[i-1][0]);
if (s[i] >= ss[i-1]) dp[i][0] = min(dp[i][0], dp[i-1][1]);
if (ss[i] >= ss[i-1]) dp[i][1] = min(dp[i][1], dp[i-1][1] + a[i]);
if (ss[i] >= s[i-1]) dp[i][1] = min(dp[i][1], dp[i-1][0] + a[i]);
}
LL ans = min(dp[n][0], dp[n][1]);
if (ans >= inf) ans = -1;
printf("%I64d\n", ans);
}
return 0;
}

 

CodeForces #367 div2 C的更多相关文章

  1. CodeForces #367 div2 D Trie

    题目链接:Vasiliy's Multiset 题意:这里有一个set容器,有三种操作,+ num, - num, ? num,分别代表往容器里加上num,或者拿走num,或着从容器里找一个数temp ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. An internal error occurred during: "Building workspace".

    当在eclipse中的web工程中增加了extjs4,出现An internal error occurred during: "Building workspace". Java ...

  2. 为mongodb加用户授权并使用RockMongo进行管理

    在刚安装完mongodb后是没有管理员的,如果要为mongodb设置管理员在启动时需要以用户授权的方式启动: /usr/local/webserver/mongodb/bin/mongod --dbp ...

  3. Android DrawerLayout Plus 增强版抽屉菜单

    DrawerLayout是官方提供的侧滑菜单,相比SliddingMenu,它更加轻量级.默认情况下,DrawerLayout可以设置左侧或者右侧滑出菜单.如下,   xml布局: <!-- & ...

  4. Dinic算法模板

    详解:http://blog.csdn.net/wall_f/article/details/8207595 算法时间复杂度:O(E * V * V) #include <cstdio> ...

  5. [转]SOCKET通信中TCP、UDP数据包大小的确定

    TCP.UDP数据包大小的确定 UDP和TCP协议利用端口号实现多项应用同时发送和接收数据.数据通过源端口发送出去,通过目标端口接收.有的网络应用只能使用预留或注册的静态端口:而另外一些网络应用则可以 ...

  6. log4j.properties的配置

    #初始化类中要使用的Logger对象实例:log4j.rootLogger = [ level ] , appenderName1, appenderName2, … #第一个参数是日志级别(DEBU ...

  7. VC++6.0在Win7以上系统上Open或Add to Project files崩溃问题 解决新办法

    崩溃原因是和office高版本冲突,比如我64位win7装了64位office2013及visio就遇到了这个问题(我很纳闷,记得重装系统前装的是32位office2013及visio就未曾遇到该问题 ...

  8. [问题2014S15] 复旦高等代数II(13级)每周一题(第十五教学周)

    [问题2014S15]  设 \(O\) 为 \(n\) 阶正交阵,\(A=\mathrm{diag}\{a_1,a_2,\cdots,a_n\}\) 为实对角阵, 证明: 方阵 \(OA\) 的特征 ...

  9. redhat6.3安装MySQL-server-5.6.13-1.el6.x86_64.rpm

    redhat6.3安装MySQL-server-5.6.13-1.el6.x86_64.rpm   首先下载下面三个文件:   [plain]  MySQL-client-5.6.13-1.el6.x ...

  10. 定时器图片轮播淡入淡出基本功能已实现,正在修改BUG中。。(附图效果和源代码)

    用JQ写的源码如下: 实现功能: 1,图片自动按顺序轮播,轮播选中的图片透明度为1,其他为0.1: 2,鼠标停在的图片上透明度为1,其他为0.1: 3,鼠标离开,继续轮播,起始位置为鼠标停在的图片的下 ...