2.Dynamic Programming on Stolen Values【dp】
#include <iostream>
#include <string.h>
using namespace std; int dp[100]; int main()
{
memset(dp,0,sizeof(dp));
const int len=6;
int a[len]={6,1,33,7,11,13}; for(int i=0;i<len;i++)
{
if(i<2)
{
if(i==0)
dp[i]=a[i];
else if(i==1)
dp[i]=a[i]>a[i-1]?a[i]:a[i-1];
}
else
{
dp[i]=dp[i-2]+a[i]>dp[i-1]?dp[i-2]+a[i]:dp[i-1];
} }
cout<<dp[len-1]<<endl;
return 0;
}
f(
i) is defined to denote the maximal stolen value from the first house to the
ithhouse, and the value contained in the
ith house is denoted as
vi. When the thief reaches the
ithhouse, he has two choices: to steal or not. Therefore,
f(
i) can be defined with the following equation:
n is needed, but actually it is only necessary to cache two values for
f(
i-1) and
f(
i-2) to calculate
f(
i).
const vector<
int>& values)
int length = values.size();
if(length == 0)
return 0;
int value1 = values[0];
if(length == 1)
return value1;
int value2 = max<
int>(values[0], values[1]);
if(length == 2)
return value2;
int value;
for(
int i = 2; i < length; ++i)
int>(value2, value1 + values[i]);
return value;
Amazon.com, or
Apress.
The author Harry He owns all the rights of this post. If you are going to use part of or the whole of this ariticle in your blog or webpages, please add a reference to http://codercareer.blogspot.com/. If you are going to use it in your books, please contact him via zhedahht@gmail.com . Thanks.
2.Dynamic Programming on Stolen Values【dp】的更多相关文章
- HDOJ 1159 Common Subsequence【DP】
HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- Kattis - honey【DP】
Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1501 Zipper 【DP】【DFS+剪枝】
HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...
- HDOJ 1257 最少拦截系统 【DP】
HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】
HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】
POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...
- HackerRank - common-child【DP】
HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...
- LeetCode:零钱兑换【322】【DP】
LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...
随机推荐
- ARM 寄存器
ARM总共有37个寄存器 ARM寄存器物理分类 通用寄存器:1:不分组寄存器(R0--R7) 2:分组寄存器(R8-R14) 3:程序计数器(R15)(注意:又名pc指针) 程序状态寄存器:1:CPS ...
- 概率论中常见分布总结以及python的scipy库使用:两点分布、二项分布、几何分布、泊松分布、均匀分布、指数分布、正态分布
概率分布有两种类型:离散(discrete)概率分布和连续(continuous)概率分布. 离散概率分布也称为概率质量函数(probability mass function).离散概率分布的例子有 ...
- 【BZOJ 1419】1419: Red is good (概率DP)
1419: Red is good Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 807 Solved: 343 Description 桌面上有R张 ...
- Codeforces.724G.Xor-matic Number of the Graph(线性基)
题目链接 \(Description\) 给定一张带边权无向图.若存在u->v的一条路径使得经过边的边权异或和为s(边权计算多次),则称(u,v,s)为interesting triple(注意 ...
- PHP中CGI,FastCGI,PHP-CGI与PHP-FPM对比
CGI CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一 ...
- [HDU6203]ping ping ping
题目大意: 给你一棵树,其中有一些点是坏掉的.告诉你k个点对表示这两个点的路径上至少有1个点是坏掉的.问整棵树上至少有多少点是坏的. 思路: 贪心. 找出每组点对的LCA,对所有点对按照LCA的深度排 ...
- Java NIo 笔记001
1. Channel Channel接口只提供了两个方法: package java.nio.channels; public interface Channel { public boolean i ...
- [Visual Studio] VS2012调试时很慢的解决方案
1.转自http://guooge.com/archives/408.html VS2010调试极慢获取出现死机,因为启动了IntelliTrace Visual Studio 2010 Ulti ...
- redis学习之一 - linux下安装配置
Content 0.序 1.如何安装? 2.配置参数及其意义 3.设为linux服务 0.序 本文主要是记录Redis在 Centos下的安装配置 .文中如无特别说明.表示redis-3.2.10代码 ...
- UEFI引导模式
Author: JinDate: 20140827System: windows 刚帮楼下的公司解决了个问题. 原来的办公电脑,预装linux,他们重装成win7.新买的电脑预装成win8,安装出问题 ...