#include <iostream> #include <algorithm> using namespace std; #define MAXN 24 #define MAXM 100 int a[MAXN], c[MAXM]; int main() { int n, m; cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < m; i++) cin >> c[i]; sort(a, a+n, greater<int>()); sort(c, c+m, greater<int>()); if(m > n) m = n; // heuristics 1 long long suma = 0, lbound; for(int i = 0; i < n; i++) suma += a[i]; for(int i = 0; suma > 0 && i < m; i++) { suma -= c[i]; if(suma <= 0) lbound = i+1; } if(suma > 0) { cout << "NIE"; return 0; } // heuristics 2 int wc[MAXN+2], ubound = 0; for(int i = 0; i < m; i++) wc[i] = c[i]; wc[m] = 2e9; wc[m+1] = 2e9; for(int i = 0; i < n; i++) { int j; for(j = 0; wc[j] < a[i]; j++) { wc[j] -= a[i]; if(j > ubound) ubound = j+1; } } if(ubound > m) ubound = m; if(ubound == lbound) { cout << lbound; return 0; } cout << rand() % (ubound - lbound + 1) + lbound; // desperacja :) 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 | #include <iostream> #include <algorithm> using namespace std; #define MAXN 24 #define MAXM 100 int a[MAXN], c[MAXM]; int main() { int n, m; cin >> n >> m; for(int i = 0; i < n; i++) cin >> a[i]; for(int i = 0; i < m; i++) cin >> c[i]; sort(a, a+n, greater<int>()); sort(c, c+m, greater<int>()); if(m > n) m = n; // heuristics 1 long long suma = 0, lbound; for(int i = 0; i < n; i++) suma += a[i]; for(int i = 0; suma > 0 && i < m; i++) { suma -= c[i]; if(suma <= 0) lbound = i+1; } if(suma > 0) { cout << "NIE"; return 0; } // heuristics 2 int wc[MAXN+2], ubound = 0; for(int i = 0; i < m; i++) wc[i] = c[i]; wc[m] = 2e9; wc[m+1] = 2e9; for(int i = 0; i < n; i++) { int j; for(j = 0; wc[j] < a[i]; j++) { wc[j] -= a[i]; if(j > ubound) ubound = j+1; } } if(ubound > m) ubound = m; if(ubound == lbound) { cout << lbound; return 0; } cout << rand() % (ubound - lbound + 1) + lbound; // desperacja :) return 0; } |