codeforces round367 div2.C (DP)
题目链接:http://codeforces.com/contest/706/problem/C
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+3;
const ll INF=1e18;
ll dp[N][2];
string a[N],b[N];
int c[N];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
scanf("%d",c+i);
for(int i=0;i<n;i++)
{
cin>>a[i];
b[i]=a[i];
reverse(b[i].begin(),b[i].end());
}
for(int i=1;i<n;i++)
dp[i][0]=dp[i][1]=INF;
dp[0][1]=c[0];
int i;
for(i=1;i<n;i++)
{
if(a[i]>=a[i-1])
dp[i][0]=dp[i-1][0];
if(b[i]>=a[i-1])
dp[i][1]=dp[i-1][0]+c[i];
if(a[i]>=b[i-1])
dp[i][0]=min(dp[i][0],dp[i-1][1]);
if(b[i]>=b[i-1])
dp[i][1]=min(dp[i][1],dp[i-1][1]+c[i]);
if(dp[i][0]==INF&&dp[i][1]==INF)
break;
}
if(i==n)
printf("%I64d\n",min(dp[n-1][0],dp[n-1][1]));
else
printf("-1\n");
return 0;
}
codeforces round367 div2.C (DP)的更多相关文章
- codeforces #round363 div2.C-Vacations (DP)
题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...
- codeforces 369 div2 C dp
http://codeforces.com/contest/711 C. Coloring Trees time limit per test 2 seconds memory limit per t ...
- CodeForces #363 div2 Vacations DP
题目链接:C. Vacations 题意:现在有n天的假期,对于第i天有四种情况: 0 gym没开,contest没开 1 gym没开,contest开了 2 gym开了,contest没开 3 ...
- Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)
Problem Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...
- Codeforces #180 div2 C Parity Game
// Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
- Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)
Problem Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...
- Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)
Problem Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...
- [Codeforces 1201D]Treasure Hunting(DP)
[Codeforces 1201D]Treasure Hunting(DP) 题面 有一个n*m的方格,方格上有k个宝藏,一个人从(1,1)出发,可以向左或者向右走,但不能向下走.给出q个列,在这些列 ...
随机推荐
- TCP
TCP(Transmission Control Protocol 传输控制协议)是一种面向连接的.可靠的.基于字节流的传输层通信协议. 在因特网协议族中,TCP层是位于IP层之上,应用层之下的中间层 ...
- IO流 总结一
字符流: FileReader FileWriter BufferedReader BufferedWriter readLine(); 字节流: FileInputReader FileOutput ...
- linq分页扩展(转)
原文地址:http://www.cnblogs.com/RainbowInTheSky/p/4590508.html public static List<T> ToPagedList&l ...
- for循环练习——7月23日
练习一:输入一个整数,求从1到这个数的累加和 //练习1:输入一个整数,计算从1加到这个数的结果 Console.Write("请输入一个正整数:"); int a = int.P ...
- while, do-while ,switch···case语句的学习与运用
1.while语句:当···的时候 格式:初始条件 while(循环条件) { 循环体; 状态改变; } 相当于 ...
- C#常用的加密算法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 方法一: //须添加对System.Web的引用 ...
- JDE隐藏Constant等(Hide Object)
Grid中隐藏列,列值可以使用属性配置,但是列表头Constant需要用函数修改,如下所示:
- [转]无IDE时编译和运行Java
本文由 ImportNew - Grey 翻译自 dzone.欢迎加入Java小组.转载请参见文章末尾的要求. 最近 Java subreddit 出现了一篇”在没有IDE的情况下编译Java包” 的 ...
- JavaScript 事件委托的技术原理
如今的 JavaScript 技术界里最火热的一项技术应该是‘事件委托(event delegation)’了.使用事件委托技术能让你避免对特定的每个节点添加事件监听器:相反,事件监听器是被添加到它们 ...
- 使用js给页面显示的图片添加水印效果
功能描述:使用Jquery 给页面的图片添加 版权信息水印. 这里的水印并不是真的把每一张图片上都添加了水印.而是在图片的上方添加了一个层,层中包含了水印图片效果就像是图片上加了水印. 功能原理:1, ...