C++ two dimensional array of bitsets - c++

I have an assignment where we're tackling the traveling salesman problem.
I'm not going to lie, the part I'm doing right now I actually don't understand fully that they're asking, so sorry if I phrase this question weirdly.
I sort of get it, but not fully.
We're calculating an approximate distance for the salesman. We need to create a two-dimensional array, of bitsets I believe? Storing the values in binary anyway.
0 represents that the city hasn't been visited, and 1 represents that is has been visited.
We've been given an algorithm that helps significantly, and I should be able to finish it if anyone here can help with the first step:
Create memoisation table [N][(1 << N)]
(where N = number of cities).
I get that 1 << N means convert the number of cities (e.g. 5) to binary, then move the set to the left by one place.
My main issues are:
Converting N to binary (I think this is what I need to do?)
Moving the set to the left by one
Actually creating the 2-dimensional array of these sizes...
I could be wrong here, in fact that's probably pretty likely... any help is appreciated, thanks!

Here is the general rule "<<" operator means left shift and ">>" means right shift. Right shifting any number by 1 is equivalent to divide by 2 and left shift any numbers by 2 is equivalent to multiply by 2. For example lets say a number 7 (Binary 111). So 7 << 1 will become 1110 which is 7 * 2 = 14 and 7 >> 1 will become 11 which is 7 / 2 = 3 .
So for algorithm to convert a number N to a bitset array as binary is
N mod 2 (take the remainder if you divide N by 2)
Store the remainder in a collection (i.e, List, Array, Stack )
Divide N by 2
If N/2 >1 Repeat from step 1 with N/2
Else reverse the array and you have your bitset.
Moving the set left to one, If you meant leftshift by one you can do it by N<<1
This is how you create 2 dimensional array in C++
[Variable Type] TwoDimensionalArray[size][size];
For this problem though I believe you might want to read about C++ bitset and you can easily implement it using bitset. For that you just have to figure out the size of the bitset you want to use. For example if the highest value of N is 15 then you need a bitset size of 4. Because with 4 bit the maximum number you can represent is 15 (Binary 1111). Hope this helps.

Related

What is the maximum skill that these 2 magic cards can play?

I'm trying to write a program with C/C++ that solve the problem below, I was wondering can we use treap to deal with it?
Description
There are 2n magic cards, and each magic card has two positive integers a and b. Divide these magic cards into 2 heaps, each with n cards. The skill value that a bunch of magic cards can play is the product of the smallest a and the smallest b in the heap. You have a super power that can swap the a and b values on a magic card and use this ability indefinitely. You want to know how to divide the magic cards into 2 heaps (you can use super powers). What is the maximum skill that these 2 magic cards can play?
Input
The first behavior is a positive integer n, indicating that there are 2n magic cards;
The next 2n lines, each line of two positive integers ai, bi, represent two values on each magic card;
For 60% of the data, 1<=n<=10^3;
For 100% of the data, 1<=n<=10^5, 1<=ai, bi<=10^9.
Output
Output a positive integer indicating the maximum skill value.
Sample input
2
1 2
3 4
5 6
7 8
Sample output
32
Sample output means 1*2+5*6=32
This test data is made by myself:
input:
3
8 1
2 7
5 4
2 6
4 6
5 3
output:
17
Output means 1*5+2*6=17
I am going to use the struct to do this, let's see what result OJ will give tomorrow (probably TLE).
Here is some idea from the problem hint, first use super power to turn all cards into a<b, sorting by a value from small to large.
The smallest a is fixed in the first heap, and enumerated from the second smallest value of a. Assuming enumeration to a[i], put all cards after i (including i) in the second heap, and put all the cards before i into the first heap.
At this time, we need to select n-i+1 from the second heap to put into the first heap. The strategy chosen is as follows:
Sort all the cards of the second heap from small to large based on b value, then select the first n-i+1 or the next n-i+1 for the first heap.
In fact, it is to find the card with the first smallest b value or the n-i+1 big b value. This process is maintained with treap, and the total time complexity is O(nlogn).

Bit Manipulation: Harder Flipping Coins

Recently, I saw this problem from CodeChef titled 'Flipping Coins' (Link: FLIPCOINS).
Summarily, there are N coins and we must write a program that supports two operations.
To flip coin in range [A,B]
To find the number of heads in range [A,B] respectively.
Of course, we can quickly use a segment tree (range query, range updates using lazy propagation) to solve this.
However, I faced another similar problem where after a series of flips (operation 1), we are required to output the resulting permutation of coins after the flips (e.g 100101, where 0 represents head while 1 represents tail).
More specifically, operation 2 changes from counting number of heads to producing the resulting permutation of all N coins. Also, the new operation 2 is only called after all the flips have been done (i.e operation 2 is the last to be called and is only called one time).
May I know how does one solve this? It requires some form of bit manipulation, according to the problem tags.
Edit
I attempted brute-forcing through all queries, and alas, it yield Time Limit Exceeded.
Printing out the state of the coins can be done using a Binary-indexed tree:
Initially all values are 0.
When we need to flip coins [A, B], we increment A by 1 and
decrement B + 1 by 1.
The state of coin i is then the prefix sum at i modulo 2.
This works because the prefix sum at i is always the number of flip operations done at i.

Best way to store multi variable polynomials in Lisp

I need to store polynomials in my lisp program for adding, subtracting and multiplying. But cannot find an easy way of storing one.
I've considered the following way
(2x^3 + 2x + 4y^3 - 2z) in a list of lists where each list is a list of the amount of each power
= ( (0 2 0 2) (0 0 0 4) (0 2) )
But the uncertain lengths of each list and potential length could become a problem.
Is there a generally accepted way to store them in lisp which could make it as easy as possible to add, subtract and multiply them together?
Assuming you know the number of possible variables beforehand, you could express each term like this: (constant x-exponent y-exponent z-exponent ...). Then 5xy^2 would be (5 1 2 0), and a full expression would just be a list of those terms.
If you want to be able to handle any number of arbitrary variables, you would need to do an associative list along the lines of ((constant 5) (a 0) (b 3) (z 23) (apple 13)).
Either way, if you start with individual terms, it's easy to build more complex expressions and this way you don't need to mess with multiple dimensions.
May be this idea will help you partly. You can represent polynomial as vector, when index will be a power and an element - a coefficient, and first element - your variable. I mean 5*x^3 + 10*x^2 + 40x + 50 will look like #(50 40 10 5). Working with such representation easy, but it looks like not very optimal for big powers like x^100.
Multivariable polynomial may be represented as N-dimensional array where N - number of variables.
There are several ways of representing polynomials. As usual the choice of representation is a tradeoff.
One way is an association list from order to coefficient usually sorted
after according to order.
12x^2 + 11x + 10 ((2 . 12) (11 . 1) (10 . 0))
If you need to compute with sparse polynomials, then this representation is space efficient. x^200 is just ((200 . 1)).
If your calculations consists mostly of non-sparse polynomals a vector representation is more space efficient:
12x^2 11x + 10 (vector 10 11 12)
The length of the vector minus one gives the order of the polynomial.
If you need polynomials of more than one variable there are variations of the representations. In particular you can look a the representation in Maxima:
http://maxima.sourceforge.net/docs/manual/maxima_14.html
If you happen to have "Paradigms of Artificial Intelligence Programming: Case Studies in Common LISP" by Peter Norvig, there is a nice chapter on polynomials.

Method to find number of digits after converting from a different base number

The text in quotes gives a bit of background on my program in case it's needed to understand my issue, you might be able to fully understand with the stuff at the end unquoted if you don't feel like reading it.
I'm working on the common project of sorting in C++, and I am
currently doing radix sort. I have it as a function, taking in a
vector of strings, an integer holding the max number of digits, and an
integer with the radix/base of the numbers: (numbers, maxDigits, radix)
Since the program takes in numbers of different base and as a string,
I'm using stoi to convert them to a base 10 integer to make the
process easier to generalize. Here's a quick summary of the algorithm:
create 10 queues to hold values 0 to 9
iterate through each digit (maxDigit times)
iterate through each number in the vector (here it converts to a base 10)
put them into the queue based on the current digit it's looking at
pull the numbers out of the queues from beginning to end back into the vector
As for the problem I'm trying to wrap my head around, I want to change the maxDigit value (with whatever radix the user inputs) to a maxDigit value after it is converted to base 10. In other words, say the user used the code
radixSort(myVector, 8, 2)
to sort a vector of numbers with the max number of digits 8 and a radix of 2. Since I convert the radix of the number to 10, I'm trying to find an algorithm to also change the maxDigits, if that makes sense.
I've tried thinking about this so much, trying to figure out a simple way through trial and error. If I could get some tips or help in the right direction that would be a great help.
If something is in radix 2 and max digits 8, then its largest value is all ones. And 11111111 = 255, which is (2^8 - 1).
The maximum digits in base 10 will be whatever is needed to represent that largest value. Here we see that to be 3. Which is the base 10 logarithm of 255 (2.40654018043), rounded up to 3.
So basically just round up log10 (radix^maxdigits - 1) to the nearest whole number.

Number contained in an odd number of sets

I have a homework problem which i can solve only in O(max(F)*N) ( N is about 10^5 and F is 10^9) complexity, and i hope you could help me. I am given N sets of 4 integer numbers (named S, F, a and b); Each set of 4 numbers describe a set of numbers in this way: The first a successive numbers, starting from S included are in the set. The next b successive numbers are not, and then the next a numbers are, repeating this until you reach the superior limit, F. For example for S=5;F=50;a=1;b=19 the set contains (5,25,45); S=1;F=10;a=2;b=1 the set contains (1,2,4,5,7,8,10);
I need to find the integer which is contained in an odd number of sets. It is guaranteed that for the given test there is ONLY 1 number which respects this condition.
I tried to go trough every number between min(S) and max(F) and check in how many number of sets this number is included, and if it is included in an odd number of sets, then this is the answer. As i said, in this way I get an O (F*N) which is too much, and I have no other idea how could I see if a number is in a odd number of sets.
If you could help me I would be really grateful. Thank you in advance and sorry for my bad English and explanation!
Hint
I would be tempted to use bisection.
Choose a value x, then count how many numbers<=x are present in all the sets.
If this is odd then the answer is <=x, otherwise >x.
This should take time O(Nlog(F))
Alternative explanation
Suppose we have sets
[S=1,F=8,a=2,b=1]->(1,2,4,5,7,8)
[S=1,F=7,a=1,b=0]->(1,2,3,4,5,6,7)
[S=6,F=8,a=1,b=1]->(6,8)
Then we can table:
N(y) = number of times y is included in a set,
C(z) = sum(N(y) for y in range(1,z)) % 2
y N(y) C(z)
1 2 0
2 2 0
3 1 1
4 2 1
5 2 1
6 2 1
7 2 1
8 2 1
And then we use bisection to find the first place where C(z) becomes 1.
Seems like it'd be useful to find a way to perform set operations, particularly intersection, on these sets without having to generate the actual sets. If you could do that, the intersection of all these sets in the test should leave you with just one number. Leaving the a and b part aside, it's easy to see how you'd take the intersection of two sets that include all integers between S and F: the intersection is just the set with S=max(S1, S2) and F=min(F1, F2).
That gives you a starting point; now you have to figure out how to create the intersection of two sets consider a and b.
XOR to the rescue.
Take the numbers from each successive set and XOR them with the contents of the result set. I.e., if the number is currently marked as "present", change that to "not present", and vice versa.
At the end, you'll have one number marked as present in the result set, which will be the one that occurred an odd number of times. All of the others will have been XORed an even number of times, so they'll be back to the original state.
As for complexity, you're dealing with each input item exactly once, so it's basically linear on the total number of input items -- at least assuming your operations on the result set are constant complexity. At least if I understand how they're phrasing things, that seems to meet the requirement.
It sounds like S is assumed to be non-negative. Given your desire for an O(max(F)*N) time boundary you can use a sieving-like approach.
Have an array of integers with an entry for each candidate number (that is, every number between min(S) and max(F)). Go through all the quadruples and add 1 to all array locations associated with included numbers represented by each quadruple. At the end, look through the array to see which count is odd. The number it represents is the number that satisfies your conditions.
This works because you're going under N quadruples, and each one takes O(max(F)) or less time (assuming S is always non-negative) to count the included numbers. That gives you O(max(F)*N).