CodeForces 710E Generate a String
$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的更多相关文章
- 【动态规划】【最短路】Codeforces 710E Generate a String
题目链接: http://codeforces.com/problemset/problem/710/E 题目大意: 问写N个字符的最小花费,写一个字符或者删除一个字符花费A,将当前的字符数量翻倍花费 ...
- CodeForces 710E Generate a String (DP)
题意:给定 n,x,y,表示你要建立一个长度为 n的字符串,如果你加一个字符要花费 x时间,如果你复制前面的字符要花费y时间,问你最小时间. 析:这个题,很明显的DP,dp[i]表示长度为 i 的字符 ...
- codeforces 710E Generate a String(简单dp)
传送门:http://codeforces.com/problemset/problem/710/E 分析: 让你写一个全由"a"组成的长为n的串,告诉你两种操作,第一种:插入一个 ...
- CodeForces - 710E Generate a String (dp)
题意:构造一个由a组成的串,如果插入或删除一个a,花费时间x,如果使当前串长度加倍,花费时间y,问要构造一个长度为n的串,最少花费多长时间. 分析:dp[i]---构造长度为i的串需要花费的最短时间. ...
- 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 ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- 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 ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Codeforces #541 (Div2) - E. String Multiplication(动态规划)
Problem Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...
随机推荐
- [转]How to build a data storage and VM Server using comodity hardware and free software
Source: http://learnandremember.blogspot.jp/2010_01_01_archive.html Requisites: 1) RAID protection f ...
- ASP.NET MVC学习笔记-----Filter2
ASP.NET MVC学习笔记-----Filter(2) 接上篇ASP.NET MVC学习笔记-----Filter(1) Action Filter Action Filter可以基于任何目的使用 ...
- 物理数据模型(PDM)->概念数据模型 (CDM)->面向对象模型 (OOM):适用于已经设计好数据库表结构了。
物理数据模型(PDM)->概念数据模型 (CDM)->面向对象模型 (OOM):适用于已经设计好数据库表结构了. 步骤如下: 一.反向生成物理数据模型PDM 开发环境 PowerDes ...
- Hadoop Streaming框架学习(一)
Hadoop Streaming框架学习(一) Hadoop Streaming框架学习(一) 2013-08-19 12:32 by ATP_, 473 阅读, 3 评论, 收藏, 编辑 1.Had ...
- BST&AVL&红黑树简单介绍
(BST&AVL&红黑树简单介绍) 前言: 节主要是给出BST,AVL和红黑树的C++代码,方便自己以后的查阅,其代码依旧是data structures and algorithm ...
- C#基础之方法和参数
C#基础之方法和参数 接上一篇<C#基础之类型和成员基础以及常量.字段.属性> 实例方法.静态方法 C#中的方法分为两类,一种是属于对象(类型的实例)的,称之为实例方法,另一种是属于类型的 ...
- FormData可实现异步传输二进制文件(即异步文件上传)
XMLHttpRequest Level 2 添加了一个新的接口——FormData.它能使现在的AJAX交互更加简单.之前的AJAX在上传前,需要将表单的数据序列化.与普通的 Ajax 相比,使用 ...
- 解决Eclipse一直loading workbench无法启动的问题
不知怎地,Eclipse始终停留在Loading workbench状态.反复重启,状态依旧.尝试解决. 停止时显示的状态:”Loading workbench”,看来和这个workbench插件有关 ...
- JS中转义字符的处理
//去掉html标签 1 2 3 function removeHtmlTab(tab) { return tab.replace(/<[^<>]+?>/g,'');//删除 ...
- dom4解析xml格式文件实例
以下给4种常见的xml文件的解析方式的分析对比: DOM DOM4J JDOM SAX 解析XML文件的几种方式和区别答: Dom解析 在内存中创建一个DOM树,该结构通常需要加载整个文档然后才能做工 ...