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. Qt 学习之路 :访问网络(4)

    前面几章我们了解了如何使用QNetworkAccessManager 访问网络.在此基础上,我们已经实现了一个简单的查看天气的程序.在这个程序中,我们使用QNetworkAccessManager进行 ...

  2. MVC模式下的数据展示:EasyUI的datagrid

    我的数据库设计是一张老师表teacher,一张学生表student,一个教师对应多个学生,在学生一方建立外键; 还有一点想清楚,需要展示的数据是根据什么来的,是成功登陆的用户的id?还是直接展示所有的 ...

  3. Ubuntu知识记录

    1.激活root用户:sudo passwd root 2.安装ftp:apt-get install vsftpd,修改配置文件/etc/vsftpd.conf write_enable=yes表明 ...

  4. dns-prefetch—DNS预解析技术

    今天在看一个网站的源代码时 发现了 <link rel="dns-prefetch" href="//static.tuweia.cn/"> 对dn ...

  5. lucene建立索引的过程

    建立索引过程 用户提交数据=>solr建立索引=>调用lucene包建立索引 官方建立索引和查询索引的例子如下: http://lucene.apache.org/core/4_10_3/ ...

  6. 各浏览器对 onbeforeunload 事件的支持与触发条件实现有差异

    转载:http://www.w3help.org/zh-cn/causes/BX2047 标准参考 无. 问题描述 一般情况下,onbeforeunload 事件处理函数内会写入一些提示性语句,当用户 ...

  7. 简单Word操作

    //创建空白Word文档 private void button1_Click(object sender, EventArgs e) { object missing = Missing.Value ...

  8. Activity之间的数据传递(Arraylist)

    1.使用Serialiable方法 实现序列化 2.使用Parcelable方法(这是android自己封装的类) Parcel类是封装数据的容器,封装后的数据通过Intent和IPC传递     实 ...

  9. 安装PHP过程中,make步骤报错:(集合网络上各种解决方法)

    安装PHP过程中,make步骤报错:(集合网络上各种解决方法) (1)-liconv -o sapi/fpm/php-fpm /usr/bin/ld: cannot find -liconv coll ...

  10. 启动php-fpm时报错

    [root@localhost ~]# /usr/local/php/sbin/php-fpm [06-Aug-2012 19:17:53] ALERT: [pool www] pm.min_spar ...