AlbertMourato

Untitled

Mar 1st, 2016
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <bits/stdc++.h>
  3. //%lld
  4. typedef long long ll;
  5. #define si(a) scanf("%d", &a)
  6. using namespace std;
  7.  
  8. int vetor[25];
  9. int matriz[25][2000005];
  10. int saida[25];
  11. int main(){
  12.     //freopen("in.txt", "r", stdin);
  13.     //freopen("out.txt", "w", stdout);
  14.     int n, t;
  15.     while(si(n)==1){
  16.         memset(matriz, 0, sizeof(matriz));
  17.         memset(saida, 0, sizeof(saida));
  18.         memset(vetor, 0, sizeof(vetor));
  19.         si(t);
  20.         for(int i = 0;i<t;i++)si(vetor[i]);
  21.         matriz[0][vetor[0]] = 1;
  22.         for(int i =1;i<t;i++){
  23.             for(int j = 0;j<=n;j++){
  24.                 if(j<vetor[i]){
  25.                     matriz[i][j] = matriz[i-1][j];
  26.                 }else if(vetor[i]==j){
  27.                     matriz[i][j] = 1;
  28.                 }else{
  29.                     matriz[i][j] = max(matriz[i-1][j], matriz[i-1][j-vetor[i]]);
  30.                 }
  31.             }
  32.         }//matriz marcada corretamente
  33.  
  34.         int v = 0;
  35.         for(int i = n;i>=0;i--){//procurando primeira aparição do maior numero <= n
  36.             for(int j = 0;j<25;j++){
  37.                 if(matriz[j][i]==1){//encontrou
  38.                     saida[v] = vetor[j];//salvou o valor que o compõe no vetor de saida
  39.                     v++;
  40.                     if((i-vetor[j])<=0){//chegou no ultimo numero que compoe o n
  41.                         j = 25;//sai do loop
  42.                         i = -1;
  43.                         break;
  44.                     }
  45.                     i-=vetor[j];//vai procurar na linha anterior
  46.                     j = 26;//sai daqui
  47.                     i+=2;
  48.                     //proxima iteração o i fara --
  49.                 }
  50.             }
  51.         }
  52.         for(int i = v-1;i>=0;i--)printf("%d ", vetor[i]);
  53.         printf("sum: %d", vetor[0]);
  54.         /*for(int i = 0;i<t;i++){
  55.             for(int j = 0;j<=n;j++){
  56.                 cout<<matriz[i][j]<<" ";
  57.             }
  58.             cout<<endl;
  59.         }*/
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment