【动态规划】Codeforces 706C Hard problem
来源:互联网 发布:win10 php环境搭建 编辑:程序博客网 时间:2024/05/20 07:58
题目链接:
http://codeforces.com/contest/706/problem/C
题目大意:
n(2 ≤ n ≤ 100 000)个字符串(长度不超过100000),翻转费用为Ci(<=109),求所有字符串从上到下符合字典序从小到大的最小费用。无解输出-1。
题目思路:
【动态规划】
每个字符串有2种状态,翻转或者不翻转,每次只与上一个字符串是否翻转有关,可以用DP。
费用很大,要用long long。
无解的时候我直接break了WA了好久。
////by coolxxx//#include<iostream>#include<algorithm>#include<string>#include<iomanip>#include<memory.h>#include<time.h>#include<stdio.h>#include<stdlib.h>#include<string.h>//#include<stdbool.h>#include<math.h>#define min(a,b) ((a)<(b)?(a):(b))#define max(a,b) ((a)>(b)?(a):(b))#define abs(a) ((a)>0?(a):(-(a)))#define lowbit(a) (a&(-a))#define sqr(a) ((a)*(a))#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))#define mem(a,b) memset(a,b,sizeof(a))#define eps (1e-8)#define J 10000000#define MAX 0x7f7f7f7f#define PI 3.1415926535897#define N 100004using namespace std;typedef long long LL;int cas,cass;int n,m,lll,ans;LL c[N];LL f[N][2];int l[2];char s[4][N];int main(){#ifndef ONLINE_JUDGE//freopen("1.txt","r",stdin);//freopen("2.txt","w",stdout);#endifint i,j,k;//for(scanf("%d",&cas);cas;cas--)//for(scanf("%d",&cas),cass=1;cass<=cas;cass++)//while(~scanf("%s",s))while(~scanf("%d",&n)){mem(f,0x7f);mark=0;for(i=1;i<=n;i++)scanf("%I64d",&c[i]);scanf("%s",s[0]);l[0]=strlen(s[0]);for(i=0;i<l[0];i++)s[2][i]=s[0][l[0]-1-i];f[1][0]=0;f[1][1]=c[1];for(i=2,j=1;i<=n;i++,j^=1){scanf("%s",s[j]);l[j]=strlen(s[j]);for(k=0;k<l[j];k++)s[j+2][k]=s[j][l[j]-1-k];s[j+2][k]='\000';if(strcmp(s[j],s[j^1])>=0)f[i][0]=min(f[i-1][0],f[i][0]);if(strcmp(s[j],s[(j^1)+2])>=0)f[i][0]=min(f[i-1][1],f[i][0]);if(strcmp(s[j+2],s[j^1])>=0)f[i][1]=min(f[i-1][0]+c[i],f[i][1]);if(strcmp(s[j+2],s[(j^1)+2])>=0)f[i][1]=min(f[i-1][1]+c[i],f[i][1]);}if(f[n][0]==f[0][0] && f[n][1]==f[0][0])puts("-1");else printf("%I64d\n",min(f[n][0],f[n][1]));}return 0;}/*////*/
0 0
- 【动态规划】Codeforces 706C Hard problem
- codefprces 706C C. Hard problem (动态规划)
- Codeforces-706C Hard problem
- CodeForces 706C Hard problem
- 【Codeforces 706C . Hard problem】
- codeforces 706C Hard problem
- Codeforces 706C Hard problem
- codeforces706C Hard problem 动态规划
- Codeforces 706C Hard problem(dp+字符串)
- codeforces 706 C. Hard problem #367 div2
- Codeforces 706C Hard problem (DP)
- Codeforces 706C Hard problem (DP)
- CodeForces 706C Hard problem【dp】
- CodeForces 706 C. Hard problem(dp)
- codeforces-367# C. Hard problem
- 706C. Hard problem
- CodeForces 706C-Hard problem(基本的DP)
- 动态规划 Problem C
- 项目实战:TabHost
- 【线段树】HDU 5443 The Water Problem
- 【中国剩余定理】POJ 1006 & HDU 1370 Biorhythms
- RenderScript模糊图片
- 【字典树】【贪心】Codeforces 706D Vasiliy's Multiset
- 【动态规划】Codeforces 706C Hard problem
- 【二分】Codeforces 706B Interesting drink
- 【模拟】Codeforces 706A Beru-taxi
- Linux安装SonarQube
- 【数学】HDU 5753 Permutation Bo
- 【模拟】HDU 5752 Sqrt Bo
- 【模拟】HDU 5762 Teacher Bo
- 【博弈论】HDU 5754 Life Winner Bo
- laravel 学习笔记 I