题目链接: 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. 匈牙利命名法、骆驼命名法、帕斯卡(pascal)命名法

    (2008-05-24 13:37:55) 转载▼ 标签: 杂谈 分类: 编程杂文 一.匈牙利命名法:         广泛应用于象Microsoft Windows这样的环境中. Windows 编 ...

  2. Preconditions优雅的检验参数

    Preconditions里面的方法: 1 .checkArgument(boolean) : 功能描述:检查boolean是否为真. 用作方法中检查参数 失败时抛出的异常类型: IllegalArg ...

  3. 郑轻校赛题目 问题 G: 多少个0

    问题 G: 多少个0 时间限制: 1 Sec  内存限制: 128 MB提交: 192  解决: 40 题目描述 一个n*n的方格,每个格子中间有一个数字是2或者5,现在从方格的左上角走到右下角,每次 ...

  4. 19. 星际争霸之php设计模式--迭代器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  5. 《zw版·Halcon-delphi系列原创教程》 Halcon分类函数004·edge,边缘处理

    <zw版·Halcon-delphi系列原创教程> Halcon分类函数004·edge,边缘处理 为方便阅读,在不影响说明的前提下,笔者对函数进行了简化: :: 用符号“**”,替换:“ ...

  6. nodejs框架express快速开始

    认识express 创建应用 get请求 简述中间件 all方法 use方法1 use方法2 回调函数 获取主机.路径名 Get请求 - query Get请求 - param Get请求 - par ...

  7. Scala入门学习笔记三--数组使用

    前言 本篇主要讲Scala的Array.BufferArray.List,更多教程请参考:Scala教程 本篇知识点概括 若长度固定则使用Array,若长度可能有 变化则使用ArrayBuffer 提 ...

  8. convert \uXXXX String to Unicode Characters in Python3.x

    转换\uXXXX if Python3.x: str.decode no longer exists in 3.x. that']s why Python 3.4: str : AttributeEr ...

  9. es5.0安装问题

    ES的5.0版本听说在性能上大大优化,于是老大说准备换5.0版本.由于在技术群看到很多人都说ES 5.0 安装有问题,在这里贴出自己在使用最新版5.0遇到的问题和解决方法 1.Elasticsearc ...

  10. [充电][库]Zlib文件压缩和解压

    原文链接: http://www.cnblogs.com/fairycao/archive/2009/12/09/1620414.html 开源代码:http://www.zlib.net/zlib使 ...