两个船和n个货物,两个船分别容量有一定的限制,货物也有对应的重量。
解决的策略是最优的装一个船,然后看剩下的重量是否超过了另外一个船,如果超过了的话就不可解。
#include <iostream>
#include <queue>
#include <set>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
int c1,c2,n;
int* cache[11];
vector<int> w;
vector<string> res;
string isEnough();
void init();
void dp();
int main(){
while(cin >> c1>>c2>>n){
if(c1 == 0 && c2 == 0 && n == 0)
break;
init();
dp();
res.push_back(isEnough());
}
for(int i=0;i<res.size();i++)
cout << res[i] <<endl;
return 0;
}
void dp(){
for(int i = 2;i<=n;i++){
for(int j=1;j<=c1;j++){
cache[i][j] = max(cache[i-1][j],cache[i-1][j-w[i]+w[i]]);
}
}
}
string isEnough(){
int temp = 0;
for(int i=0;i<w.size();i++)
temp += w[i];
temp -= cache[n][c1];
if(temp <= c2)
return "Yes";
else
return "No";
}
void init(){
int temp;
for(int i=0;i<11;i++){
cache[i] = new int[c1+1];
for(int j = 0;j<c1+1;j++)
cache[i][j] = 0;
}
for(int i=0;i<n;i++){
cin >> temp;
w.push_back(temp);
}
for(int j=0;j<=c1;j++){
if(j<w[1])
cache[1][j] = 0;
else
cache[1][j] = w[1];
}
}