链接[http://codeforces.com/group/1EzrFFyOc0/contest/706/problem/C]

题意:

他希望它们按词典顺序排序(就像字典中那样),但他不允许交换其中的任何一个。

唯一允许他做的操作是将其中的任何一个反转(第一个字符变成最后一个,

第二个字符变成最后一个,以此类推)。

思路;

DP,不断更新花费的值,并且判断是否可以满足字典序排序。

dp[i][0]表示,表示排到第i+1个字符串,不需要反转需要的花费,dp[i][0]表示排到第i+1个字符串,需要反转需要的花费。

代码

#include<bits/stdc++.h>
using namespace std;
#define ll long long
string reverse(string s)
{
string res=s;
int i,len=res.length();
for(i=0;i<len/2;++i)
swap(res[i],res[len-1-i]);
return res;
}
string s[100005][2];//二维字符串数组
ll dp[100005][2];
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0); //map<string,int> m;
int n,i;
ll a[100005];
//freopen("in.txt","r",stdin);
while(cin>>n){
for(i=0;i<n;i++)
cin>>a[i];
for(i=0;i<n;++i)
dp[i][0]=dp[i][1]=9999999999999999;
dp[0][0]=0,dp[0][1]=a[0];
for(i=0;i<n;++i)
{
cin>>s[i][0];
s[i][1]=reverse(s[i][0]);
}
for(i=1;i<n;i++)
{
if(s[i][0]>=s[i-1][0])
dp[i][0]=dp[i-1][0];//二者都不需要反转
if(s[i][1]>=s[i-1][0])
dp[i][1]=dp[i-1][0]+a[i];//后者需要反转就dp[i-1][0]+a[i]
if(s[i][0]>=s[i-1][1])
dp[i][0]=min(dp[i][0],dp[i-1][1]);//前者需要反转,因为开始初始化一个很大的数所以要比较
if(s[i][1]>=s[i-1][1])
dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);//二者都需要反转,且初始化为很大,需要比较
if(dp[i][0]==9999999999999999&&dp[i][1]==9999999999999999)
//如果不满足字典序排序,就退出DP
break;
}
ll ans=min(dp[n-1][0],dp[n-1][1]);
if(i>=n) cout<<ans<<endl;
else cout<<-1<<endl;
}
return 0;
}

CF367C. Hard problem的更多相关文章

  1. 1199 Problem B: 大小关系

    求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...

  2. No-args constructor for class X does not exist. Register an InstanceCreator with Gson for this type to fix this problem.

    Gson解析JSON字符串时出现了下面的错误: No-args constructor for class X does not exist. Register an InstanceCreator ...

  3. C - NP-Hard Problem(二分图判定-染色法)

    C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS     Memory Limit:262144 ...

  4. Time Consume Problem

    I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...

  5. Programming Contest Problem Types

        Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...

  6. hdu1032 Train Problem II (卡特兰数)

    题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能.    (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...

  7. BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】

    2301: [HAOI2011]Problem b Time Limit: 50 Sec  Memory Limit: 256 MBSubmit: 4032  Solved: 1817[Submit] ...

  8. [LeetCode] Water and Jug Problem 水罐问题

    You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...

  9. [LeetCode] The Skyline Problem 天际线问题

    A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...

随机推荐

  1. log4.net 配置-控制台输出带颜色的日志

    一般对控制台输入的日志我们使用的 Appender 为 ConsoleAppender ,日志为白色文字. 当我们想到 DEBUG,INFO,WARN,ERROR  每种日志设置一种颜色时我们需要将  ...

  2. Spring boot + mybatis + orcale

    接着上次的实现, 添加 mybatis 查询 orcale 数据库 第一步: 新建几个必须的包, 结果如下 第二步: 在service包下新建personService.java 根据名字查perso ...

  3. Android Activity学习笔记——Activity的启动和创建

    http://www.cnblogs.com/bastard/archive/2012/04/07/2436262.html 最近学习Android相关知识,感觉仅仅了解Activity几个生命周期函 ...

  4. Centos7下安装docker(1)

    1.确认系统没安装任何docker相关的安装包 yum remove docker docker-common docker-selinux docker-engine centos7的docker存 ...

  5. A - Dogs and Cages HDU - 6243(组合数学)

    题意:在1—n的数字,放入编号为1—n的框中,每个框只放一个数字,问数字与所放的框的编号不同的个数的期望值. 思路:在1—n中任选一个数字,设为k 那么 k 排到非k编号的框中的方案数为 n!-(n- ...

  6. 如何在Windows平台下安装配置Memcached

    Memcached是一个自由开源的,高性能,分布式内存对象缓存系统. Memcached是以LiveJournal旗下Danga Interactive公司的Brad Fitzpatric为首开发的一 ...

  7. DNS攻击

    DNS攻击 实验是基于Linux系统,配置了bind9服务的机器 大纲 本地修改Host文件重定向路径到指定地址 对User的DNS查询进行欺骗攻击 在同一局域网下,对DNS服务器的DNS查询进行欺骗 ...

  8. ROS教程4 ROS自定义srv类型及使用

    创建srv文件 在上一节单独为自定义的消息和服务的包 test_msgs 里面 创建 srv文件夹 进入创建 testsrv.srv 文件 ,内容为: (srv文件和msg文件类似,唯一不同的是它包含 ...

  9. Oracle数据库查询优化(上百万级记录如何提高查询速度)

    1.对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引.2.应尽量避免在 where 子句中对字段进行 null 值判断,否则将导致引擎放弃使用索引 ...

  10. Web 前端怎样入门?(转)

    转自知乎https://www.zhihu.com/question/32314049/answer/100898227