Chip Factory(HDU5536 + 暴力 || 01字典树)

Posted dillonh

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Chip Factory(HDU5536 + 暴力 || 01字典树)相关的知识,希望对你有一定的参考价值。

题目链接:

  http://acm.hdu.edu.cn/showproblem.php?pid=5536

题目:

技术分享图片

题意:

  对于给定的n个数,求出三个下标不同的数使得(si+sj)^sk最大。

思路:

  由于时间给了9s,所以可以暴力过。不过还可以用01字典树艹过去,不过注意字典树里面存si查询(sj+sk),不要存(si+sj)查询sk,不然会T。

暴力代码实现如下:

 1 #include <set>
 2 #include <map>
 3 #include <deque>
 4 #include <ctime>
 5 #include <stack>
 6 #include <cmath>
 7 #include <queue>
 8 #include <string>
 9 #include <cstdio>
10 #include <vector>
11 #include <iomanip>
12 #include <cstring>
13 #include <iostream>
14 #include <algorithm>
15 using namespace std;
16 
17 typedef long long LL;
18 typedef pair<LL, LL> pll;
19 typedef pair<LL, int> pli;
20 typedef pair<int, int> pii;
21 typedef unsigned long long uLL;
22 
23 #define lson rt<<1
24 #define rson rt<<1|1
25 #define name2str(name)(#name)
26 #define bug printf("**********
");
27 #define IO ios::sync_with_stdio(false);
28 #define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
29 #define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin);
30 
31 const double eps = 1e-8;
32 const int mod = 1e9 + 7;
33 const int maxn = 1000 + 7;
34 const int inf = 0x3f3f3f3f;
35 const double pi = acos(-1.0);
36 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
37 
38 int t, n;
39 int s[1007];
40 
41 int main() {
42 #ifndef ONLINE_JUDGE
43     FIN;
44 #endif
45     scanf("%d", &t);
46     while(t--) {
47         scanf("%d", &n);
48         LL ans = -1;
49         for(int i = 1; i <= n; i++) {
50             scanf("%d", &s[i]);
51         }
52         for(int i = 1; i <= n; i++) {
53             for(int j = 1; j < i; j++) {
54                 for(int k = 1; k < j; k++) {
55                     ans = max(ans, (LL)(s[i] + s[j]) ^ s[k]);
56                     ans = max(ans, (LL)(s[i] + s[k]) ^ s[j]);
57                     ans = max(ans, (LL)(s[j] + s[k]) ^ s[i]);
58                 }
59             }
60         }
61         printf("%lld
", ans);
62     }
63     return 0;
64 }

01字典树:

  1 #include <set>
  2 #include <map>
  3 #include <deque>
  4 #include <ctime>
  5 #include <stack>
  6 #include <cmath>
  7 #include <queue>
  8 #include <string>
  9 #include <cstdio>
 10 #include <vector>
 11 #include <iomanip>
 12 #include <cstring>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16 
 17 typedef long long LL;
 18 typedef pair<LL, LL> pll;
 19 typedef pair<LL, int> pli;
 20 typedef pair<int, int> pii;
 21 typedef unsigned long long uLL;
 22 
 23 #define lson rt<<1
 24 #define rson rt<<1|1
 25 #define name2str(name)(#name)
 26 #define bug printf("**********
");
 27 #define IO ios::sync_with_stdio(false);
 28 #define debug(x) cout<<#x<<"=["<<x<<"]"<<endl;
 29 #define FIN freopen("/home/dillonh/CLionProjects/in.txt","r",stdin);
 30 
 31 const double eps = 1e-8;
 32 const int mod = 1e9 + 7;
 33 const int maxn = 1000 + 7;
 34 const int inf = 0x3f3f3f3f;
 35 const double pi = acos(-1.0);
 36 const LL INF = 0x3f3f3f3f3f3f3f3fLL;
 37 
 38 int t, n, le, root;
 39 int a[40], num[maxn];
 40 
 41 struct node{
 42     int cnt;
 43     int nxt[3];
 44 
 45     void init(){
 46         cnt = 0;
 47         nxt[0] = nxt[1] = -1;
 48     }
 49 }T[maxn*130];
 50 
 51 void insert(int n){
 52     int now = root;
 53     for(int i = 0; i <= 30; i++) {
 54         a[i] = n & 1;
 55         n >>= 1;
 56     }
 57     for(int i = 30; i >= 0; i--){
 58         int x = a[i];
 59         if(T[now].nxt[x] == -1){
 60             T[le].init();
 61             T[now].nxt[x] = le++;
 62         }
 63         now = T[now].nxt[x];
 64         T[now].cnt++;
 65     }
 66 }
 67 
 68 LL search(int n){
 69     int now = root;
 70     LL ans = 0;
 71     for(int i = 0; i <= 30; i++) {
 72         a[i] = n & 1;
 73         n >>= 1;
 74     }
 75     for(int i = 30; i >= 0; i--){
 76         int x = a[i];
 77         if(T[now].nxt[1-x] == -1 || T[T[now].nxt[1-x]].cnt <= 0) {
 78             now = T[now].nxt[x];
 79         } else {
 80             ans += 1LL << i;
 81             now = T[now].nxt[1-x];
 82         }
 83     }
 84     return ans;
 85 }
 86 
 87 void Trie_dele(int n){
 88     int now = 0;
 89     for(int i = 0; i <= 30; i++) {
 90         a[i] = n & 1;
 91         n >>= 1;
 92     }
 93     for(int i = 30;i >= 0; i--){
 94         int tmp = a[i];
 95         now = T[now].nxt[tmp];
 96         T[now].cnt--;
 97     }
 98 }
 99 
100 int main() {
101 #ifndef ONLINE_JUDGE
102     FIN;
103 #endif
104     scanf("%d", &t);
105     while(t--) {
106         scanf("%d", &n);
107         le = 1;
108         T[0].init();
109         LL ans = -1;
110         for(int i = 0; i < n; i++) {
111             scanf("%d", &num[i]);
112             insert(num[i]);
113         }
114         for(int i = 0; i < n; i++) {
115             Trie_dele(num[i]);
116             for(int j = 0; j < i; j++) {
117                 if(i == j) continue;
118                 Trie_dele(num[j]);
119                 ans = max(ans, search(num[i] + num[j]));
120                 insert(num[j]);
121             }
122             insert(num[i]);
123         }
124         printf("%lld
", ans);
125     }
126     return 0;
127 }

 

以上是关于Chip Factory(HDU5536 + 暴力 || 01字典树)的主要内容,如果未能解决你的问题,请参考以下文章

HDU 5536: Chip Factory

HDU 5536 Chip Factory

HDU 5536 Chip Factory

HDU5536 Chip Factory

HDU - 5536 Chip Factory 字典树的删除

hdu 5536 Chip Factory 字典树+bitset 铜牌题