hdu-5014-Number Sequence-XiAn网络赛1008-水题

思路:既然是求两个数的异或运算之和,且由于数字不重复,那么肯定两个数异或的结果数字越大越好,即异或后从ai二进制的最高位后全是1。

具体思路看代码:

AC代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <queue>
 4 #include <vector>
 5 #include <algorithm>
 6 using namespace std;
 7 int bit[17];
 8 int brr[100005];
 9 int n;
10 void init()
11 {
12     bit[0] = 1;
13     for(int i = 0; i <= 16; i++) {
14         bit[i+1] = 2<<i;
15     }
16 }
17 int arr[100005];
18 void deal(int x)
19 {
20     for(int i = 0; i < n+10; i++) arr[i] = -1;
21     arr[0] = 0;
22     int k, le, xx;
23     while(x != 0){
24         if(x <= 0) return;
25         int pos = upper_bound(bit, bit+18, x) - bit - 1;
26         if(pos == 0) {
27             arr[0] = 1; arr[1] = 0;
28             return;
29         }
30         xx = x;
31         if(pos == 1) k = 0x00000003 ^ x;
32         else if(pos == 2) k = 0x00000007 ^ x;
33         else if(pos == 3) k = 0x0000000f ^ x;
34         else if(pos == 4) k = 0x0000001f ^ x;
35         else if(pos == 5) k = 0x0000003f ^ x;
36         else if(pos == 6) k = 0x0000007f ^ x;
37         else if(pos == 7) k = 0x000000ff ^ x;
38         else if(pos == 8) k = 0x000001ff ^ x;
39         else if(pos == 9) k = 0x000003ff ^ x;
40         else if(pos == 10) k = 0x000007ff ^ x;
41         else if(pos == 11) k = 0x00000fff ^ x;
42         else if(pos == 12) k = 0x00001fff ^ x;
43         else if(pos == 13) k = 0x00003fff ^ x;
44         else if(pos == 14) k = 0x00007fff ^ x;
45         else if(pos == 15) k = 0x0000ffff ^ x;
46         else if(pos == 16) k = 0x0001ffff ^ x;
47         le = k;
48         x = k - 1;
49         for(int i = xx; i >= k; i--) arr[i] = le++;
50     }
51 }
52 int main()
53 {
54     init();
55     while(scanf("%d", &n) != EOF) {
56         deal(n);
57         long long ans = 0;
58         for(int i = 0; i <= n; i++) {
59             int a; scanf("%d", &a);
60             brr[i] = arr[a];
61             ans += a ^ arr[a];
62         }
63         printf("%I64d\n%d", ans, brr[0]);
64         for(int i = 1; i <= n; i++) {
65             printf(" %d", brr[i]);
66         }
67         printf("\n");
68     }
69     return 0;
70 }

总结:QAQ比赛的时候看错题意了。。。。忽略了数字不重复的条件,导致想了好久。。。。

时间: 2024-10-07 06:49:28

hdu-5014-Number Sequence-XiAn网络赛1008-水题的相关文章

HDU 5014 Number Sequence(西安网络赛H题)

HDU 5014 Number Sequence 题目链接 思路:对于0-n,尽量不让二进制中的1互相消掉就是最优的,那么只要两个数只要互补就可以了,这样每次从最大的数字,可以找到和他互补的数字,然后这个区间就能确定了,然后剩下的递归下去为一个子问题去解决 代码: #include <cstdio> #include <cstring> const int N = 100005; int n, a[N], ans[N]; int cnt[N]; int count(int x) {

HDU 5014 Number Sequence(2014 ACM/ICPC Asia Regional Xi&#39;an Online) 题解

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Number Sequence Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequ

hdu 5014 Number Sequence(贪心)

题目链接:hdu 5014 Number Sequence 题目大意:给定n,表示有0~n这n+1个数组成的序列a,要求构造一个序列b,同样是由0~n组成,要求∑ai⊕bi尽量大. 解题思路:贪心构造,对于n来说,找到n对应二进制的取反对应的数x,那么从x~n之间的数即可两两对应,然后x-1即是一个子问题. #include <cstdio> #include <cstring> #include <algorithm> using namespace std; con

HDU 5014 Number Sequence 贪心 2014 ACM/ICPC Asia Regional Xi&#39;an Online

尽可能凑2^x-1 #include <cstdio> #include <cstring> const int N = 100005; int a[N], p[N]; int init(int x) { int cnt = 0; while(x > 1) { x /= 2; cnt ++; } return cnt + 1; } int main() { int n; while(~scanf("%d", &n)){ for(int i = 0;

HDU 5014 Number Sequence ( 构造 )

HDU 5014 Number Sequence ( 构造 ) 题目意思: 给出一个数列 由0 - n 组成 然后需要构造一个数列 (也是由0-n组成),使得 sum(A[i] ^ B[i])的值最大. 分析: 异或不能出现比这个数大的情况,,所以要从大的数往前找. 现计算出当前判断数的二进制位的位数m,然后再与2^m - 1进行异或,这样会保证最大,具体为什么,自己可以想一想 比如: 5 - 101 -- 3位 - 对应 111 101 ^ 111 = 010 代码: #include <cs

hdu 5014 Number Sequence(西安网络赛1008)

Number Sequence                                                                  Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 633    Accepted Submission(s): 300 Special Judge Problem Descrip

ACM学习历程——HDU 5014 Number Sequence (贪心)(2014西安网赛)

Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● a i ∈ [0,n] ● a i ≠ a j( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows(“?” denotes exclus

HDU 5014 Number Sequence(异或 进制问题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5014 Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the inte

HDU - 5014 Number Sequence

Problem Description There is a special number sequence which has n+1 integers. For each number in sequence, we have two rules: ● ai ∈ [0,n] ● ai ≠ aj( i ≠ j ) For sequence a and sequence b, the integrating degree t is defined as follows("⊕" deno

hdu 5014 Number Sequence(意淫题)

Number Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 545    Accepted Submission(s): 258 Special Judge Problem Description There is a special number sequence which has n+1 integers. F