Wednesday, 30 September 2015

*TRT - Treats for the Cows

TRT - Treats for the Cows



FJ has purchased N (1 <= N <= 2000) yummy treats for the cows who get money for giving vast amounts of milk. FJ sells one treat per day and wants to maximize the money he receives over a given period time. The treats are interesting for many reasons:
  • The treats are numbered 1..N and stored sequentially in single file in a long box that is open at both ends. On any day, FJ can retrieve one treat from either end of his stash of treats.
  • Like fine wines and delicious cheeses, the treats improve with age and command greater prices.
  • The treats are not uniform: some are better and have higher intrinsic value. Treat i has value v(i) (1 <= v(i) <= 1000).
  • Cows pay more for treats that have aged longer: a cow will pay v(i)*a for a treat of age a.
Given the values v(i) of each of the treats lined up in order of the index i in their box, what is the greatest value FJ can receive for them if he orders their sale optimally?
The first treat is sold on day 1 and has age a=1. Each subsequent day increases the age by 1.

Input

Line 1: A single integer, N
Lines 2..N+1: Line i+1 contains the value of treat v(i)

Output

The maximum revenue FJ can achieve by selling the treats

Example

Input:
5
1
3
1
5
2

Output:
43
-------------------------------------editorial---------------------------------------------------------
similar than optimal game stratgy but only difference is that player is only one , so and he has choice to chose either left or right at any instant
#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,int day)
 {


   if(start>end)return 0;
  
  if(start==end)
   {
  
       return day*dp[start][start];
    
   }
  else  if(dp[start][end]!=-1)
  {
 
   return dp[start][end];
   }
   
   
    else
    {
     
     lli val1=day*arr[start]+solve(arr,start+1,end,day+1);
      lli  val2=day*arr[end]+solve(arr,start,end-1,day+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;
   
    
    
    
      for(int i=0;i<n;i++)
   {
    cin>>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,1);
 
 
  cout<<dp[0][n-1]<<endl;
  } 
}




basic optimal game stratgy

#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(dp[start][end]!=-1) return dp[start][end];
  else
  {
 
  lli val1=arr[start]+min(solve(arr,start+2,end),solve(arr,start+1,end-1));
  lli  val2=arr[end]+min(solve(arr,start,end-2),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;
}
}

**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;
  } 
}