解题报告
比赛链接
F.循环移位
原题链接
题目大意:首先我们定义以下概念

现在给你n个整数,你必须分别做一次$L(x),R(y),x \ne y$,问最大的异或和是多少
解:
我们令$res=a_1 \land a_2 \land… \land a_n$,假设我选取了$i,j分别做L(i),R(j)$那么我的答案就是$res \land x_i \land L(i) \land x_j\land R(j)$,我们再令$ls(i)=x_i\land L(i),rs(j)=x_j\land R(j)$,则我的答案又可以写做$res \land ls(i) \land rs(j)$
那么此时我们可以考虑把$rs(i)$全部存放在trie中,枚举$res\land ls(i)$的值,在trie中找最大的异或值
特别需要注意的是,由于$i \ne j$所以在找最大异或值的时候需要先把$ls(i)$从trie中删除
代码如下,特别注意其中的remove操作。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
| struct Node { bool isEnd; int cnt; vector<Node *> children; Node() : isEnd(false), cnt(0) { children.resize(2); children[0] = children[1] = nullptr; } };
class Trie { public: Node* root; Trie() { root = new Node(); } void insert(int x) { Node *u = root; u->cnt += 1; for (int i = 30; i >= 0; i--) { int tmp = (x >> i) & 1; if (u->children[tmp] == nullptr) u->children[tmp] = new Node(); u = u->children[tmp]; u->cnt += 1; } u->isEnd = true; } void remove(int x) { Node *u = root; for (int i = 30; i >= 0; i--) { int tmp = (x >> i) & 1; u->cnt -= 1; u = u->children[tmp]; } } int query(int x) { Node *u = root; int ans = 0; for (int i = 30; i >= 0; i--) { int tmp = (x >> i) & 1; if (u->children[tmp ^ 1] == nullptr || u->children[tmp ^ 1]->cnt == 0) { u = u->children[tmp]; continue; } ans |= (1 << i); u = u->children[tmp ^ 1]; } return ans; } }; Trie tr;
int L(int x) { return ((x ^ (1 << __lg(x))) << 1) | 1; }
int R(int x) { return ((x >> 1) | ((x & 1) << __lg(x))); }
void solve() { int n = read(), xornums = 0; vector<int> a(n + 1), l(n + 1), r(n + 1); for (int i = 1; i <= n; i++) { a[i] = read(); xornums ^= a[i]; l[i] = a[i] ^ L(a[i]); r[i] = a[i] ^ R(a[i]); tr.insert(r[i]); } int ans = 0; for (int i = 1; i <= n; i++) { int tmp = xornums ^ l[i]; tr.remove(r[i]); ans = max(ans, tr.query(tmp)); tr.insert(r[i]); } printf("%d\n", ans); }
|
J.大炮运输
原题链接
解:一道很显然的dp,我们可以设$dp[i]$表示到达i号炮台的最小代价,显然,我们有如下转移方程$dp[i]=\min_{j=1}^{i-1}{(dp[j]+a_j(i-j)^3+b_j)}$,但是这个转移方程的时间复杂度是$O(n^2)$,所以我们尝试减少复杂度。
有什么情况跳着走永远比一步一步跳代价要大呢,假设我是一步一步跳的,有$cost1=\sum_j^{i-1}(a[i]+b[i])$。
如果我是跳着走的,有$cost2=a[j]\times (i-j)^3+b[j]$ 。
根据数据范围$a[i],b[i] \le 1e5$,有$cost1\le 2e5\times(i-j)$,$cost2 \ge (i-j)^3$,联立有$(i-j)^3 \ge 2e5\times(i - j)$,化简得,$i-j\ge\sqrt{2e5} = 500$,即转移的范围不会超过500。