#include<iostream> using namespace std; void quicksort(int* tab, int first, int last) { int i = first-1; int temp, j; int pivot = tab[last]; for(j=first; j<last; ++j) { if(tab[j]>pivot) { ++i; temp = tab[i]; tab[i] = tab[j]; tab[j] = temp; } } if(i<first) { temp = tab[first]; tab[first]=tab[last]; tab[last]=temp; i++; } if (first<i) quicksort(tab, first, i); if (last>i+1) quicksort(tab, i+1, last); } int main(int argc, char** argv) { int n,m; int * a; int * c; int ** v; cin >> n; cin >> m; a = new int[n]; c = new int[m]; for(int i=0; i<n; ++i) { cin >> a[i]; } for(int i=0; i<m; ++i) { cin >> c[i]; } if(n>1) quicksort(a,0,n-1); if(m>1) quicksort(c,0,m-1); if(m>n) m = n; v = new int*[n]; for(int i=0;i<n;++i) { v[i] = new int[m+1]; for(int j=0;j<m+1;++j) { v[i][j] = 0; } } int i = 0; while(i<n) { if(i>0) for(int j=0;j<m && j<=i;++j) v[i][j] = v[i-1][j]; else v[0][0] = 0; while(v[i][m]<=i && v[i][m]<m) { int j = v[i][m]; int temp = v[i][j] + a[i]; while(j>0 && v[i][j-1] < temp && v[i][j-1] < c[j]) j--; if(temp < c[j]) { for(int k=v[i][m]; k>j; k--) { v[i][k] = v[i][k-1]; } v[i][j] = temp; i++; break; } else { v[i][m]++; } } if(i<n && (v[i][m] > i || v[i][m]==m)) { if(i==0) break; v[i][m]=0; i--; v[i][m]++; } } if(i<n) { cout << "NIE"; } else { i=0; while(v[n-1][i] > 0) ++i; cout << i; } delete [] a; delete [] c; for(int j=0;j<n;++j) { delete [] v[j]; } delete [] v; return 0; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 | #include<iostream> using namespace std; void quicksort(int* tab, int first, int last) { int i = first-1; int temp, j; int pivot = tab[last]; for(j=first; j<last; ++j) { if(tab[j]>pivot) { ++i; temp = tab[i]; tab[i] = tab[j]; tab[j] = temp; } } if(i<first) { temp = tab[first]; tab[first]=tab[last]; tab[last]=temp; i++; } if (first<i) quicksort(tab, first, i); if (last>i+1) quicksort(tab, i+1, last); } int main(int argc, char** argv) { int n,m; int * a; int * c; int ** v; cin >> n; cin >> m; a = new int[n]; c = new int[m]; for(int i=0; i<n; ++i) { cin >> a[i]; } for(int i=0; i<m; ++i) { cin >> c[i]; } if(n>1) quicksort(a,0,n-1); if(m>1) quicksort(c,0,m-1); if(m>n) m = n; v = new int*[n]; for(int i=0;i<n;++i) { v[i] = new int[m+1]; for(int j=0;j<m+1;++j) { v[i][j] = 0; } } int i = 0; while(i<n) { if(i>0) for(int j=0;j<m && j<=i;++j) v[i][j] = v[i-1][j]; else v[0][0] = 0; while(v[i][m]<=i && v[i][m]<m) { int j = v[i][m]; int temp = v[i][j] + a[i]; while(j>0 && v[i][j-1] < temp && v[i][j-1] < c[j]) j--; if(temp < c[j]) { for(int k=v[i][m]; k>j; k--) { v[i][k] = v[i][k-1]; } v[i][j] = temp; i++; break; } else { v[i][m]++; } } if(i<n && (v[i][m] > i || v[i][m]==m)) { if(i==0) break; v[i][m]=0; i--; v[i][m]++; } } if(i<n) { cout << "NIE"; } else { i=0; while(v[n-1][i] > 0) ++i; cout << i; } delete [] a; delete [] c; for(int j=0;j<n;++j) { delete [] v[j]; } delete [] v; return 0; } |