#include <iostream>
#include <algorithm>
#include <list>
#include <vector>
using namespace std;
typedef pair<int,int> PII;
int n,m;
int waga[30];
int plecak[105];
list<int>l;
vector<PII>v;
vector<int> nast[30];
bool cmp(int a, int b){
return a>b;
}
bool cmp2(PII a, PII b){
return a.first>b.first;
}
int maks=-1;
int pop[30];
int ile=0;
int odw[30];
int odw2[30];
void dfs(int a, int b,int suma, int szuk){
for(int i=a; i<n; i++){
if(suma+waga[i]<=szuk&&!odw[i]){
pop[b]=i;
dfs(i+1,b+1,suma+waga[i], szuk);
}
else continue;
}
if(maks<suma) {
maks=suma;
ile=b;
for(int i=0; i<b; i++) odw2[i]=0;
for(int i=0; i<b; i++) odw2[i]=pop[i];
}
}
int main(){
cin >> n >> m;
for(int i=0; i<n; i++) {
int a;
cin >> waga[i];
v.push_back(PII(waga[i],i));
}
for(int i=0; i<m; i++) {
cin >> plecak[i];
}
sort(plecak, plecak+m);
int wszystkie=0;
int cos=0;
for(int j=0; j<m; j++){
dfs(0,0,0,plecak[j]);
for(int i=0; i<ile; i++) odw[odw2[i]]=1;
maks=-1;
wszystkie+=ile;
if(wszystkie==n) {cos=j;break;}
ile=0;
}
if(wszystkie==n) cout << cos+1 << endl;
else cout << "NIE" << endl;
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 | #include <iostream> #include <algorithm> #include <list> #include <vector> using namespace std; typedef pair<int,int> PII; int n,m; int waga[30]; int plecak[105]; list<int>l; vector<PII>v; vector<int> nast[30]; bool cmp(int a, int b){ return a>b; } bool cmp2(PII a, PII b){ return a.first>b.first; } int maks=-1; int pop[30]; int ile=0; int odw[30]; int odw2[30]; void dfs(int a, int b,int suma, int szuk){ for(int i=a; i<n; i++){ if(suma+waga[i]<=szuk&&!odw[i]){ pop[b]=i; dfs(i+1,b+1,suma+waga[i], szuk); } else continue; } if(maks<suma) { maks=suma; ile=b; for(int i=0; i<b; i++) odw2[i]=0; for(int i=0; i<b; i++) odw2[i]=pop[i]; } } int main(){ cin >> n >> m; for(int i=0; i<n; i++) { int a; cin >> waga[i]; v.push_back(PII(waga[i],i)); } for(int i=0; i<m; i++) { cin >> plecak[i]; } sort(plecak, plecak+m); int wszystkie=0; int cos=0; for(int j=0; j<m; j++){ dfs(0,0,0,plecak[j]); for(int i=0; i<ile; i++) odw[odw2[i]]=1; maks=-1; wszystkie+=ile; if(wszystkie==n) {cos=j;break;} ile=0; } if(wszystkie==n) cout << cos+1 << endl; else cout << "NIE" << endl; return 0; } |
English