Hdu5014number sequence (Greedy)
Question Link
Question:
N is given, and a number string is given. The length is n + 1 and the range is [0, n-1]. then ask you to find another sequence B to meet the above requirements, and make T = A0 ^ b0 + AI + 1 ^ Bi + 1 +... + an ^ BN is the largest.
Solution:
If a number is exclusive or requires the maximum result, it is best to take the binary complementary number of this number, it can be found that each time a number is found and the corresponding complementary number is a range. In this way, we can find the best matching point for each point in a certain interval.
Code:
#include <cstdio>#include <cstring>typedef long long ll;const int N = 1e5 + 5;const int M = 20;int num[N];int Map[N];int n;ll t[M];void init () { t[0] = 1; for (int i = 1; i <= M; i++) t[i] = t[i - 1] * 2;}int main () { init(); while (scanf ("%d", &n) == 1) { for (int i = 0; i <= n; i++) scanf ("%d", &num[i]); int rear = n; int front; ll ans = 0;// printf ("%lld\n", t[M - 1]); while (rear >= 0) { for (int i = 0; i < M; i++) if (t[i] > rear) { front = t[i] - rear - 1; break; } for (int i = 0; i < (rear - front + 1) / 2; i++) { Map[rear - i] = front + i; Map[front + i] = rear - i; } if (rear == front) Map[rear] = front; rear = front - 1; } for (int i = 0; i <= n; i++) ans += num[i] ^ Map[num[i]]; printf ("%lld\n", ans); for (int i = 0; i < n; i++) printf("%d ", Map[num[i]]); printf ("%d\n", Map[num[n]]); } return 0;}
Hdu5014number sequence (Greedy)