$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. JavaScript插件——标签页

    JavaScript插件——标签页 前言 阅读之前您也可以到Bootstrap3.0入门学习系列导航中进行查看http://www.cnblogs.com/aehyok/p/3404867.html ...

  2. 迟到的 WPF 学习 —— 依赖项属性

    本章学习依赖项属性,英文原文 Dependency Property,它是传统 .Net Framework 属性的扩展,是 WPF 的专属,但所幸使用起来和传统属性几乎一样.WPF 元素所提供的大多 ...

  3. C# 遍历本地网络

    public static bool IsHNetCfgWayConnected(string opName) { NetSharingManagerClass netSharingMgr = new ...

  4. windows下配置PHP+Nginx+MySQL完整流程(转)

    对于在windows上的php+nginx的配置可能好多同学一次根本都配不正确,于我也是如此,为此我将我成功配置的过程细致的总结如下,希望能帮助搞PHP研究的同学 1.资源准备 MySQL:这个链接不 ...

  5. javascript中的promise和deferred:实践(二)

    javascript中的promise和deferred:实践(二) 介绍: 在第一节呢,我花了大量的时间来介绍promises和deferreds的理论.现在呢,我们来看看jquery中的promi ...

  6. Winform常用开发模式第一篇

    Winform常用开发模式第一篇 上一篇博客最后我提到“异步编程模型”(APM),之后本来打算整理一下这方面的材料然后总结一下写篇文章与诸位分享,后来在整理的过程中不断的延伸不断地扩展,发现完全偏离了 ...

  7. 应聘linux/ARM嵌入式开发岗位

    **************************************************************** 因为发在中华英才和智联招聘没有人采我所以我 在这里发布我的个人简历希望 ...

  8. [置顶] 使用Android OpenGL ES 2.0绘图之六:响应触摸事件

    传送门 ☞ 系统架构设计 ☞ 转载请注明 ☞ http://blog.csdn.net/leverage_1229 传送门 ☞ GoF23种设计模式 ☞ 转载请注明 ☞ http://blog.csd ...

  9. gcc与g++的编译链接的示例详解

    一.编译方式的示例详解 1. 编译C代码 代码如下:main.c /*!  ************************************************************** ...

  10. [转]Ubuntu10下MySQL搭建Amoeba系列(文章索引)

    一.前言(Introduction) 使用了Amoeba有一段时间了,发现官方博客:Amoeba使用指南有很多地方都是错误的,在我实战中给到一些错误的指示,所以我想写些在搭建的实战中给大家一点指引.欢 ...