Posts

Showing posts with the label Game theory

Game theory (dp +game theory )

                                                 ww.spoj.com/problems/TRT/           TRT - Treats for the Cows In this problem ,we have to maximize the result . at each chance the value becomes (chance_number*value).we can pick either #include<bits/stdc++.h> using namespace std; int dp[2005][2005]; int a[10000]; int cal(int a[],int st,int en,int cur) {  if(st>en)     return 0;  if(dp[st][en]!=-1)     return dp[st][en];  if(st==en)  {      dp[st][en]=a[st]*cur;      return dp[st][en];  }  else  {  int ans1=a[st]*cur;  ans1+=cal(a,st+1,en,cur+1);  int ans2=a[en]*cur;  ans2+=cal(a,st,en-1,cur+1);  dp[st][en]=max(ans1,ans2);  return max(ans1,ans2);  } } int main(...

Game theory (Optimal game strategy)

                                                   http://www.spoj.com/problems/TWENDS/ TWENDS - Two Ends  In this problem 1st player  plays  optimally and second player plays greedly.  We have to pick either from first end or second end. #include<bits/stdc++.h> using namespace std; long long int  dp[2002][2002]; long long int  a[50000]; long long int send(long long int  a[],long long int  st ,long long int  end) {   if(st>end)    return 0;     if(dp[st][end]!=-1)     {        // dp[st][end]=a[st];         return dp[st][end];     }    if(st==end)    {      dp[st][end]=a[st];      return a[st];    }    if(st+1==end) ...