A、B两伙马贼意外地在一片沙漠中发现了一处金矿,双方都想独占金矿,但各自的实力都不足以吞下对方,经过谈判后,双方同意用一个公平的方式来处理这片金矿。处理的规则如下:他们把整个金矿分成n段,由A、B开始...

第一种做法:这种方法,算法复杂性大,重复的递归
#include "stdafx.h"

#include<iostream>  
#include<vector>  
#include<string>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<numeric>
using namespace std;

int getGolds(vector<int> golds);

int delGolds(vector<int> golds)
{
    if (golds.size() == 1)
    {
        golds.erase(golds.begin());
        return 0;
    }

    int leftNum = golds[0];
    vector<int> goldsLeft = golds;
    goldsLeft.erase(goldsLeft.begin());

    int rightNum = golds[golds.size() - 1];
    vector<int> goldsRight = golds;
    goldsRight.erase(goldsRight.end() - 1);

    if (leftNum+delGolds(goldsLeft) > rightNum+delGolds(goldsRight))
    {
//      cout << "B取:" << *golds.begin() << endl;
        golds.erase(golds.begin());
        
    }
    else
    {
    //  cout << "B取:" << *(golds.end() - 1) << endl;
        golds.erase(golds.end()-1);
    }
    return getGolds(golds);
}


int getGolds(vector<int> golds)
{
    if (golds.size() == 1 )
    {
        return golds[0];
    }

    int leftNum =golds[0];
    vector<int> goldsLeft=golds;
    goldsLeft.erase(goldsLeft.begin());

    int rightNum = golds[golds.size()-1];
    vector<int> goldsRight = golds;
    goldsRight.erase(goldsRight.end() - 1);

    
    
    return max(leftNum + delGolds(goldsLeft), rightNum + delGolds(goldsRight));
}


int main()
{
    int T;
    cin >> T;
    int flag = 1;
    while (T != 0)
    {
        T--;
        vector<int> golds;
        int n;
        cin >> n;
        for (int i = 0;i < n;i++)
        {
            int g;
            cin >> g;
            golds.push_back(g);
        }

        int aSum = getGolds( golds);
        int sum = accumulate(golds.begin(),golds.end(),0);
        int bSum = sum - aSum;
        
        
        cout << "Case #" << flag << ": " << aSum << " " << bSum << endl;
    
        
//      cout << "Case #" << flag << ": " << aSumleft << " " << bSumleft << endl;
        flag++;

    }


    return 0;
}

//第二种做法,还是算法复杂性大

#include "stdafx.h"

#include<iostream>  
#include<vector>  
#include<string>
#include<algorithm>
#include<math.h>
#include<iomanip>
#include<numeric>
#include<list>
#include<deque>

using namespace std;



int getGolds(vector<int> golds,int sum)
{
    if (golds.size() == 1 )
    {
        return golds[0];
    }

    int leftNum =golds[0];
    vector<int> goldsLeft=golds;
    goldsLeft.erase(goldsLeft.begin());

    int rightNum = golds[golds.size()-1];
    vector<int> goldsRight = golds;
    goldsRight.erase(goldsRight.end() - 1);

    
    
    return sum -min(getGolds(goldsLeft,sum- leftNum),  getGolds(goldsRight, sum - rightNum));
}


int main()
{
    int T;
    cin >> T;
    int flag = 1;
    while (T != 0)
    {
        T--;
        vector<int> golds;
        int n;
        int sum=0;
        cin >> n;
        for (int i = 0;i < n;i++)
        {
            int g;
            cin >> g;
            golds.push_back(g);
            sum += g;
        }

        int aSum = getGolds( golds,sum);
        
        int bSum = sum - aSum;
        
        
        cout << "Case #" << flag << ": " << aSum << " " << bSum << endl;
    
        
//      cout << "Case #" << flag << ": " << aSumleft << " " << bSumleft << endl;
        flag++;

    }


    return 0;
}

转载于:https://www.cnblogs.com/wdan2016/p/6582151.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值