CF367C. Hard problem
链接[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的更多相关文章
- 1199 Problem B: 大小关系
求有限集传递闭包的 Floyd Warshall 算法(矩阵实现) 其实就三重循环.zzuoj 1199 题 链接 http://acm.zzu.edu.cn:8000/problem.php?id= ...
- 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 ...
- C - NP-Hard Problem(二分图判定-染色法)
C - NP-Hard Problem Crawling in process... Crawling failed Time Limit:2000MS Memory Limit:262144 ...
- Time Consume Problem
I joined the NodeJS online Course three weeks ago, but now I'm late about 2 weeks. I pay the codesch ...
- Programming Contest Problem Types
Programming Contest Problem Types Hal Burch conducted an analysis over spring break of 1999 and ...
- hdu1032 Train Problem II (卡特兰数)
题意: 给你一个数n,表示有n辆火车,编号从1到n,入站,问你有多少种出站的可能. (题于文末) 知识点: ps:百度百科的卡特兰数讲的不错,注意看其参考的博客. 卡特兰数(Catalan):前 ...
- BZOJ2301: [HAOI2011]Problem b[莫比乌斯反演 容斥原理]【学习笔记】
2301: [HAOI2011]Problem b Time Limit: 50 Sec Memory Limit: 256 MBSubmit: 4032 Solved: 1817[Submit] ...
- [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 ...
- [LeetCode] The Skyline Problem 天际线问题
A city's skyline is the outer contour of the silhouette formed by all the buildings in that city whe ...
随机推荐
- Python进阶(三)
匿名函数 匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达式的结果.用匿名函数有个好处,因为函数没有名字,不必担心函数名冲突.此外,匿名函数也是一个函数对象,也可以把匿名函 ...
- C#Url处理类
using System; using System.Text.RegularExpressions; using System.Web; using System.Collections.Speci ...
- centos7升级内核版本
本文转载http://blog.csdn.net/nciasd/article/details/51490146,大神非常厉害!!!!! 查看当前系统的内核版本 # uname -r 1.导入key ...
- 移动端自适应rem布局
补充一个基本知识,不许笑,1rem等于HTML中设置的字体大小(px) 首先,HTML 的 head 部分中加入如下代码: <meta name="viewport" con ...
- UVA12166-Equilibrium Mobile
Problem UVA12166-Equilibrium Mobile Accept:529 Submit:4330 Time Limit: 3000 mSec Problem Descriptio ...
- Java中关于CyclicBarrier的使用
CyclicBarrier工具类主要是控制多个线程的一起执行,CyclicBarrier 实例可以多次使用. 演示程序: import java.util.Random; import java.ut ...
- 2.1 View与ViewGroup的概念
http://www.runoob.com/w3cnote/android-tutorial-view-viewgroup-intro.html UI Overview 在Android APP中,所 ...
- metamask源码学习导论
()MetaMask Browser Extension https://github.com/MetaMask/metamask-extension 这就是整个metamask的源码所在之处,好好看 ...
- LoadRunner 11安装Micosoft Visual C++ 2005 SP1时提示命令行选项语法错误
如果安装LoadRunner 11时弹窗提示"Micosoft Visual C++ 2005 SP1 可再发行组件包(X86):'命令行选项语法错误.键入命令 / ? 可获得帮助信息'&q ...
- Android ScrollView和ListView联用,且ListView可以下拉刷新和上拉加载
ScrollView嵌套listView且ListView可以实现上拉加载. 由于代码太长,在此只提供实现思路: 先不说上拉加载的事,咱们先回想一下,ScrollView和LsitView联用,时的解 ...