zoukankan      html  css  js  c++  java
  • 动态规划初级练习(二):BadNeighbors

    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=0;
            int dp[50];
            bool flag;
            memset(dp,0,sizeof dp);
            if(donations.size()==0) return 0;
            if(donations.size()==1) return donations[0];
            if(donations.size()==2) return max(donations[0],donations[1]);
            REP(i,1,donations.size())
            {
                flag=true;
                dp[i]=donations[i];
                REP(j,1,i-1)
                {
                    ans=max(dp[j]+donations[i],ans);
                    flag=false;
                }
                if(!flag)
                    dp[i]=ans;
            }
            int ret=0;
            memset(dp,0,sizeof dp);
            dp[0]=donations[0];
            REP(i,1,donations.size()-1)
            {
                dp[i]=donations[i];
                flag=true;
                REP(j,0,i-1)
                {
                    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[]= { 11,15 };
        vector<int> x(_x,_x+sizeof(_x)/sizeof(_x[0]));
        BadNeighbors b;
        printf("%d
    ",b.maxDonations(x));
        return 0;
    }
  • 相关阅读:
    日期 根据所选日期 获取 之后N天的日期
    错误退出登录
    挂载路由导航守卫 router
    缓存 ssessionStorage&localStorage
    vue项目 第三方图标库阿里图库
    码云新建仓库 以及本地上传
    sql的四种连接-左外连接、右外连接、内连接、全连接
    C#中常用修饰符
    接口的隐式和显式实现
    C#break、continue、return、goto
  • 原文地址:https://www.cnblogs.com/aboutblank/p/3309152.html
Copyright © 2011-2022 走看看