Posts

Showing posts with the label DP

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) ...

Important links DP

http://www.sharjah.ac.ae/en/about/agc/conferences/ME-GPC/Documents/Chapter%2011%20-%20Dynamic%20Programming.pdf https://sites.google.com/site/indy256/algo/convex_hull_optimization

DP hackerrank (stock maximize)

STock Maximize (iterative dP  and  branch & bound approach ) #include<bits/stdc++.h> using namespace std; long long int sell[1000000],a[1000000]; int main() {  int t;  cin>>t;  while(t--)  {   long long int n,i,j,k,ans=0,m=0;   cin>>n;   for(i=0;i<n;i++)    cin>>a[i];   for(i=n-1;i>=0;i--)    {     if(m<a[i]) m=a[i];     sell[i]=m;    }   for(i=0;i<n;i++)   {     if(sell[i]>a[i])    ans+=sell[i]-a[i];   }   cout<<ans<<endl;  } return 0; } -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------Branch And  Bound -----------...

DP (rectangles perimetere (spoj) ,,,Sherlock and cost (hackerrank) ) -->> 2^n DP

1) DP prblm of 2^n type ,(rectangles perimetre http://www.spoj.com/problems/MMAXPER/) #include using namespace std; int main() { int n; cin>> n; int a[n],b[n],dp[n][2],i,j,k; for(i=0;i >a[i]>>b[i]; dp[0][0]=a[0], dp[0][1]=b[0]; for(i=1;i using namespace std; int dp[100002][2]; int a[100002]; int main() { int t; cin>>t; while(t--) { int n,i,j,k; cin>>n; dp[0][0]=0; dp[0][1]=0; for(i=0;i >a[i]; for(i=1;i

knapsack /unbounded knapsack

Image
http://www.spoj.com/problems/KNAPSACK/    (simple knapsack) http://www.spoj.com/problems/LKS/         (large knapsack) Knapsack table filling #include<stdio.h> int maxi(int a,int b) {     if(a>b)         return a;     else         return b; } int main() {     int i,h,j,p,mm,summ=0,g,mw;     scanf("%d",&mw);     scanf("%d",&h); int w[h+1],v[h+1]; for(i=1;i<h+1;i++) { scanf("%d",&w[i]); scanf("%d",&v[i]); } v[0]=0;w[0]=0; int n[h+1][mw+1]; v[0]=0;w[0]=0; for(i=0;i<mw+1;i++)     n[0][i]=0; for(i=0;i<h+1;i++)     n[i][0]=0; for(i=1;i<h+1;i++) {     for(j=1;j<mw+1;j++)     {         p=j-w[i];         if(p<0)                    ...