Test instructions
A sequence of n+1 numbers is given, which consists of a 0-n.
Ask you to give another sequence has a 0-n composition, so that they each corresponding to the value of the difference or add the largest;
Output maximum, and the sequence you give;
Ideas:
In order to get the maximum value, the binary system should be complementary, such as 10110^01001;
Our maximum value is n, so we first want to find out what the value of n complementary is, for example finding is C and n complementary;
Then n-1 and c+1 complement each other, n-2 and c+2 are complementary;
And then recursively using c-1 as the maximum value;
As to how to find and n complementary;
such as 10110, then I first calculate it is less than equal to which 2 of the second side, that is, 100000;
Then take N to get back; 11111. 01001 (the last 5 are reversed, the front is 1)
and (100000-1) do and, so that the last 5 digits, leaving 0;
(Do this problem hdu convulsions, hand it all is time-out, has been modified to change for a long time do not know why, and later put someone else A's code to turn over. Full face black line. )
Ac
#include <cstdio> #include <cstring>const int N = 1e5 + 10;int sum;struct uni{int x, y;} U[n];int m[n],num[n],n;void F (int x) {if (x = = 0) {m[0] = 0;return;} int cur = 1;int c;for (int i = 0;; i++) {if (x <= cur) break;cur *= 2;} c = ~x & (cur-1); for (int i = X,J = C; i > J; I--, J + +) {M[i] = j;m[j] = i;} if (cur! = 0) f (c-1);} int main () {while (~SCANF ("%d", &n)) {sum = 0;for (int i = 0; I <= N; i++) {scanf ("%d", &num[i]);} F (n);p rintf ("%d\n", (1 + N) * n/2), for (int i = 0; I <= N; i++) {if (i) printf ("");p rintf ("%d", M[num[i]]);} printf ("\ n");}}
hdu5014 (xor + search interval)