Cod sursa(job #656337)

Utilizator vendettaSalajan Razvan vendetta Data 4 ianuarie 2012 14:45:44
Problema Ghiozdan Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.16 kb
#include <fstream>
#define nmax 25000
#define gmax 75000

using namespace std;

int dp[nmax][gmax], t[100][100];
int n, G;
int a[100][100];
int w[nmax];
ifstream f("ghiozdan.in");
ofstream g("ghiozdan.out");

void citeste(){

   f>>n>>G;
   for(int i=1; i<=n; ++i){
      f>>w[i];
   }
   int k = 0;

   for(int i=1; i<=n; ++i)
      for(int j=0; j<=G; ++j)
      {a[i][j] = k; ++k;}

}

void rezolva(){

   for(int i=1; i<=n; ++i){
      for(int j=0; j<=G; ++j){
         dp[i][j] = dp[i-1][j];
         t[i][j] = a[i-1][j];
         if (j >= w[i])
            if (dp[i][j] >= dp[i-1][j - w[i]] + w[i])
               t[i][j] = a[i-1][j];
            else
               dp[i][j] = dp[i-1][j - w[i]]+ w[i],
               t[i][j] = w[i];
            //else t[i][j] = t[i-1][j] + 1;
      }
   }

}

void scrie(){

   for(int i=1; i<=n; ++i){
      for(int j=1; j<=G; ++j)
         g<<a[i][j]<<" ";
      g<<"\n";
   }
      g<<"\n";
   for(int i=1; i<=n; ++i){
      for(int j=1; j<=G; ++j)
         g<<t[i][j]<<" ";
      g<<"\n";
   }


}

int main(){

   citeste();
   rezolva();
   scrie();

   return 0;

}