(DP)codeforces - 710E Generate a String
原题链接:http://www.codeforces.com/problemset/problem/710/E
题意:一个字符串,开始长度为0,目标长度为n,长度+1或-1需要的时间为x,长度*2需要的时间为y,求0到m需要的最少时间。
分析:这题一上来直接写优先队列bfs,然后很愉快的超内存的了。就想别的方法,想了一会没想清晰,感觉可以用记忆化搜索,就往这上面一想,才发现直接dp就行了。
可以很容易发现,奇数肯定是+1或者通过*2逼近并且+1或-1得到。
而偶数只能在+1和翻倍得到。
所以在奇数情况下的状态转移方程为dp[i]=min(dp[i-1]+x,dp[i/2]+x+y,dp[i/2+1]+x+y)
偶数情况下的为dp[i]=min(dp[i-1]+x,dp[i/2]+y)
代码:
#include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<cstring> #include<set> #include<vector> #include<queue> #include<map> #include<list> #include<bitset> #include<string> #include<cctype> #include<cstdlib>using namespace std;typedef long long ll; typedef unsigned long long ull; const int inf = 1 << 30; const ll lnf = 1ll << 60;//-----upstair is template------// const int maxn=1e7; ll dp[maxn];void solve() {int n,x,y;scanf("%d%d%d",&n,&x,&y);fill(dp,dp+maxn,lnf);dp[0]=0;for(int i=1;i<=n;i++){if(i&1){dp[i]=min(dp[i-1]+x,min(dp[i/2+1]+x+y,dp[i/2]+x+y));}else{dp[i]=min(dp[i-1]+x,dp[i/2]+y);}}printf("%I64d\n",dp[n]); }int main() {#ifndef ONLINE_JUDGE//freopen("in.txt", "r", stdin);//freopen("out.txt", "w", stdout); #endif//iostream::sync_with_stdio(false); solve();return 0; }
转载于:https://www.cnblogs.com/tak-fate/p/5811471.html
(DP)codeforces - 710E Generate a String相关推荐
- 求三角形最大面积(DP)
求三角形最大面积(DP) 在OJ上奇迹般WA了:WA:70. Why? #include <iostream> #include <string.h> using namesp ...
- LeetCode 编辑距离 II(DP)
1. 题目 给你两个单词 s 和 t,请你计算出将 s 转换成 t 所使用的最少操作数. 你可以对一个单词进行如下两种操作: 删除一个字符 替换一个字符 注意: 不允许插入操作 题目保证有解 示例: ...
- LeetCode 1406. 石子游戏 III(DP)
1. 题目 Alice 和 Bob 用几堆石子在做游戏.几堆石子排成一行,每堆石子都对应一个得分,由数组 stoneValue 给出. Alice 和 Bob 轮流取石子,Alice 总是先开始.在每 ...
- LeetCode 72. 编辑距离(DP)
1. 题目 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换一个字符 示 ...
- 2019年杭电多校第一场 1001题blank(DP)HDU6578
2019年杭电多校第一场 1001题blank(DP)HDU6578 解决思路,开一个DP数组来存储0 1 2 3四个字符最后出现的位置,并且在DP中已经==排好序==. DP开四维,DP[i][j] ...
- HDU6578 2019HDU多校训练赛第一场 1001 (dp)
HDU6578 2019HDU多校训练赛第一场 1001 (dp) 传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6578 题意: 你有n个空需要去填,有 ...
- 0-1背包问题(DP)-超有趣版
0-1背包问题(DP)-超有趣版 文章目录 0-1背包问题(DP)-超有趣版 一. 0-1背包问题 二. 0-1背包问题分析 三.0-1背包问题--DP算法求解 四.0-1背包问题--DP算法深入分析 ...
- 【CF 149D】Coloring Brackets(dp)
[CF 149D]Coloring Brackets(dp) D. Coloring Brackets time limit per test 2 seconds memory limit per t ...
- 辉姐姐与复读机(dp)
辉姐姐与复读机 Time Limit: 1000 ms Memory Limit: 65536 KiB Problem Description 一天,辉姐姐在 SDUT2018ACM 群里发了一句& ...
最新文章
- LSMW、BDC、CATT 区别
- 【python 6】Numpy
- gym 102875 H. Happy Morse Code
- matlab imfinfo返回图像信息
- Java基础---数组练习(最大值、最小值的索引)
- js:变量,作用域以及内存问题
- win7系统下装ubuntu系统
- IAR 软件激活步骤
- harmonyOS应用开发环境搭建
- 水下声学的基本声学量介绍
- 十分钟入门Matplotlib
- Halcon之图像采集II
- win7连接网络打印机需要输入网络密码,怎么办
- 第三次作业:卷积神经网络基础
- python—networkx:画随机几何图,找出中心节点并按路径长度染色
- 华硕服务器怎么装系统教程视频,华硕怎么安装linux系统教程视频
- C++ string CString 详解
- pytorch 训练时候不动了
- Crayon:新研究指出让IT决策者夜不能寐的原因
- typec扩展坞hdmi没反应_手机\笔记本使用Type-c扩展坞的问题解析