solution-chaynOI-R1
chaynflow
·
·
个人记录
膜拜 @Nicrobot rk1
bx
第三小的。
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int n, v;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n;
int a = 2e9, b = 2e9, c = 2e9;
for (int i = 1; i <= n; i++)
{
cin >> v;
if (v < a) c = b, b = a, a = v;
else if (v < b) c = b, b = v;
else if (v < c) c = v;
}
cout << c;
return 0;
}
```
## print
分类讨论。
记 $m$ 为 $s$ 中的字符种数,$M$ 为最大次数。
若 $k=0$ 或 $M \le \lceil \dfrac{n}{2}\rceil$,显然答案为 $ma+(n-m)b$。
否则答案要加上 $(2M-n-1)(c-b)$。
$O(n)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 1e6 + 5;
int n, k, a, b, c, maxx;
string s;
int d[26];
long long ans;
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> k >> a >> b >> c >> s; n = s.size();
for (int i = 0; i < n; i++)
{
if (d[s[i]-'a']) ans += b;
else ans += a;
d[s[i]-'a']++; maxx = max(maxx, d[s[i]-'a']);
}
if (!k || maxx <= (n + 1) / 2) return cout << ans, 0;
int num = n - maxx, cnum = maxx - 1 - num;
cout << ans + 1ll * cnum * (c - b);
return 0;
}
```
## function
简单背包。
记 $p_i$ 为 $k_i$ 中质因子 $2$ 的个数。
则构造一部分的 $j$ 使得 $\sum b_j = \sum_{i=1}^{m} p_i$,这些 $j$ 用 `f`,其他用 `F` 即可。
$O(n\sum p_i)$。
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
int n, m, k, b[N], ans;
void fj(int x)
{
while (x % 2 == 0) ans++, x /= 2;
}
int p[N * 5];
bool f[N * 5];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> n >> m;
for (int i = 1; i <= m; i++) cin >> k, fj(k);
f[0] = 1;
for (int i = 1; i <= n; i++)
{
cin >> b[i];
for (int j = ans; j >= b[i]; j--)
if (!f[j] && f[j - b[i]])
p[j] = i, f[j] = 1;
}
string s(n, 'F');
for (int num = ans; num; num = num - b[p[num]])
s[p[num] - 1] = 'f';
cout << s;
return 0;
}
```
## rebuild
不咋像 dp。
记 $f_{l,r,L,R}$ 为 $a_{l\sim r},b_{L\sim R}$ 范围建成一棵树的答案。
首先,若 $a_l \ne b_R$,答案 $=0$。
否则假设 $a_{l+1}$ 在 $b_k$,记 $len = k-L+1$。
若 $a_{l+1 \sim l+len}$ 与 $b_{L \sim k}$ 有不同的元素(无顺序),答案 $=0$。
否则若 $k=R$,则说明剩下的随便插在 $a_l$ 的左/右字树都可以,$f_{l,r,L,R}=f_{l+1,r,L,R-1}\times2$。
其余情况,$a_{l+1 \sim l+len}$ 插在左,$a_{l+len+1 \sim r}$ 插在右。$f_{l,r,L,R}=f_{l+1,l+len,L,k}\times f_{l+len+1,r,k+1,R}$。
复杂度分析:假设一共 $k$ 层,每次都要 $O(n)$ 判定是否数字集相同,总复杂度 $O(nk)$,极端是 $O(n^2)$。
参考代码(造数据使用的):
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 3005, mod = 1e8 + 7;
bitset <N> A, B;
int t, n, a[N], b[N], p[N];
int f(int l, int r, int L, int R)
{
if (a[l] != b[R]) return 0;
if (l == r) return 1;
int pos = p[a[l + 1]];
if (pos == R - 1) return f(l + 1, r, L, R - 1) * 2 % mod;
A.reset(); B.reset();
int len = pos - L + 1;
for (int i = l + 1; i <= l + len; i++) A[a[i]] = 1;
for (int i = L; i <= pos; i++) B[b[i]] = 1;
if (A != B) return 0;
return 1ll * f(l + 1, l + len, L, pos) * f(l + len + 1, r, pos + 1, R - 1) % mod;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i], p[b[i]] = i;
cout << f(1, n, 1, n) << "\n";
}
return 0;
}
```
显然的优化:每次选择较短的一半判定是否合法。虽然看上去没有用,不过我们可以来分析两个极端情况:
1. 每层卡满 $\dfrac{n}{2}$,显然此时只有 $\log n$ 层。
2. 层数卡满 $\dfrac{n}{2}$,显然每一层判断变为 $O(1)$。
对于一般情况,复杂度介于 $O(n) \sim O(n \log n)$ 之间。
具体留给读者自己实现。
对于更低的复杂度,考虑使用哈希,对每一个数字随机分配权值,若两段权值和相同则大概率数字集相同,数据没有特意卡,可以通过。
```cpp
#include <bits/stdc++.h>
using namespace std;
const int N = 200005, mod = 1e8 + 7;
int t, n, a[N], b[N], p[N], h[N], H[N];
int f(int l, int r, int L, int R)
{
if (a[l] != b[R]) return 0;
if (l == r) return 1;
int pos = p[a[l + 1]];
if (pos == R - 1) return f(l + 1, r, L, R - 1) * 2 % mod;
int len = pos - L + 1;
long long hh = h[l + len] - h[l];
long long HH = H[pos] - H[L - 1];
if (hh != HH) return 0;
return 1ll * f(l + 1, l + len, L, pos) * f(l + len + 1, r, pos + 1, R - 1) % mod;
}
long long key[N];
void calc_hash()
{
for (int i = 1; i <= n; i++) h[i] = h[i - 1] + key[a[i]];
for (int i = 1; i <= n; i++) H[i] = H[i - 1] + key[b[i]];
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
random_device seed;
mt19937 r(seed());
uniform_int_distribution <long long> P(1, 1e13);
for (int i = 1; i <= 200000; i++) key[i] = P(r);
cin >> t;
while (t--)
{
cin >> n;
for (int i = 1; i <= n; i++) cin >> a[i];
for (int i = 1; i <= n; i++) cin >> b[i], p[b[i]] = i;
calc_hash();
cout << f(1, n, 1, n) << "\n";
}
return 0;
}
```
$O(Tn)$。
~~我不会告诉你不判定数字集是否相同有80pts~~
## 写在最后
总体难度,红/橙-/黄+/绿。
感谢报名&阅读,我们会做得更好。