geronimo00's blog

By geronimo00, history, 5 years ago, In English

Hi, I need help from CF community regarding this DP problem. I had idea implementing this problem with bitwise subsets, foreach combination -> check max weight.. And can't see what is wrong. My answers are partially correct. (I know that DP solution is better, this is just for fun)

https://atcoder.jp/contests/dp/tasks/dp_d

This is my code and it only works on small numbers.

#include<bits/stdc++.h>
#define vi vector<int>
#include <algorithm>
#include <iterator>
#include <inttypes.h>
#include<iostream>
using ll = long long;
using namespace std;

int main()
{
   long long n,w;
   cin >> n >> w;

   long long a[100][2];
   for(int i=0;i<n;i++){
        cin>>a[i][0] >> a[i][1];

   }
   long long result = 0;

   for(int i=0;i<1<<n;i++){
        long long weightSum = 0;
        long long valueSum = 0;

        for(int j=0;j<n;j++){
            if(i & (1 << j)){
                weightSum+= a[j][0];
                valueSum+= a[j][1];
            }
        }

        if(weightSum <= w){
            result = max(result, valueSum);
        }
   }

   cout << result;

   return 0;
}

  • Vote: I like it
  • -3
  • Vote: I do not like it

| Write comment?
»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

To speak frankly, your code has so many mistakes that I don't know how to point your mistakes......

I think you should make some small data on your computer and find what goes wrong with the help of debugger or sth else by yourself. Thit is also good for you to practice how to debug on your compuputer.

  • »
    »
    5 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    I posted here hoping that mistake will be obvious for trained eye. I made small data examples, and every single one of results is ok, but for these large examples it fails..

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Sorry, I didn't carefully read what you said above your code before, so I mistakenly thought you didn't know this code would TLE or MLE and this isn't so-called "DP with bitmasks". (which in fact you knew)

      Since you knew that wasn't dp with bitmasks and you know it would certainly get TLE or MLE on that peoblem, the only mistake in your code is what abhishek_saini pointed, and that makes your code only works for N no more than 30.

      Btw, you used "using ll" but still used "long long" in your code lol

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Value of n can be till $$$100$$$. 4 byte int cannot hold $$$1$$$ << $$$100$$$ (neither can 8 byte long long int).