#include <stdlib.h>
#include <cstdio>
#include <algorithm>
#include<iterator>
#include <string>
#include <vector>
#include <iostream>
#include <cmath>
#include <list>
#include<queue>
#include <deque>
#define FOR(x,z) for(int x=0;x<(z);++x)
#define DS(i) fprintf(stderr, "DEBUG: %s\n",i);
#define DI(i) fprintf(stderr, "DEBUG: %d\n",i);
#define DF(i) fprintf(stderr, "DEBUG: %f\n",i);
using namespace std;
vector<int> a;
//vector<int> pq;
int n,m;
bool sprawdz1(vector<int>& a, int m, int p)//najwiekszy w najmniejsze
{
//cout << "raz\n";
vector<int>b(a),c;
//printf("ssss %d\n", b.size());
c.assign(m,p);
bool ok;
while(b.size()>0)
{
ok=false;
//for(int i =0;i<m;i++)
for(int i =0;i<c.size();i++)
{
//printf("tutaj %d %d\n", c[i],b.back());
if(c[i]>=b.back())
{
c[i]-=b.back();
b.pop_back();
ok=true;
break;
}
}
if(!ok)
{
//cout << "dwaf\n";
return false;
}
sort(c.begin(),c.end());
}
return true;
}
void wykonaj()
{
long double suma=0;
int x, p=0,w=0;
scanf("%d %d", &n, &m);
FOR(i,n)
{
scanf("%d", &x);
suma+=x;
a.push_back(x);
}
FOR(i,m)
{
scanf("%d", &x);
if(x>p)p=x;
}
sort(a.begin(),a.end());
if(a.back()>p)
{
printf("NIE\n");
return;
}
int z;
for(z =0;z<a.size()&&a[a.size()-z-1]<p/2;z++);
//printf("aaa %d %d %d\n", int(suma/p), int(z),int(p));
if(suma/p>z)z=suma/p;
//z - ograniczenie od dolu;
//n - ogranieczenie od gory;
int o=(n-z)/2;//obecny
w=n;
while(z<=n)
{
o=(n+z+1)/2;
// printf("%d %d %d %d\n",z, n, o, p);
if(sprawdz1(a,o,p))
{
w=o;
n=o-1;
}
else
{
z=o+1;
}
}
//cout << w;
printf("%d\n",w);
// while(a.size()>0)
// {
// if(a.front()>p/2)
// {
// pq.push_back(p-a.front());
// a.pop_front();
// }
// else
// break;
// }
// sort(pq.begin(),pq.end());//,greater<int>());
// cout << pq[0] << pq[1];
}
int main()
{
wykonaj();
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 122 123 124 125 | #include <stdlib.h> #include <cstdio> #include <algorithm> #include<iterator> #include <string> #include <vector> #include <iostream> #include <cmath> #include <list> #include<queue> #include <deque> #define FOR(x,z) for(int x=0;x<(z);++x) #define DS(i) fprintf(stderr, "DEBUG: %s\n",i); #define DI(i) fprintf(stderr, "DEBUG: %d\n",i); #define DF(i) fprintf(stderr, "DEBUG: %f\n",i); using namespace std; vector<int> a; //vector<int> pq; int n,m; bool sprawdz1(vector<int>& a, int m, int p)//najwiekszy w najmniejsze { //cout << "raz\n"; vector<int>b(a),c; //printf("ssss %d\n", b.size()); c.assign(m,p); bool ok; while(b.size()>0) { ok=false; //for(int i =0;i<m;i++) for(int i =0;i<c.size();i++) { //printf("tutaj %d %d\n", c[i],b.back()); if(c[i]>=b.back()) { c[i]-=b.back(); b.pop_back(); ok=true; break; } } if(!ok) { //cout << "dwaf\n"; return false; } sort(c.begin(),c.end()); } return true; } void wykonaj() { long double suma=0; int x, p=0,w=0; scanf("%d %d", &n, &m); FOR(i,n) { scanf("%d", &x); suma+=x; a.push_back(x); } FOR(i,m) { scanf("%d", &x); if(x>p)p=x; } sort(a.begin(),a.end()); if(a.back()>p) { printf("NIE\n"); return; } int z; for(z =0;z<a.size()&&a[a.size()-z-1]<p/2;z++); //printf("aaa %d %d %d\n", int(suma/p), int(z),int(p)); if(suma/p>z)z=suma/p; //z - ograniczenie od dolu; //n - ogranieczenie od gory; int o=(n-z)/2;//obecny w=n; while(z<=n) { o=(n+z+1)/2; // printf("%d %d %d %d\n",z, n, o, p); if(sprawdz1(a,o,p)) { w=o; n=o-1; } else { z=o+1; } } //cout << w; printf("%d\n",w); // while(a.size()>0) // { // if(a.front()>p/2) // { // pq.push_back(p-a.front()); // a.pop_front(); // } // else // break; // } // sort(pq.begin(),pq.end());//,greater<int>()); // cout << pq[0] << pq[1]; } int main() { wykonaj(); return 0; } |
English