AtCoder Begginer Contest ABC032 D - ナップサック問題
2020/1/28
ポイント
半分全列挙
制約によってDPを変える
code:c++
using namespace std;
#define rep(i,N) for(int i=0;i<int(N);++i) #define all(a) (a).begin(),(a).end() typedef long long ll;
template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return true; } return false; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return true; } return false; }
const int INF = 1e9;
const ll INFLL = 1e18;
const ll MOD = 1e9+7;
const double PI = acos(-1.0);
const int dx8 = {1, 0, -1, 0, 1, -1, -1, 1}; const int dy8 = {0, 1, 0, -1, 1, 1, -1, -1}; /*------------------------------------/
for library*/
/*------------------------------------*/
int N;
ll W;
vector<ll> v,w;
ll maxW, maxV;
//solve1
vector<pair<ll,ll>> ps;
//solve2,3
//入力
void input(){
cin >> N >> W;
v.resize(N); w.resize(N);
maxW = maxV = 0;
rep(i,N){
}
}
//Nが小さい -> 半分全列挙
void solve1(){
//片方を全列挙
int n2 = N/2;
//半分の選ぶ選ばないを全列挙
for(int i = 0; i < bit(n2);i++){
ll sw = 0;
ll sv = 0;
for(int j = 0;j < n2;j++){
if(i & bit(j)){
}
}
ps.push_back({sw, sv});
}
sort(all(ps));
//不要なものを除く(wが大きいのに価値が小さいものを除く)
ll m = 1;
for(int i = 1; i < bit(n2);i++){
if(psm-1.second < psi.second){ m++;
}
}
ll ans = 0;
//後ろ半分を列挙して解を求める
for(int i = 0; i < bit(N - n2);i++){
ll sw = 0;
ll sv = 0;
for(int j = 0; j < N-n2; j++){
if(i & bit(j)){
}
}
if(sw <= W){
ll idx = lower_bound(all(ps),make_pair(W - sw,INFLL)) - ps.begin();
chmax(ans, sv + tv);
}
}
cout << ans <<endl;
}
//wが小さいdp
//i番目までの商品で、重さw以下で実現できる最大の価値v
void solve2(){
rep(i,210)rep(j,202000){
}
rep(i,N)rep(j,202000){
}
}
}
//vが小さいdp
//i番目までの商品で、価値vを実現するのに必要な最小のw
void solve3(){
rep(i,210)rep(j,202000){
}
rep(i,N)rep(j,202000){
}
}
ll ans = 0;
rep(j, 202000){
chmax(ans, (ll)j);
}
}
cout<<ans<<endl;
}
int main() {
cin.tie(0);
ios::sync_with_stdio(false);
cout << fixed << setprecision(20);
input();
if(N <= 30){
solve1();
}
else if(maxW <= 1000){
solve2();
}
else{
solve3();
}
}