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个,
很显然有:
(注意:题目可没说长度不可以超过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;
}
本文介绍了一个编程竞赛问题的解决方案:如何使用最少的时间手动在文本编辑器中生成由n个字母'a'组成的字符串。通过动态规划的方法,计算出完成任务所需的最短时间。
350

被折叠的 条评论
为什么被折叠?



