1176. Two Ends

#include <iostream>
#include <stdlib.h>
#include <algorithm>
#include <memory.h>

using namespace std;
int dp[1000][1000];
int a[1000];

int fun(int left, int right)
{
  if(left > right)
    return 0;
  //if(left == right)
  //  return dp[left][right];
  if(dp[left][right] != -1)
    return dp[left][right];
  //1.重点:动态规划,最容易出题点,但是题目很难理解 
  //选择左端,如果剩下的左端大则选择左端,否则选择右端,然后剩下的段再递归 
  int leftSum = a[left] + (a[left+1] >= a[right]? fun(left+2, right) : fun(left+1, right-1) );
  //选择右端,如果剩下的左端大则选择左端,否则选择右端,然后剩下的段再递归 
  int rightSum = a[right] +(a[left] >= a[right-1]? fun(left+1, right-1): fun(left, right-2));  
  //2.重点:max函数 
  dp[left][right] = max(leftSum, rightSum);
  return dp[left][right];
}

int main()
{
    int n = 0;
    int count = 0;
    
    while(1)
    {
      cin >> n;
      if (n == 0)
      break;   
      //3.重点:memset函数 (#include <memory.h>) 
      memset(dp,-1,sizeof(dp));
      
      int Sum =0;
      for(int i=0; i < n; i++)
      {
         cin >> a[i];
         Sum = Sum+a[i];
       }
       int firstSum = fun(0,n-1);
        
       count++;
       cout << "In game " << count << ", the greedy strategy might lose by as many as "<<2*firstSum-Sum<<" points."<< endl;  
       
    }
    
    //system("pause");   
    return 0;   
}                                 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值