#include <cstdio> #include <algorithm> using namespace std; inline bool rev(int a, int b){ return (a>b); } int a[24]; int c[100]; bool Check(int k, int n){ int s=0; bool b[24]={0}; for(int i=0;i<k;++i){ int tmp=c[i]; for(int j=0;j<n;++j) if(!b[j] && tmp>=a[j]){ tmp-=a[j]; b[j]=1; ++s; } } return (s==n); } int BinarySearch(int m, int n){ int p=1, q=m, res=m+1; while(p<=q){ int m=(p+q)>>1; if(Check(m, n)){ res=m; q=m-1; }else p=m+1; } return res; } int main() { int n, m; scanf("%d%d", &n, &m); for(int i=0;i<n;++i) scanf("%d", a+i); sort(a, a+n, rev); for(int i=0;i<m;++i) scanf("%d", c+i); sort(c, c+m, rev); int res=BinarySearch(m, n); if(res<=m) printf("%d", res); else printf("NIE"); }
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 | #include <cstdio> #include <algorithm> using namespace std; inline bool rev(int a, int b){ return (a>b); } int a[24]; int c[100]; bool Check(int k, int n){ int s=0; bool b[24]={0}; for(int i=0;i<k;++i){ int tmp=c[i]; for(int j=0;j<n;++j) if(!b[j] && tmp>=a[j]){ tmp-=a[j]; b[j]=1; ++s; } } return (s==n); } int BinarySearch(int m, int n){ int p=1, q=m, res=m+1; while(p<=q){ int m=(p+q)>>1; if(Check(m, n)){ res=m; q=m-1; }else p=m+1; } return res; } int main() { int n, m; scanf("%d%d", &n, &m); for(int i=0;i<n;++i) scanf("%d", a+i); sort(a, a+n, rev); for(int i=0;i<m;++i) scanf("%d", c+i); sort(c, c+m, rev); int res=BinarySearch(m, n); if(res<=m) printf("%d", res); else printf("NIE"); } |