$SPFA$,优化,$dp$。

写了一个裸的$SPFA$,然后加了一点优化就过了,不过要$300$多$ms$。

$dp$的话跑的就比较快了。

$dp[i]$表示输入$i$个字符的最小花费。

首先$dp[i]=dp[i-1]+x$。

其次,如果$i$是个偶数,那么$dp[i]$还可以从$dp[\frac{i}{2}]$推过来。

如果$i$是奇数,那么$dp[i]$还可以从$dp[\frac{i}{2}]$和$dp[\frac{i}{2} + 1]$推过来。

$SPFA$:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} int n;
LL x,y;
LL a[];
bool f[]; int main()
{
scanf("%d%lld%lld",&n,&x,&y); memset(a,-,sizeof a); a[]=; a[n]=n*x;
queue<int>Q; Q.push(); f[]=; while(!Q.empty())
{
int h=Q.front(); Q.pop(); f[h]=; if(h+<=n)
{
if(a[h+]==-||a[h]+x<a[h+])
{
a[h+]=a[h]+x;
if(f[h+]==) { Q.push(h+); f[h+]=; }
}
} if(h->=)
{
if(a[h-]==-||a[h]+x<a[h-])
{
a[h-]=a[h]+x;
if(f[h-]==) { Q.push(h-); f[h-]=; }
}
} LL cost=min(h*x,y);
if(*h<=n)
{
if(a[*h]==-||a[h]+cost<a[*h])
{
a[*h]=a[h]+cost;
if(f[*h]==) { Q.push(*h); f[*h]=; }
}
} else
{
if(a[n]==-) a[n]=a[h]+y+x*(*h-n);
else a[n]=min(a[n],a[h]+y+x*(*h-n));
}
} printf("%lld\n",a[n]);
return ;
}

$dp$:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<iostream>
using namespace std;
typedef long long LL;
const double pi=acos(-1.0),eps=1e-;
void File()
{
freopen("D:\\in.txt","r",stdin);
freopen("D:\\out.txt","w",stdout);
} int n; LL x,y,dp[]; int main()
{
scanf("%d%lld%lld",&n,&x,&y);
for(int i=;i<=n;i++)
{
dp[i]=dp[i-]+x;
if(i%==) dp[i]=min(dp[i],dp[i/]+y);
else
{
dp[i]=min(dp[i],dp[i/+]+y+x);
dp[i]=min(dp[i],dp[i/]+x+y);
}
}
printf("%lld\n",dp[n]);
return ;
}

CodeForces 710E Generate a String的更多相关文章

  1. 【动态规划】【最短路】Codeforces 710E Generate a String

    题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...

  2. CodeForces 710E Generate a String (DP)

    题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...

  3. codeforces 710E Generate a String(简单dp)

    传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...

  4. CodeForces - 710E Generate a String (dp)

    题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...

  5. codeforces 710E E. Generate a String(dp)

    题目链接: E. Generate a String time limit per test 2 seconds memory limit per test 512 megabytes input s ...

  6. [Educational Codeforces Round 16]E. Generate a String

    [Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...

  7. Educational Codeforces Round 16 E. Generate a String dp

    题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...

  8. Educational Codeforces Round 16 E. Generate a String (DP)

    Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...

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

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

随机推荐

  1. 在 go/golang语言中使用 google Protocol Buffer

    怎么在go语言中实用google protocol Buffer呢? 现在的潮流趋势就是一键搞定,跟ubuntu安装软件一样 go get code.google.com/p/goprotobuf/{ ...

  2. PHP gbk转换成utf8

    /** * GBK ASCII 转换成utf8 */ public function to_utf8($str){ $detect = array('ASCII', 'GBK', 'UTF-8'); ...

  3. [转] Building xnu for OS X 10.10 Yosemite

    Source:http://shantonu.blogspot.jp/2014/10/building-xnu-for-os-x-1010-yosemite.html The OS X kernel ...

  4. macvim打造python IDE

    昨天安装了macvim,今天在上面配置了一下python的ide: 大家也可参考http://blog.dispatched.ch/2009/05/24/vim-as-python-ide/ 1.文法 ...

  5. Matlab内置函数

    [原创]Matlab.NET混编技巧之——找出Matlab内置函数   Matlab与.NET的混合编程,掌握了基本过程,加上一定的开发经验和算法基础,肯定不难.反之,有时候一个小错误,可能抓破脑袋, ...

  6. C Socket初探

    C Socket初探 前段时间写了个C# Socket初探,这次再写个C语言的Socket博文,运行效果如下: 实现步骤: 1. Server端 #include <stdio.h> // ...

  7. struts征程:1.初识struts2

    1.struts2在开发中所必须用到的jar包导入到项目的lib目录下 2.在web.xml中配置一个过滤器,代码格式如下 <filter> <filter-name>stru ...

  8. CSS3高级

    一.学习目标 二.box-sizing属性 语法:box-sizing: content-box|border-box|inherit box-sizing属性的用法: box-sizing属性可以为 ...

  9. java编程思想笔记(一)——面向对象导论

    1.1 抽象过程 1.所有编程语言都提供抽象编程机制. 2.人们所能够解决的问题的复杂性直接取决于抽象的类型(所抽象的是什么)和质量. 3."命令式"语言(basic,c等)都是对 ...

  10. [转]HTTP请求模型和头信息参考

    [转]HTTP请求模型和头信息参考 参考: http://blog.csdn.net/baggio785/archive/2006/04/13/661410.aspx模型: http://blog.c ...