动态规划(线性类)--ones 唯“一”运算
来源:互联网 发布:mac鼠标能点右键吗 编辑:程序博客网 时间:2024/06/08 05:22
【动态规划】ones 唯“一”运算
Time Limit:300000MS Memory Limit:65536K
Description
给定n, 计算只用1, *, +, (, ) 表示n所需要的最少的1的个数
例子:
3 = 1 + 1 + 1
6 = (1 + 1 + 1) * (1 + 1)
20 = (1 + 1 + 1 + 1 + 1) * (1 + 1) * (1 + 1)
Input
输入为一个数n
Output
输出所需要的最少的1的个数
Sample Input
20
Sample Output
9
看完这个时间限制,我也是醉了,立马想到了暴力枚举
代码交上去才发现自己太天真,超时了
没办法,只能动态规划了
var n,i,j,k:longint; opt:array[0..1000001]of integer;begin readln(n); for i:=1 to n do opt[i]:=100; opt[0]:=0; opt[1]:=1; for i:=2 to n do begin for j:=1 to trunc(sqrt(i)) do begin if opt[j]+opt[i-j]<opt[i] then opt[i]:=opt[j]+opt[i-j];end; for k:=2 to trunc(sqrt(i)) do begin if opt[k]+opt[i div k]+opt[i mod k]<opt[i]then opt[i]:=opt[k]+opt[i div k]+opt[i mod k];end; end; writeln(opt[n]);end.
0 0
- 动态规划(线性类)--ones 唯“一”运算
- 动态规划基础学习(一)线性
- 动态规划----线性动态规划
- fzoj 1320 Ones(动态规划:水题)
- [动态规划]线性(一维/串、环)模型
- 【LeetCode】Jump Game (一维动态规划 + 线性扫描)
- 动态规划入门 & 线性动态规划
- 线性动态规划-文件排版
- 线性动态规划 (共六题)
- LeetCode 474. Ones and Zeroes 动态规划解法+拓展
- leetcode 474. Ones and Zeroes 动态规划DP + 背包问题
- 算法:大数运算,动态规划
- 动态规划一例
- 动态规划学习一
- 算法---->动态规划(一)
- 动态规划(一)
- 动态规划一
- 动态规划(一)
- poj 2446 二分图最大匹配 匈牙利算法
- Solution to CLRS Chapter 4
- C++ Primer:第八章:IO库(续)
- 第十一周项目一 是春哥啊
- 四十七、多态性:运算符重载为类的友元函数
- 动态规划(线性类)--ones 唯“一”运算
- 每天拿出来2小时浪费(文/王路) 作者: 王路
- uva 11991 Easy Problem from Rujia Liu? Data Structure
- 答微信产品经理假面试题
- Java遇见的一些概念问题
- app icon 需要哪些尺寸的图片
- Python报错:ProgrammingError: You must not use 8-bit bytestrings unless you use a text_factory that can
- BestCoder Round #41 A B C
- HTML5学堂 全新的HTML5/前端技术分享平台