题目链接:

http://codeforces.com/problemset/problem/710/E

E. Generate a String

time limit per test 2 seconds
memory limit per test 512 megabytes
#### 问题描述
> 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.
#### 输入
> The only line contains three integers n, x and y (1 ≤ n ≤ 107, 1 ≤ x, y ≤ 109) — the number of letters 'a' in the input file and the parameters from the problem statement.
#### 输出
> Print the only integer t — the minimum amount of time needed to generate the input file.
#### 样例
> **sample input**
> 8 1 1
>
> **sample output**
> 4

题意

加减1的消耗是x,乘2的消耗是y,问通过这三个操作把0变成n的最小消耗

题解

贪心+dp

能证明的是偶数都不可能由后面的减回来的,所以即使是奇数也只能由后面的那个偶数减回来,而那个偶数就不用考虑后面再减了。

所以有转移方程:

i是偶数: dp[i]=min(dp[i/2]+y,dp[i-1]+x)

i是奇数: dp[i]=min(dp[i/2+1]+x+y,dp[i-1]+x)

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=1e7+10; LL dp[maxn];
int n,x,y; int main() {
scf("%d%d%d",&n,&x,&y);
dp[1]=x;
for(int i=2;i<=n;i++){
if(i&1){
dp[i]=min(dp[i-1]+x,dp[i/2+1]+y+x);
}else{
dp[i]=min(dp[i/2]+y,dp[i-1]+x);
}
}
prf("%I64d\n",dp[n]);
return 0;
} //end-----------------------------------------------------------------------

划分阶段,然后先处理乘2,再处理乘加,再处理乘减。(但是感觉还是在偶数不可能通过更大的数减回来的这个结论的基础上做的,可能没有理解清楚吧。。)

阶段1:1~2(dp[1]=x,dp[2]=min(x,y))

阶段2:2~4

...

阶段m:n~n*2

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef __int64 LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=2e7+10; LL dp[maxn];
int n,x,y; int main() {
clr(dp,0x3f);
scf("%d%d%d",&n,&x,&y);
dp[1]=x;
dp[2]=min(x+dp[1],y+dp[1]);
for(int cur=2;cur<=n;cur<<=1){
for(int i=cur;i<=cur*2;i++){
if(i%2==0) dp[i]=min(dp[i],dp[i/2]+y);
dp[i]=min(dp[i],dp[i-1]+x);
}
for(int i=cur*2;i>=cur;i--){
dp[i]=min(dp[i],dp[i+1]+x);
}
}
prf("%I64d\n",dp[n]);
return 0;
} //end-----------------------------------------------------------------------

Educational Codeforces Round 16 E. Generate a String dp的更多相关文章

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

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

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

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

  3. [Educational Codeforces Round 16]D. Two Arithmetic Progressions

    [Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...

  4. [Educational Codeforces Round 16]C. Magic Odd Square

    [Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...

  5. [Educational Codeforces Round 16]B. Optimal Point on a Line

    [Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...

  6. [Educational Codeforces Round 16]A. King Moves

    [Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...

  7. Educational Codeforces Round 53 E. Segment Sum(数位DP)

    Educational Codeforces Round 53 E. Segment Sum 题意: 问[L,R]区间内有多少个数满足:其由不超过k种数字构成. 思路: 数位DP裸题,也比较好想.由于 ...

  8. Educational Codeforces Round 9 C. The Smallest String Concatenation 排序

    C. The Smallest String Concatenation 题目连接: http://www.codeforces.com/contest/632/problem/C Descripti ...

  9. Educational Codeforces Round 8 C. Bear and String Distance 贪心

    C. Bear and String Distance 题目连接: http://www.codeforces.com/contest/628/problem/C Description Limak ...

随机推荐

  1. less使用

    Less在浏览器上使用的方法 <link rel="stylesheet" type="text/less" href="styles.less ...

  2. 微信小程序实现转义换行符

    在html中可以直接使用<br />换行,但是小程序wxml中使用<br />无效,可以换成\n Page({ data: { title: '至少5个字\n请多说些感受吧' ...

  3. MongoDB4.0在windows10下的安装与服务配置

    本地安装及网页测试 在官网下载最新的安装文件 下载地址 : https://www.mongodb.com/download-center#community 可以在MongoDB官网选择Commun ...

  4. 如何从SAP ECC中抽取簇表数据

    打开SAP 客户端工具 ABAP 中 创建包(SE80) 创建函数组 展开ABAP 工作台,双击ABAP Dictionary 字典: 选择第三个data type,输入数据结构名称ZSQL_CLAU ...

  5. 海思平台交叉编译curl支持SSL功能

    1.准备工具 1).交叉编译工具 2).下载libcurl和openssl源代码,我使用的是(openssl-1.0.2o.tar,curl-7.59.0.tar) 3).查看cpu详细 ~ # ca ...

  6. sqli-labs (less-8-less-10)

    盲注需要掌握一些MySQL的相关函数:length(str):返回str字符串的长度.substr(str, pos, len):将str从pos位置开始截取len长度的字符进行返回.注意这里的pos ...

  7. go 网络请求篇二

    框架地址:https://github.com/parnurzeal/gorequest package main //https://antarx.com/2018/05/05/gorequest- ...

  8. Gblocks命令行

    使用默认的设置: $ Gblocks proteins.fasta -t=p 必须是 fasta 文件在前,参数在后.若没有参数,则进入交互式界面. Gblocks cds.fasta −t=c −b ...

  9. Centos7 安装 Python 的笔记

    Centos7 安装 Python 的笔记 注意:系统自带的Python2.7不要改动,最好也不要出错,不然yum之类的工具可能会出错. 安装Python3.7.0 TensorFlow对Python ...

  10. uni-app 下拉至指定高度固定view

    uni.createSelectorQuery().select(‘#salyt’).boundingClientRect(function(rects){ console.log(rects) va ...