Wednesday, 30 September 2015

**TWENDS - Two Ends

TWENDS - Two Ends



In the two-player game “Two Ends”, an even number of cards is laid out in a row. On each card, face up, is written a positive integer. Players take turns removing a card from either end of the row and placing the card in their pile. The player whose cards add up to the highest number wins the game. Now one strategy is to simply pick the card at the end that is the largest — we’ll call this the greedy strategy. However, this is not always optimal, as the following example shows: (The first player would win if she would first pick the 3 instead of the 4.)
3 2 10 4
You are to determine exactly how bad the greedy strategy is for different games when the second player uses it but the first player is free to use any strategy she wishes.

Input

There will be multiple test cases. Each test case will be contained on one line. Each line will start with an even integer n followed by n positive integers. A value of n = 0 indicates end of input. You may assume that n is no more than 1000. Furthermore, you may assume that the sum of the numbers in the list does not exceed 1,000,000.

Output

For each test case you should print one line of output of the form:
In game m, the greedy strategy might lose by as many as p points.
where m is the number of the game (starting at game 1) and p is the maximum possible difference between the first player’s score and second player’s score when the second player uses the greedy strategy. When employing the greedy strategy, always take the larger end. If there is a tie, remove the left end.

Example

Input:
4 3 2 10 4
8 1 2 3 4 5 6 7 8
8 2 2 1 5 3 8 7 3
0

Output:
In game 1, the greedy strategy might lose by as many as 7 points.
In game 2, the greedy strategy might lose by as many as 4 points.
In game 3, the greedy strategy might lose by as many as 5 points.

-----------------------------------------code--------------------------------------------------------
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
typedef long int li;

#define test()    int test_case;cin>>test_case;while(test_case--)
#define fr(i,n) for(int i=0;i<n;i++)
#define frr(i,a,n) for(int i=a;i<n;i++)
#define sll(a) scanf("%lld",&a)
#define sl(a) scanf("%ld",&a)
#define si(a) scanf("%i",&a)
#define sd(a)  scanf("%ld",&a)
#define sf(a) scanf("%f",&a)
#define rn(a) return a
#define pai pair<int,int>
#define pal pair<li,li>
#define pall pair<lli,lli>
#define ff first
#define ss second
#define mod  1000000007
#define mp make_pair
#define pb push_back
#define pll(a) printf("%lld\n",a);
#define pl(a) printf("%lld\n",a);
#define pi(a) printf("%d\n",a);
#define pd(a) printf("%lf\n",a);
#define pf(a) printf("%f\n",a);
lli  dp[3000][3000];

int solve(lli  arr[],int start,int end)
 {
  
  if(start>end)return 0;
  if(dp[start][end]!=-1)
  {
 
   return dp[start][end];
   } 
  else if(start==end)
   {
    return dp[start][start];
    
   }
   else if(start+1==end)
    {
 
     return max(arr[start],arr[start+1]);
      
    }
 
    else
    {
      // if i chose start 
        lli val1=arr[start];
     if(arr[end]>arr[start+1])
     val1+=solve(arr,start+1,end-1);
     else
     val1+=solve(arr,start+2,end);
     
     // if i chose end 
      lli  val2=arr[end];
      if(arr[end-1]>arr[start])
      val2+=solve(arr,start,end-2);
      else
      val2+=solve(arr,start+1,end-1);
   
      
      dp[start][end]=max(val1,val2);
      
    }
    return dp[start][end];
 
 }
int main()
{

int n;
lli arr[10000];
int t=1;
while(1)
 {
    cin>>n;
   lli  tot_sum=0;
     if(n==0) return 0;
     
     
     else
     {
      for(int i=0;i<n;i++) 
   {
    cin>>arr[i];
             tot_sum+=arr[i];
   }
  }
  for(int i=0;i<n;i++)
   {
     for(int j=0;j<n;j++)
     dp[i][j]=-1;
   }
  for(int i=0;i<n;i++) dp[i][i]=arr[i];
 
 solve(arr,0,n-1);
 // cout<<dp[0][n-1]<<endl;
 
   cout<<"In game "<<t++<<", the greedy strategy might lose by as many as "<<dp[0][n-1]-(tot_sum-dp[0][n-1])<<" points."<<endl;
  } 
}

No comments:

Post a Comment