zoukankan      html  css  js  c++  java
  • CF Round #367 C题

    题目 链接:http://codeforces.com/contest/706/problem/C

    好像又是DP...

    dp[i][0]表示第i个字符串不翻转成字典序排列的花费,dp[i][1]表示第i个字符串翻转成字典序排列的花费;

    #include<bits/stdc++.h>
    using namespace std;
    #define ll __int64
    #define mod 1000000007
    #define pi (4*atan(1.0))
    const int N=1e5+10,M=2e6+10,inf=1e9+10;
    const ll INF=1e17;
    ll dp[N][3];
    string str1[N];
    string str2[N];
    ll a[N];
    void init()
    {
        for(int i=1;i<N;i++)
        dp[i][0]=dp[i][1]=INF;
    }
    int main()
    {
        int x,y,z,i,t;
        init();
        scanf("%d",&x);
        for(i=1;i<=x;i++)
        scanf("%I64d",&a[i]);
        for(i=1;i<=x;i++)
        {
            cin>>str1[i];
            str2[i]=str1[i];
            reverse(str2[i].begin(),str2[i].end());
        }
        dp[1][0]=0;
        dp[1][1]=a[1];
        for(i=2;i<=x;i++)
        {
            if(str1[i]>=str1[i-1])
            dp[i][0]=min(dp[i][0],dp[i-1][0]);
            if(str1[i]>=str2[i-1])
            dp[i][0]=min(dp[i][0],dp[i-1][1]);
            if(str2[i]>=str1[i-1])
            dp[i][1]=min(dp[i][1],dp[i-1][0]+a[i]);
            if(str2[i]>=str2[i-1])
            dp[i][1]=min(dp[i][1],dp[i-1][1]+a[i]);
        }
        ll ans=min(dp[x][0],dp[x][1]);
        if(ans<INF)
        printf("%I64d
    ",ans);
        else
        printf("-1
    ");
        return 0;
    }
  • 相关阅读:
    MySQL视图
    MySQL触发器
    SQL语法详解
    MySQL函数和操作符
    MySQL常用查询
    MySQL数据类型
    MySQL操作详解
    MySQL学习-SQL约束
    MySQL 其它基本操作
    MySQL创建数据库并插入数据
  • 原文地址:https://www.cnblogs.com/Lionel002/p/5768372.html
Copyright © 2011-2022 走看看