【CodeForce 710E】Generate a String

Description

抽象题意:给出n(目标字符串有n个‘a’),x(增加或删除一个‘a’需要多少秒),y(复制并粘贴当前的字符串需要多少秒)
求用最少的时间,生成n个‘a’;

zscoder wants to generate an input file for some programming competition problem.

His input is a string consisting of n letters ‘a’. He is too lazy to write a generator so he will manually generate the input in a text editor.

Initially, the text editor is empty. It takes him x seconds to insert or delete a letter ‘a’ from the text file and y seconds to copy the contents of the entire text file, and duplicate it.

zscoder wants to find the minimum amount of time needed for him to create the input file of exactly n letters ‘a’. Help him to determine the amount of time needed to generate the input.

Solution

可以往DP上想,设 fi 表示当前做到了i个,
很显然有:

fi=min(fi1+x,fj(j<i2j>=i)+y+(2ji)x)

(注意: 题目可没说长度不可以超过n
后面的可以用单调队列来维护。

复杂度: O(n)

Code

#include<cstdio>
#include<cstring>
#define fo(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
typedef long long LL;
const int N=1e7+5;
LL ans,x,y;
LL f[N*2];
int d[N],S,n;
LL min(LL q,LL w){return q<w?q:w;}
int main()
{
    int q,w;
    scanf("%d%I64d%I64d",&n,&x,&y);
    memset(f,127,sizeof(f));
    f[1]=x;S=1;f[2]=x+y;
    q=n;n*=2;
    fo(i,2,n)
    {
        while(S<=d[0]&&d[S]<=i)S++;
        f[i]=min(f[i],f[i-1]+x);
        if(S<=d[0])f[i]=min(f[i],f[d[S]]+x*(d[S]-i));
        if(i*2<=n)
        {
            f[i*2]=min(f[i*2],f[i]+y);
            while(S<=d[0]&&f[d[d[0]]]+x*(d[d[0]]-i)>=f[i*2]+x*i)d[0]--;
            d[++d[0]]=i*2;
        }
    }
    printf("%I64d\n", f[q]);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值