前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【Gym 100733D】Little thief Shi(取数,DP)

【Gym 100733D】Little thief Shi(取数,DP)

作者头像
饶文津
发布2020-05-31 23:38:08
4060
发布2020-05-31 23:38:08
举报
文章被收录于专栏:饶文津的专栏饶文津的专栏

 题

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.

Input

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

Output the maximum value that Shi can get.

Sample test(s)

input

代码语言:javascript
复制
4
1 2 3 4

output

代码语言:javascript
复制
5

input

代码语言:javascript
复制
6
1 2 4 0 3 0

output

代码语言:javascript
复制
5

input

代码语言:javascript
复制
7
2 10 12 24 29 69 0

output

代码语言:javascript
复制
81

input

代码语言:javascript
复制
10
15 1 6 3 7 100 9 15 80 95

output

代码语言:javascript
复制
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]}

代码语言:javascript
复制
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[1000025],tem,ans=0;
int main(){
    scanf("%d",&n);
    for(int i=5;i<n+5;i++)
    {
         scanf("%d",&tem);
         s[i]=max(s[i-3],max(s[i-4],s[i-5]))+tem;
         ans=max(ans,s[i]);
    }
    printf("%d",ans);
    return 0;
}

想法二、也是dp,s[i]表示前i个的最大和为多少,s[i]=max{s[i-1],s[i-3]+第i个}

代码语言:javascript
复制
#include<stdio.h>
#include<algorithm>
using namespace std;
int n,s[1000005],tem;
int main(){
    scanf("%d",&n);
    for(int i=3;i<n+3;i++)
        scanf("%d",&tem),s[i]=max(s[i-1],tem+s[i-3]);
    printf("%d",s[n+2]);
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2016-02-05 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档