题目:这里

题意:给定n个字符串,每个字符串可以进行一项操作,就是将这个字符串交换,就是该字符串的第一个和最后一个交换,第二个和倒数第二个交换,以此类推,当然可以选择对于

该字符串进行或不进行这项操作,而每个字符串都有一个相应的能量值,进行操作了就要消耗那么多能量值,最后是否能在消耗的能量值最小的情况下保证这些字符串是升序的(

字典序从小到大),不能就输出-1。

字符串用string,DP,dp[i][j]表示到第i个字符串的状态为j的时候(j为1表示这个串交换了,j为0表示这个串没有交换),注意的是不能形成升序的判断,并不是任意相邻的两个字符串

能够满足升序就可以了,还要考虑前面的。

 #include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std; typedef long long ll;
#define inf 0x3f3f3f3f3f3f
const int M = 1e5 + ;
ll v[M],dp[M][];
string str[M]; string revers(string x)
{
string y=x;
int len=x.length();
for (int i= ; i<len/ ; i++)
swap(y[i],y[len-i-]);
return y;
} ll min(ll x,ll y) {return x<y?x:y;} int main()
{
int n;
scanf("%d",&n);
for (int i= ; i<=n ; i++) {
scanf("%I64d",&v[i]);
dp[i][]=dp[i][]=inf;
}
for (int i= ; i<=n ; i++) cin>>str[i];
dp[][]=;dp[][]=v[];
bool flag1=false;int i;
for (i= ; i<=n ; i++)
{
bool flag2=false;
if (str[i]>=str[i-])
dp[i][]=dp[i-][],flag2=true;
if (str[i]>=revers(str[i-]))
dp[i][]=min(dp[i][],dp[i-][]),flag2=true;
if (revers(str[i])>=str[i-])
dp[i][]=dp[i-][]+v[i],flag2=true;
if (revers(str[i])>=revers(str[i-]))
dp[i][]=min(dp[i][],dp[i-][]+v[i]),flag2=true;
//if (!flag2) {flag1=true;break;}
if(dp[i][]==inf&&dp[i][]==inf)
break;
}
//if (flag1) puts("-1");
if (i!=n+)puts("-1");
else printf("%I64d\n",min(dp[n][],dp[n][]));
return ;
}

CodeForces 607C (DP) Hard problem的更多相关文章

  1. codeforces C. Sonya and Problem Wihtout a Legend(dp or 思维)

    题目链接:http://codeforces.com/contest/713/problem/C 题解:这题也算是挺经典的题目了,这里附上3种解法优化程度层层递进,还有这里a[i]-i<=a[i ...

  2. codeforces的dp专题

    1.(467C)http://codeforces.com/problemset/problem/467/C 题意:有一个长为n的序列,选取k个长度为m的子序列(子序列中不能有位置重复),求所取的k个 ...

  3. [codeforces 528]B. Clique Problem

    [codeforces 528]B. Clique Problem 试题描述 The clique problem is one of the most well-known NP-complete ...

  4. codeforces.com/contest/325/problem/B

    http://codeforces.com/contest/325/problem/B B. Stadium and Games time limit per test 1 second memory ...

  5. Codeforces 442B Andrey and Problem(贪婪)

    题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,如今他有n个朋友.每一个朋友想出题目的概率为pi,可是他能够 ...

  6. CodeForces 867B Save the problem

    B. Save the problem! http://codeforces.com/contest/867/problem/B time limit per test 2 seconds memor ...

  7. Codeforces 776D The Door Problem

    题目链接:http://codeforces.com/contest/776/problem/D 把每一个钥匙拆成两个点${x,x+m}$,分别表示选不选这把钥匙. 我们知道一扇门一定对应了两把钥匙. ...

  8. codeforces 803G Periodic RMQ Problem

    codeforces 803G Periodic RMQ Problem 题意 长度为\(1e5\)的数组复制\(1e4\)次,对新的数组进行区间覆盖和区间最小值查询两种操作,操作次数\(1e5\). ...

  9. [Codeforces 986E] Prince's Problem

    [题目链接] https://codeforces.com/contest/986/problem/E [算法] X到Y的路径积 , 可以转化为X到根的路径积乘Y到根的路径积 , 除以LCA到根的路径 ...

随机推荐

  1. Struts2通配符问题(待解决)

    目录结构如下: 运行结果: 为什么会访问input.jsp?不应该是访问emp-input吗?

  2. MapReduce单表关联学习~

    首先考虑表的自连接,其次是列的设置,最后是结果的整理. 文件内容: import org.apache.hadoop.conf.Configuration; import org.apache.had ...

  3. Error: Error #2014: Feature is not available at this time. at flash.filesystem::File$/initDocumentsDir()

    Error: Error #2014: Feature is not available at this time. at flash.filesystem::File$/initDocumentsD ...

  4. NGUI之渲染DrawCall的合并

    在Unity中,每次引擎准备数据并通知GPU的过程称为一次Draw Call.Draw Call值越低,会得到更好的渲染性能. (NGUI 查看DrawCall工具(NGUI-OPEN-Draw Ca ...

  5. XE6移动开发环境搭建之IOS篇(8):在Mac OSX 10.8中安装XE6的PAServer(有图有真相)

    网上能找到的关于Delphi XE系列的移动开发环境的相关文章甚少,本文尽量以详细的图文内容.傻瓜式的表达来告诉你想要的答案. 原创作品,请尊重作者劳动成果,转载请注明出处!!! 安装PAServer ...

  6. 创建线程方式-NSOperation

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  7. Good Bye 2013 C

    C. New Year Ratings Change time limit per test 1 second memory limit per test 256 megabytes input st ...

  8. Squid服务日志分析

    Squid服务日志分析 Apache 和 Squid 是两种著名的代理缓存软件,但Squid 较 Apache 而言是专门的代理缓存服务器软件,其代理缓存的功能强大,支持 HTTP/1.1 协议,其缓 ...

  9. MySQL安装过程net start mysql 启动失败 报“错误2,系统找不到文件”的解决办法

    MySQL安装过程net start mysql 启动失败 报“错误2,系统找不到文件”的解决办法 错误2,系统找不到文件. 开始...运行... regedit  注册表项: HKEY_LOCAL_ ...

  10. html嵌套表格示例

    常用嵌套表格示例,出自<网页开发手记:HTML+CSS+JavaScript实战详解>   <html>   <head>   <title>嵌套表格布 ...