BUCT20180814邀请赛

Posted dup4

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了BUCT20180814邀请赛相关的知识,希望对你有一定的参考价值。

A:SUM

水。

技术分享图片
 1 #include<bits/stdc++.h>
 2  
 3 using namespace std;
 4  
 5 #define N 100010
 6 typedef long long ll;
 7  
 8 int n;
 9 ll arr[N];
10 ll sum[N];
11  
12 int main()
13 {
14     while(~scanf("%d",&n))
15     {
16         memset(sum, 0, sizeof sum);
17         for(int i = 1; i <= n; ++i)
18         {
19             scanf("%lld",&arr[i]);
20         }
21         for(int i = n; i >= 1; --i)
22         {
23             sum[i] = sum[i + 1] + arr[i] * (n - i + 1);
24         }
25         for(int i = 1;i <= n; ++i)
26         {
27             printf("%lld%c",sum[i]," 
"[i == n]);
28         }
29     }
30     return 0;
31 }
View Code

 

B:XOR1

思路:枚举ai, 去字典树中找最大值,取max

技术分享图片
 1 #include <bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define N 100010
 6 #define ll long long
 7 
 8 int tree[N * 64][5];
 9 int cnt[N * 64];
10 int pos;
11 
12 inline void Init()
13 {
14     memset(tree, 0, sizeof tree);
15     memset(cnt, 0, sizeof cnt);
16     pos = 0;
17 }
18 
19 inline void insert(int x)
20 {
21     bitset <40> b; b = x;
22     int root = 0;
23     for (int i = 39; i >= 0; --i)
24     {
25         int id = b[i];
26         if (!tree[root][id])
27             tree[root][id] = ++pos;
28         root = tree[root][id];
29         cnt[root]++; 
30     }
31 }
32 
33 inline ll Find(int x)
34 {
35     bitset <40> b; b = x;
36     ll ans = 0;
37     int root = 0;
38     for (int i = 39; i >= 0; --i)
39     {
40         int id = b[i] ^ 1;
41         bool flag = true;
42         if (!tree[root][id] || cnt[tree[root][id]] <= 0)
43             id ^= 1, flag = false;
44         root = tree[root][id];
45         if (flag) ans += (1 << i);
46     }
47     return ans;
48 }
49 
50 int n;
51 int arr[N];
52 
53 int main()
54 {
55     while (scanf("%d", &n) != EOF)
56     {
57         Init();
58         for (int i = 1; i <= n; ++i)
59         {
60             scanf("%d", arr + i);
61             insert(arr[i]);
62         }
63         ll ans = 0; 
64         for (int i = 1; i < n; ++i)
65         {
66             ans = max(ans, Find(arr[i]));
67         }
68         printf("%lld
", ans);
69     }    
70     return 0;
71 }
View Code

 

 

C:XOR2

思路:先DFS跑出DFS序,然后配合可持久化线段树就可以对子树进行区间操作。

我们自底向上推,这样对于一棵树,它的孩子的答案都已经更新,那么先取max, 假设直系孩子有n个,那么只需要枚举n - 1个孩子以及它的子树里的点更新答案就可以。

 

技术分享图片
  1 #include <bits/stdc++.h>
  2 
  3 using namespace std;
  4 
  5 #define N 100010
  6 #define ll long long
  7 
  8 int arr[N];
  9 
 10 struct Edge
 11 {
 12     int to, nx;
 13     inline Edge() {}
 14     inline Edge(int to, int nx) : to(to), nx(nx) {}
 15 }edge[N << 1]; 
 16 
 17 int head[N], pos, cnt;
 18 
 19 inline void Init() 
 20 {
 21     memset(head, -1, sizeof head);
 22     pos = 0; cnt = 0;
 23 }
 24 
 25 inline void addedge(int u, int v)
 26 {
 27     edge[++cnt] = Edge(v, head[u]); head[u] = cnt;
 28     edge[++cnt] = Edge(u, head[v]); head[v] = cnt; 
 29 }
 30 
 31 struct Point 
 32 {
 33     int fa, ord, son, id;
 34     int ans;
 35     inline bool operator < (const Point& r) const
 36     {
 37         return son - ord < r.son - r.ord;
 38     } 
 39 }point[N];  
 40 
 41 int n;
 42 int ford[N]; 
 43 
 44 struct node
 45 {
 46     int son[2], cnt;
 47     inline node()
 48     {
 49         memset(son, 0, sizeof son);
 50         cnt = 0;
 51     }
 52 }tree[N * 64]; 
 53 
 54 int root[N];
 55 int tot;
 56 
 57 inline void insert(int id, int x)
 58 {
 59     root[id] = ++tot;
 60     int pre = root[id - 1];
 61     int now = root[id]; 
 62     tree[now] = tree[pre];
 63     bitset <32> b; b = x; 
 64     for (int i = 31; i >= 0; --i)
 65     {
 66         int index = b[i];  
 67         tree[++tot] = tree[tree[now].son[index]];
 68         tree[tot].cnt++; 
 69         tree[now].son[index] = tot; 
 70         now = tot;
 71     }
 72 }
 73 
 74 inline int Find(int l, int r, int x)
 75 {
 76     int ans = 0;
 77     l = root[l], r = root[r];
 78     bitset <32> b; b = x;
 79     for (int i = 31; i >= 0; --i)
 80     {
 81         int index = b[i] ^ 1;
 82         bool flag = true;
 83         if (tree[tree[r].son[index]].cnt - tree[tree[l].son[index]].cnt <= 0)
 84         {
 85             index ^= 1;
 86             flag = false;
 87         }
 88         if (flag) ans += (1 << i);
 89         r = tree[r].son[index]; l = tree[l].son[index];
 90     }
 91     return ans;
 92 }
 93 
 94 struct Node
 95 {
 96     int id, cnt;
 97     inline Node() {}
 98     inline Node(int id, int cnt) : id(id), cnt(cnt) {}
 99     inline bool operator < (const Node &r) const
100     {
101         return cnt < r.cnt;
102     }
103 };
104 
105 inline void DFS(int u)
106 {
107     point[u].ord = ++pos; 
108     point[u].id = u;
109     point[u].ans = 0; 
110     insert(pos, arr[u]); 
111     ford[pos] = u; 
112     vector <Node> vv;    
113     for (int it = head[u]; ~it; it = edge[it].nx)
114     {
115         int v = edge[it].to; 
116         if (v == point[u].fa) continue; 
117         point[v].fa = u; DFS(v);
118         point[u].ans = max(point[u].ans, point[v].ans);
119         vv.emplace_back(v, point[v].son - point[v].ord);   
120     }
121     point[u].son = pos; 
122     int l = point[u].ord, r = point[u].son; 
123     if (l == r) return; 
124     if (l + 1 == r) 
125     {
126         point[u].ans = arr[ford[l]] ^ arr[ford[r]];
127         return;
128     }
129     point[u].ans = max(point[u].ans, Find(l - 1, r, arr[u])); 
130     sort(vv.begin(), vv.end());     
131     for (int i = 0, len = vv.size(); i < len - 1; ++i) 
132     {
133         int it = vv[i].id;
134         int L = point[it].ord, R = point[it].son;  
135         for (int j = L; j <= R; ++j)
136         {
137             point[u].ans = max(point[u].ans, Find(l - 1, r, arr[ford[j]]));
138         }
139     }    
140 }
141 
142 int main()
143 {
144     while (scanf("%d", &n) != EOF)
145     {
146         Init(); 
147         memset(root, 0, sizeof root);
148         tot = 0; 
149         for (int i = 1; i <= n; ++i)
150             scanf("%d", arr + i);
151         for (int i = 1, u, v; i < n; ++i)
152         {
153             scanf("%d%d", &u, &v);
154             addedge(u, v); 
155         }
156         DFS(1);
157         for (int i = 1; i <= n; ++i) printf("%d%c", point[i].ans, " 
"[i == n]);
158     }
159     return 0;
160 }
View Code

 

D:String

留坑。

 

E:Dirt

留坑。

 

F:Poker

水。

技术分享图片
 1 #include <bits/stdc++.h>
 2  
 3 using namespace std;
 4  
 5 #define N 100010
 6  
 7 int n;
 8 int arr[N];
 9 int brr[N];
10 int a[N];
11  
12 inline void Init()
13 {
14     memset(a, 0, sizeof a);
15 }
16  
17 inline int lowbit(int x)
18 {
19     return x & (-x);
20 }
21  
22 inline void update(int x, int val)
23 {
24     for (int i = x; i <= n; i += lowbit(i))
25         a[i] += val;
26 }
27  
28 inline int sum(int x)
29 {
30     int ans = 0;
31     for (int i = x; i > 0; i -= lowbit(i))
32         ans += a[i];
33     return ans;
34 }
35  
36 inline bool check(int mid, int emp)
37 {
38     int tot = sum(mid);
39     return tot <= emp;
40 }
41  
42 int main()
43 {
44     while (scanf("%d", &n) != EOF)
45     {
46         Init();
47         for (int i = 1; i <= n; ++i) update(i, 1);
48         for(int i = 1; i <= n; ++i)
49         {
50             scanf("%d",&arr[i]);
51         }
52         for (int i = 1; i <= n; ++i)
53         {
54             int index = (int)floor(sqrt(n - i + 1));
55             int l = 1, r = n, id;
56             while (r - l >= 0)
57             {
58                 int mid = (l + r) >> 1;
59                 int tot = sum(mid);
60                 if (tot == index)
61                     id = mid;
62                 if (tot >= index)
63                     r = mid - 1;
64                 else
65                     l = mid + 1;
66             }
67             brr[id] = arr[i];
68             update(id, -1);
69         }
70         for (int i = 1; i <= n; ++i) printf("%d%c", brr[i], " 
"[i == n]);
71     }
72     return 0;
73 }
View Code

 

技术分享图片
 1 #include<bits/stdc++.h>
 2 
 3 using namespace std;
 4 
 5 #define N 100010
 6 
 7 int n;
 8 int arr[N];
 9 int brr[N];
10 
11 int main()
12 {
13     while(~scanf("%d",&n))
14     {
15         for(int i = 1; i <= n; ++i)
16         {
17             scanf("%d",&arr[i]);
18         }
19         vector<int>vec;
20         for(int i = n; i >= 1;--i)
21         {
22             int tmp = floor(sqrt(n - i + 1));
23             vec.insert(vec.begin() + tmp - 1, arr[i]);
24         }
25         for(int i = 0; i < n; ++i)
26         {
27             printf("%d%c",vec[i]," 
"[i == n - 1]);
28         }
29     }
30     return 0;
31 }
View Code

 

以上是关于BUCT20180814邀请赛的主要内容,如果未能解决你的问题,请参考以下文章

BUCT2020春季学期ACM周赛-11

BUCT - 2021-2022-1 ACM集训队每周程序设计竞赛题解

BUCT - 2021-2022-1 ACM集训队每周程序设计竞赛题解

BUCT - 2021-2022-1 ACM集训队每周程序设计竞赛题解

BUCT - 2021-2022-1 ACM集训队每周程序设计竞赛(10)题解

BUCT - 2021-2022-1 ACM集训队每周程序设计竞赛(10)题解