Problem Statement

    

The old song declares "Go ahead and hate your neighbor", and the residents of Onetinville have taken those words to heart. Every resident hates his next-door neighbors on both sides. Nobody is willing to live farther away from the town's well than his neighbors, so the town has been arranged in a big circle around the well. Unfortunately, the town's well is in disrepair and needs to be restored. You have been hired to collect donations for the Save Our Well fund.

Each of the town's residents is willing to donate a certain amount, as specified in the int[] donations, which is listed in clockwise order around the well. However, nobody is willing to contribute to a fund to which his neighbor has also contributed. Next-door neighbors are always listed consecutively indonations, except that the first and last entries in donations are also for next-door neighbors. You must calculate and return the maximum amount of donations that can be collected.

 

Definition

    
Class: BadNeighbors
Method: maxDonations
Parameters: int[]
Returns: int
Method signature: int maxDonations(int[] donations)
(be sure your method is public)
    
 
 

Constraints

- donations contains between 2 and 40 elements, inclusive.
- Each element in donations is between 1 and 1000, inclusive.
 

Examples

0)  
    
 { 10, 3, 2, 5, 7, 8 }
Returns: 19
The maximum donation is 19, achieved by 10+2+7. It would be better to take 10+5+8 except that the 10 and 8 donations are from neighbors.
1)  
    
{ 11, 15 }
Returns: 15
 
2)  
    
{ 7, 7, 7, 7, 7, 7, 7 }
Returns: 21

题目中要求是 一个环中的某些数的和最大,这些数要满足的条件是,不能是相邻的,又因为是一个环,所以首尾两个数也认为是相邻的。这样的话,不妨认为测试数据给出的数组下标是从0~i,第一次先处理A[0]~A[i-1],再处理一次A[1]~A[i],各得出一个最大的,两者较大的即是要求的。处理过程本可以写成一个函数,但是写完懒得改了,就这样吧。

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <list>
#include <cctype>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define REP(i,j,k) for(int i = j ; i < k ; ++i)
#define MAXV (1000)
#define INF (0x6FFFFFFF)
using namespace std;
class BadNeighbors
{
public:
int maxDonations(vector <int> donations)
{
int ans=;
int dp[];
bool flag;
memset(dp,,sizeof dp);
if(donations.size()==) return ;
if(donations.size()==) return donations[];
if(donations.size()==) return max(donations[],donations[]);
REP(i,,donations.size())
{
flag=true;
dp[i]=donations[i];
REP(j,,i-)
{
ans=max(dp[j]+donations[i],ans);
flag=false;
}
if(!flag)
dp[i]=ans;
}
int ret=;
memset(dp,,sizeof dp);
dp[]=donations[];
REP(i,,donations.size()-)
{
dp[i]=donations[i];
flag=true;
REP(j,,i-)
{
ret=max(dp[j]+donations[i],ret);
flag=false;
}
if(!flag)
dp[i]=ret;
}
return max(ans,ret);
}
};
int main()
{
//freopen("in.txt","r",stdin);
int _x[]= { , };
vector<int> x(_x,_x+sizeof(_x)/sizeof(_x[]));
BadNeighbors b;
printf("%d\n",b.maxDonations(x));
return ;
}

动态规划初级练习(二):BadNeighbors的更多相关文章

  1. 动态规划初级 入门理解 C#代码

      using System; using System.Collections.Generic; using System.Linq; using System.Text; using Micros ...

  2. 偏前端-vue.js学习之路初级(二)组件化构建

    vue.js   组件化构建 组件系统是 Vue 的另一个重要概念,因为它是一种抽象,允许我们使用小型.自包含和通常可复用的组件构建大型应用.仔细想想,几乎任意类型的应用界面都可以抽象为一个组件树: ...

  3. python面试题--初级(二)

    基础不牢,地动山摇,面试的时候经常会被问到一些平时基础的很容易被忽视的知识点,所以重在积累,多看多背深入理解,才能在某一天工作中豁然开朗恍然大悟. 面试题不仅仅为了应付面试,更是知识点的一个梳理总结归 ...

  4. 动态规划初级练习(一):ZigZag

    Problem Statement      A sequence of numbers is called a zig-zag sequence if the differences between ...

  5. vue.js 组件引用之初级 之二

    1. template 标签也可以实现替换,这样可以省去script标签了 <!DOCTYPE html> <html lang="en"> <hea ...

  6. js 正则函数初级之二

    1. 小括号在正则中: 1.1 小括号:表示分组 1.2 分组之后,,每个组都有一个序号,从左到右,依次为1,2,3.......:可以使用 RegExp.$1,RegExp.$2,RegExp.$3 ...

  7. Flask初级(二)为flash创建路由,访问路径。

    Project name :Flask_Plan 上一篇文章,我们创建了默认的flask项目,也可以运行起来. 但是只有一个首页,只显示一个hello world. 现在我们创建访问路由,也就是访问地 ...

  8. SOAP协议初级指南 (二)

    XML 作为一个更好的网络数据表达方式(NDR) HTTP是一个相当有用的RPC协议,它提供了IIOP或DCOM在组帧.连接管理以及序列化对象应用等方面大部分功能的支持.( 而且URLs与IORs和O ...

  9. 动态规划专题(二)——树形DP

    前言 \(DP\)这东西真的是博大精深啊...... 简介 树形\(DP\),顾名思义,就是在树上操作的\(DP\),一般可以用\(f_i\)表示以编号为\(i\)的节点为根的子树中的最优解. 转移的 ...

随机推荐

  1. css居中技巧

    1    text-align: center; 只能对图片,按钮,文字等行内元素(display为inline或inline-block等)进行水平居中.在IE6.7中能对任何元素进行水平居中.另外 ...

  2. .Net程序员 Solr-5.3之旅 (一)Solr入门

    阅读目录 引言 Lunece是什么? Solr是什么 JAVA环境搭建 JAVA环境搭建之变量配置 Tomcat简单配置 结尾 引言 君子生非异也,善假于物也. Java和.Net哪个好,我们也不需要 ...

  3. Sql Server跨服务器导出特定表数据

    --连接服务器 exec sp_addlinkedserver 'Test',' ','SQLOLEDB', '192.168.0.7'; exec sp_addlinkedsrvlogin 'Tes ...

  4. XCode的一些调试技巧

    XCode 内置GDB,我们可以在命令行中使用 GDB 命令来调试我们的程序.下面将介绍一些常用的命令以及调试技巧. po 命令:为 print object 的缩写,显示对象的文本描述(显示从对象的 ...

  5. SVN的使用(转发)

    http://my.oschina.net/joanfen/blog/194491?fromerr=LM5QY3YF

  6. CSS 布局Float 【0】

    float是 css 样式的定位属性.我们在印刷排版中,文本可以按照需要围绕图片.一般把这种方式称为“文本环绕”.在网页设计中,应用了CSS的float属性的页面元素就像在印刷布局里面的被文字包围的图 ...

  7. SQL Server附加数据库拒绝访问

    打开要附加的数据库文件所在的文件夹,即扩展名为mdf的文件所在的文件夹,如下图所示:   右键单击mdf文件,选择“属性”,如下图所示:   单击“安全”选项卡,如下图所示:   单击“编辑”按钮,如 ...

  8. 读取url(1

    就书本例子 import java.io.InputStream; import java.net.URL; public class Test { public static void main(S ...

  9. 【USACO 3.2.6】香甜的黄油

    [描述] 农夫John发现做出全威斯康辛州最甜的黄油的方法:糖.把糖放在一片牧场上,他知道N(1<=N<=500)只奶牛会过来舔它,这样就能做出能卖好价钱的超甜黄油.当然,他将付出额外的费 ...

  10. 支持mdev的init脚本片断

    mount -t tmpfs mdev /dev mount -t sysfs sysfs /sys mkdir /dev/pts mount -t devpts devpts /dev/pts ec ...