题目:这里

题意:给定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. 在Linux下安装PHP过程中,编译时出现错误的解决办法

    在Linux下安装PHP过程中,编译时出现configure: error: libjpeg.(a|so) not found 错误的解决办法 configure: error: libjpeg.(a ...

  2. 黄聪:wordpress向一个页面POST数据,出现404页面访问不了

    出现这个情况,说明POST的数据中存在一些关键词,触发调用了page以外的模版.比如POST数据中有 name , author 等参数. 解决办法,就是把这些参数改一下名称.

  3. mysql 安装日志

    善于使用 mysqld.exe --console 来得到提示

  4. ISO 14229 简介 转载

    作者:autogeek 原文链接:http://www.cnblogs.com/autogeek/p/4458591.html 前言 由于工作中经常用到ISO-14229,因此决定对该协议做个总体介绍 ...

  5. DELPHI声明一个指针变量,什么时候需要分配内存,什么时候不需要分配内存?

    DELPHI声明一个指针变量,什么时候需要分配内存,什么时候不需要分配内存?比如我定义个变量 var p:Pchar;如果这个变量声明为全局变量,需要分配内存吗?分配为局部变量,需要分为内存吗?为什么 ...

  6. opencv透视变换

    关于透视投影的几何知识,以及求解方法,可以参考 http://media.cs.tsinghua.edu.cn/~ahz/digitalimageprocess/chapter06/chapt06_a ...

  7. C语言的time.h解释python的time

    clock_t clock(void),对比python的clock() 返回程序执行的时间,clock_t的实际类型是long #include<Windows.h> Sleep(); ...

  8. Winform数据导出Execl小工具

    前台界面.cs文件 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...

  9. 使用history.back()出现"警告: 网页已过期的解决办法"

    原因: 表单提交页面中使用了 session_start 函数.由于我们后退浏览的是缓存页,而该函数会强制当前页面不被缓存. 解决: PHP: 此提示出现在一个POST提交的页面,点到其它页面后,通过 ...

  10. Mysql命令行中文乱码的解决方法

    环境:Windows 8 64位,Mysql  5.0.96 for Win64 (x86) 数据库本身安装时默认已经是使用utf8编码的了,但在命令行中执行查询时,查询到的中文依然乱码,解决方法如下 ...