【Gym 100733D】Little thief Shi
Shi realized that he was almost out of money, even renting Shitalian lands. Shi was walking on a street, while thinking of a way to recover his fortune. In his way, he passed by a jewelry store. The owner of the store was a Shitalian man suspected of committing minor crimes, as cutting bushes and stealing old bread. Shi hated people who cut bushes, so he decided to rob the store to take revenge and make some money.
The store has n jewels, put in a row, one after another. Each jewel i can be sold in the black market for a value vi. Shi wants to steal as much as possible, but if he steals everything, the owner will notice, so if Shi steals the i-th jewel, he can't steal the i - 1-th, i - 2-th, i + 1-th and i + 2-th jewels.
Using the best strategy possible, calculate the sum of the jewels values that Shi can obtain.
The input begins with an integer n (1 ≤ n ≤ 106), indicating the number of jewels in the store. The next line containsn integers. The i-th integer vi (1 ≤ vi ≤ 103) is the value of the i-th jewel.
Output the maximum value that Shi can get.
4
1 2 3 4
5
6
1 2 4 0 3 0
5
7
2 10 12 24 29 69 0
81
10
15 1 6 3 7 100 9 15 80 95
210
题意:n个数字,每次最少隔两个取一个,求取得数的最大和
分析:dp,想法一、s[i]表示取第i个时最大和为多少,那就取不了i-1、i-2,可以取i-3、i-4、i-5,.....,当取i-6时,可取i-3,显然取了更划算,同理,i-7、i-8在算s[i-4]、s[i-5]时考虑过了,s[i]=a[i]+max{s[i-3],s[i-4],s[i-5]}
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[],tem,ans=;
int main(){
scanf("%d",&n);
for(int i=;i<n+;i++)
{
scanf("%d",&tem);
s[i]=max(s[i-],max(s[i-],s[i-]))+tem;
ans=max(ans,s[i]);
}
printf("%d",ans);
return ;
}
想法二、也是dp,s[i]表示前i个的最大和为多少,s[i]=max{s[i-1],s[i-3]+第i个}
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[],tem;
int main(){
scanf("%d",&n);
for(int i=;i<n+;i++)
scanf("%d",&tem),s[i]=max(s[i-],tem+s[i-]);
printf("%d",s[n+]);
return ;
}
【Gym 100733D】Little thief Shi的更多相关文章
- 【Gym 100733D】Little thief Shi(取数,DP)
题 Shi realized that he was almost out of money, even renting Shitalian lands. Shi was walking on a s ...
- 【 Gym 101116K 】Mixing Bowls(dfs)
BUPT2017 wintertraining(15) #4H Gym - 101116K 题意 给定一个菜谱,大写的单词代表混合物,小写的代表基础原料.每个混合物由其它混合物或基础原料组成,不会间接 ...
- 【 Gym - 101124E 】Dance Party (数学)
BUPT2017 wintertraining(15) #4G Gym - 101124 E.Dance Party 题意 有c种颜色,每个颜色最多分配给两个人,有M个男士,F个女士,求至少一对男士同 ...
- 【Gym - 101124A】The Baguette Master (数学,几何)
BUPT2017 wintertraining(15) #4F Gym - 101124A 题意 给定画框宽度,画的四边和一个对角线长度,求画框外沿周长. 题解 过顶点做画框的垂线,每个角都得到两个全 ...
- 【 Gym - 101138K 】 The World of Trains (DP)
BUPT2017 wintertraining(15) #4E Gym - 101138K 题意 N节车厢的火车,每节车厢容量是1~K,那么有\(K^N\)种火车. 求选择D个连续的且容量相同的车厢的 ...
- 【 Gym - 101138J 】Valentina and the Gift Tree(树链剖分)
BUPT2017 wintertraining(15) 4 D Gym - 101138J 数据 题意 n个节点的一棵树,每个节点的权值为g,q个询问,树上的节点U-V,求U到V的路径的最大子段和. ...
- 【 Gym - 101138F 】GukiZ Height (数学)
BUPT2017 wintertraining(15) #4 C Gym - 101138F 题意 初始高度0,目标值h,第i天目标值会下降i,当前高度会改变a[i%n],求高度不小于目标值的最早的时 ...
- 【 Gym - 101138D 】Strange Queries (莫队算法)
BUPT2017 wintertraining(15) #4B Gym - 101138D 题意 a数组大小为n.(1 ≤ n ≤ 50 000) (1 ≤ q ≤ 50 000)(1 ≤ ai ≤ ...
- 【Gym - 101164I】Cubes(dfs,剪枝)
BUPT2017 wintertraining(15) #4 A - I.Cubes Gym - 101164I 题意 将n拆成最少个立方数相加的形式. 题解 根据n的范围,立方数最大不超过400的立 ...
随机推荐
- codeforces 713A A. Sonya and Queries(状态压缩)
题目链接: A. Sonya and Queries time limit per test 1 second memory limit per test 256 megabytes input st ...
- Win8/8.1 .NET3.5安装失败
.NetFramework 作为开发人员,很多情况都需要.NetFramework,在Win7及之前的系统上直接双击 .NetFramework的安装包就可安装了. Win8/8.1无法安装.Net3 ...
- Unity3D多人协作开发环境搭建
多人协作 说到多人协作开发,大家都会想到要使用版本控制工具来管理项目,当然最常用的要数SVN和Git了,但是SVN管理Unity3D项目的确有一些不尽人意的地方. 比如:两个人修改了同一个场景,SVN ...
- Android 可拖拽的GridView效果实现, 长按可拖拽和item实时交换
转帖请注明本文出自xiaanming的博客(http://blog.csdn.net/xiaanming/article/details/17718579),请尊重他人的辛勤劳动成果,谢谢! 在And ...
- 事故记录-过多进程致使CPU卡死
早上到公司,发现邮箱内有报警信息显示CPU和IO使用都已超标.报警内容如下:主机: test-server-192.168.1.18 时间: 2015.11.15 15:25:17状态: PROBLE ...
- 20Mybatis_订单商品数据模型_一对一查询——resultType和resultMap两种方式以及两种方式的总结
上一篇文章分析了数据模型,这篇文章就给出一个需求,这个需求是一对一查询,并完成这个需求. ------------------------------------------------------- ...
- mysql实体关系
数据库连接细节架构: 数据库编码: 关系数据库玩的是生活中物与物之间的关系.那么数据库要保存数据,事先就要描述情况数据如何保存.事务之间的关系如何处理等等. 现实生活中物称为实体,如商品,学生,文章, ...
- C语言 文件操作12--文件加密
//文件加密解密 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include ...
- “插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题
“插件(application/x-vlc-plugin)不受支持”NPAPI和PPAPI的问题 最近做一个前端的项目,项目需要引用VLC浏览器插件,javascript在IE.Firefox等浏览器 ...
- matlab中textread
今天打算跑下程序,突然发现,真的很烂,不会读入数据,简单的Iris.txt一上午都没读进去,在此对matlab中的textread函数做下总结,textscan函数待续. 本文主要内容引自http:/ ...