GastonFontenla

N3P3 - Hilera

Sep 1st, 2019
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.43 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define tripla vector <int>
  6. ///Sin ficha, ficha común, ficha especial
  7. #define sin 0
  8. #define com 1
  9. #define esp 2
  10.  
  11. int hilera(vector <int> numeros, int k, vector <int> &fichas)
  12. {
  13.     int n = numeros.size();
  14.     vector <vector <tripla> > DP(n+1, vector <tripla> (k+2, {0, 0, 0}));
  15.     vector <vector <vector <tripla> > > A(n+1, vector <vector <tripla> > (k+2, vector <tripla> (3, {-1, -1, -1})));
  16.  
  17.     for(int i=0; i<n; i++)
  18.     {
  19.         for(int j=0; j<=k; j++)
  20.         {
  21.             ///i numeros, j fichas especiales
  22.             ///No puedo ir de común a común (1 -> 1)
  23.             ///Los demás movimientos están permitidos
  24.  
  25.             ///Partiendo de "Sin ficha"
  26.             if(DP[i][j][sin] > DP[i+1][j][sin])
  27.             {
  28.                 DP[i+1][j][sin] = DP[i][j][sin];
  29.                 A[i+1][j][sin] = {i, j, sin};
  30.             }
  31.             if(DP[i][j][sin]+numeros[i] > DP[i+1][j][com])
  32.             {
  33.                 DP[i+1][j][com] = DP[i][j][sin]+numeros[i];
  34.                 A[i+1][j][com] = {i, j, sin};
  35.             }
  36.             if(DP[i][j][sin]+numeros[i] > DP[i+1][j+1][esp])
  37.             {
  38.                 DP[i+1][j+1][esp] = DP[i][j][sin]+numeros[i];
  39.                 A[i+1][j+1][esp] = {i, j, sin};
  40.             }
  41.  
  42.             ///Partiendo de "Ficha comun"
  43.             if(DP[i][j][com] > DP[i+1][j][sin])
  44.             {
  45.                 DP[i+1][j][sin] = DP[i][j][com];
  46.                 A[i+1][j][sin] = {i, j, com};
  47.             }
  48.             if(DP[i][j][com]+numeros[i] > DP[i+1][j+1][esp])
  49.             {
  50.                 DP[i+1][j+1][esp] = DP[i][j][com]+numeros[i];
  51.                 A[i+1][j+1][esp] = {i, j, com};
  52.             }
  53.  
  54.             ///Partiendo de "Ficha especial"
  55.             if(DP[i][j][esp] > DP[i+1][j][sin])
  56.             {
  57.                 DP[i+1][j][sin] = DP[i][j][esp];
  58.                 A[i+1][j][sin] = {i, j, esp};
  59.             }
  60.             if(DP[i][j][esp]+numeros[i] > DP[i+1][j][com])
  61.             {
  62.                 DP[i+1][j][com] = DP[i][j][esp]+numeros[i];
  63.                 A[i+1][j][com] = {i, j, esp};
  64.             }
  65.             if(DP[i][j][esp]+numeros[i] > DP[i+1][j+1][esp])
  66.             {
  67.                 DP[i+1][j+1][esp] = DP[i][j][esp]+numeros[i];
  68.                 A[i+1][j+1][esp] = {i, j, esp};
  69.             }
  70.         }
  71.     }
  72.  
  73.     int fil = 0, col = 0, tip = 0;
  74.     for(int i=0; i<=n; i++)
  75.         for(int j=0; j<=k; j++)
  76.             for(int c=0; c<3; c++)
  77.                 if(DP[i][j][c] > DP[fil][col][tip])
  78.                     fil = i, col = j, tip = c;
  79.  
  80.     fichas = vector <int> (n, 0);
  81.     int maxSuma = DP[fil][col][tip];
  82.  
  83.     while(fil != 0)
  84.     {
  85.         fichas[fil-1] = tip;
  86.         tripla res = A[fil][col][tip];
  87.         fil = res[0];
  88.         col = res[1];
  89.         tip = res[2];
  90.     }
  91.  
  92.     return maxSuma;
  93. }
  94. /**
  95. Casos sencillos
  96. A) 6 0 20 10 10 20 10 20 (K = 0)
  97. B) 9 3 10 20 10 10 20 10 10 20 10 (K = 3)
  98. C) 6 3 20 10 10 20 10 20 (K = 3)
  99. D) 5 0 20 10 10 30 15 (K = 0)
  100. E) 5 0 20 10 10 20 15 (K = 0)
  101. F) 7 0 3 2 2 3 2 2 3 (K = 0)
  102. G) 5 0 20 10 10 30 15 (K = 0)
  103. **/
  104.  
  105. /**
  106. //Función main auxiliar para testear
  107. int main()
  108. {
  109.     int n, k;
  110.     cin >> n >> k;
  111.     vector <int> numeros(n);
  112.     for(int i=0; i<n; i++)
  113.         cin >> numeros[i];
  114.     vector <int> fichas;
  115.     int res = hilera(numeros, k, fichas);
  116.     cout << res << endl;
  117.     for(auto i:fichas)
  118.         cout << i << " ";
  119.     cout << endl;
  120. }
  121. **/
Advertisement
Add Comment
Please, Sign In to add comment