동적 계획 초급 연습(二): BadNeighbors

8426 단어 동적 기획
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; }

좋은 웹페이지 즐겨찾기