redusk's blog

By redusk, history, 8 years ago, In English

Dear All,

I have solved the problem using dynamic programming where dp[i][0] indicates not to include the current element i into the set and dp[i][1] indicates to include the current element into the set. Here is my code:

#include<bits/stdc++.h>
#include<math.h>
using namespace std;

long long int a[100001];

long long int dp[100002][2];

int main()
{
    long int n,i;
    scanf("%ld",&n);
    for ( i = 1 ; i <= n ; i++ ){
        scanf("%lld",&a[i]);
    }
    //memset(dp, 0, sizeof(dp[0][0]) * 2 * 100002);
    for( i = n ; i >= 1 ; i--){
        dp[i][0] = max(dp[i+1][0] ^ 0 , dp[i+1][1] ^ 0);
        dp[i][1] = max(dp[i+1][0] ^ a[i],dp[i+1][1] ^ a[i]);
    }
    dp[0][0] = max(dp[1][0]^0,dp[1][1]^0);
    printf("%lld",dp[0][0]);
}

I am getting a wrong answer even though the test cases I tried are giving the correct ouput. I checked the constraints as well and the data types are correct. It is a very short code. Could someone please help me?

Thank you.

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

| Write comment?
»
8 years ago, # |
  Vote: I like it +4 Vote: I do not like it

The dp solution is wrong. Take this example:

3

15 0 1

The reason it is wrong is because it is not optimal to always be looking at the maximum xor you can get ahead of the current number because it is possible that on taking xor with the current number some smaller number would have given a better result (I hope I made some sense).