题目链接: Hard problem

题意:每个字符串可以选择反转或者不反转,给出反转每个字符串的代价,问使最少的代价使得这个字符串序列成字典序。

dp[i][j] = x : 第一维是第i个字符串,第二维表示当前字符串是否反转。x表示当前状态下使前i个字符串成字典序的最小代价。

感觉可以做的dp,然后我没写出来。T_T

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <iostream>
  4. #include <string>
  5. #define maxn 100100
  6. #include <algorithm>
  7. #define inf 1e20
  8. #define LL long long
  9. using namespace std;
  10.  
  11. LL dp[maxn][2];
  12. LL a[maxn];
  13.  
  14. string s[maxn];
  15. string ss[maxn];
  16.  
  17. int main() {
  18. //freopen("in.cpp", "r", stdin);
  19. int n;
  20. while(~scanf("%d", &n)) {
  21. for (int i=1; i<=n; ++i) {
  22. scanf("%I64d", &a[i]);
  23. }
  24. for (int i=1; i<=n; ++i) {
  25. cin >> s[i];
  26. ss[i] = s[i];
  27. reverse(ss[i].begin(), ss[i].end());
  28. }
  29.  
  30. dp[1][0] = 0;
  31. dp[1][1] = a[1];
  32.  
  33. for (int i=2; i<=n; ++i) {
  34. dp[i][0] = inf;
  35. dp[i][1] = inf;
  36. if (s[i] >= s[i-1]) dp[i][0] = min(dp[i][0], dp[i-1][0]);
  37. if (s[i] >= ss[i-1]) dp[i][0] = min(dp[i][0], dp[i-1][1]);
  38. if (ss[i] >= ss[i-1]) dp[i][1] = min(dp[i][1], dp[i-1][1] + a[i]);
  39. if (ss[i] >= s[i-1]) dp[i][1] = min(dp[i][1], dp[i-1][0] + a[i]);
  40. }
  41. LL ans = min(dp[n][0], dp[n][1]);
  42. if (ans >= inf) ans = -1;
  43. printf("%I64d\n", ans);
  44. }
  45. return 0;
  46. }

 

CodeForces #367 div2 C的更多相关文章

  1. CodeForces #367 div2 D Trie

    题目链接:Vasiliy's Multiset 题意:这里有一个set容器,有三种操作,+ num, - num, ? num,分别代表往容器里加上num,或者拿走num,或着从容器里找一个数temp ...

  2. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  3. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  4. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  5. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  6. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  7. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  8. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  9. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

随机推荐

  1. 解决MyEclipse报错问题

    转载文章.   做NC的时候从别人那拷了个NC_DEMO结果我这报错他那没报错  import nc.bs.wfengine.engine.ext.TaskTopicResolver;  报错信息:A ...

  2. 权限获取异常(不能用ModuleId,得换个名字)目前还没搞清楚为啥

    CenterController: /// <summary> /// 访问模块,写入系统菜单Id /// </summary> /// <param name=&quo ...

  3. SQL语句实现取消自增列属性

    SQL语句实现取消自增列属性 --由于在SQL-SERVER中,自增列属性不能直接修改,但可以通过以下方式变向实现 --1.如果仅仅是指定值插入,可用以下语句,临时取消 SET IDENTITY_IN ...

  4. python核心编程学习记录之模块

  5. MySQL and Postgres command equivalents (mysql vs psql)

    MySQL and Postgres command equivalents (mysql vs psql) 博客分类: Database   From: http://blog.endpoint.c ...

  6. osip状态机分析

    转载于:http://blog.csdn.net/lbc2100/article/details/48342889 OSIP的核心是系统状态机,在不同情况下,系统处于不同的状态,在某一状态下当系统发生 ...

  7. [课程设计]Sprint Two 回顾与总结&发表评论&团队贡献分

    [课程设计]Sprint Two 回顾与总结&发表评论&团队贡献分 ● 一.回顾与总结 (1)回顾 燃尽图: Sprint计划-流程图: milestones完成情况如下: (2)总结 ...

  8. 实现一个名为Person的类和它的子类Employee,Employee有两个子类Faculty 和Staff。

    (1)Person类中的属性有:姓名name(String类型),地址address(String类型), 电话号码telphone(String类型)和电子邮件地址email(String类型): ...

  9. 简单练习题2编写Java应用程序。首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”、“取款”和“余额查询”。其次, 编写一个主类,在主类中测试Account类的功能

    编写Java应用程序.首先定义一个描述银行账户的Account类,包括成员变 量“账号”和“存款余额”,成员方法有“存款”.“取款”和“余额查询”.其次, 编写一个主类,在主类中测试Account类的 ...

  10. Python3基础 isinstance 判断一个变量是否为指定的类型

    镇场诗:---大梦谁觉,水月中建博客.百千磨难,才知世事无常.---今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ ...