跳到正文

个人记录

solution-chaynOI-R1

chaynflow约 10 分钟
原文
文章信息与版本

文章信息

发布
2023年9月12日
收录
2025年12月12日
更新
2026年11月8日
篇幅
约 10 分钟

版本

  1. v1当前

膜拜 @Nicrobot rk1

bx

第三小的。

O(n)O(n)。

C++
#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

分类讨论。

记 mm 为 ss 中的字符种数,MM 为最大次数。

若 k=0k=0 或 M≤⌈n2⌉M \le \lceil \dfrac{n}{2}\rceil,显然答案为 ma+(n−m)bma+(n-m)b。

否则答案要加上 (2M−n−1)(c−b)(2M-n-1)(c-b)。

O(n)O(n)。

C++
#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

简单背包。

记 pip_i 为 kik_i 中质因子 22 的个数。

则构造一部分的 jj 使得 ∑bj=∑i=1mpi\sum b_j = \sum_{i=1}^{m} p_i,这些 jj 用 f,其他用 F 即可。

O(n∑pi)O(n\sum p_i)。

C++
#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。

记 fl,r,L,Rf_{l,r,L,R} 为 al∼r,bL∼Ra_{l\sim r},b_{L\sim R} 范围建成一棵树的答案。

首先,若 al≠bRa_l \ne b_R,答案 =0=0。

否则假设 al+1a_{l+1} 在 bkb_k,记 len=k−L+1len = k-L+1。

若 al+1∼l+lena_{l+1 \sim l+len} 与 bL∼kb_{L \sim k} 有不同的元素(无顺序),答案 =0=0。

否则若 k=Rk=R,则说明剩下的随便插在 ala_l 的左/右字树都可以,fl,r,L,R=fl+1,r,L,R−1×2f_{l,r,L,R}=f_{l+1,r,L,R-1}\times2。

其余情况,al+1∼l+lena_{l+1 \sim l+len} 插在左,al+len+1∼ra_{l+len+1 \sim r} 插在右。fl,r,L,R=fl+1,l+len,L,k×fl+len+1,r,k+1,Rf_{l,r,L,R}=f_{l+1,l+len,L,k}\times f_{l+len+1,r,k+1,R}。

复杂度分析:假设一共 kk 层,每次都要 O(n)O(n) 判定是否数字集相同,总复杂度 O(nk)O(nk),极端是 O(n2)O(n^2)。

参考代码(造数据使用的):

C++
#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. 每层卡满 n2\dfrac{n}{2},显然此时只有 log⁡n\log n 层。

  2. 层数卡满 n2\dfrac{n}{2},显然每一层判断变为 O(1)O(1)。

对于一般情况,复杂度介于 O(n)∼O(nlog⁡n)O(n) \sim O(n \log n) 之间。

具体留给读者自己实现。

对于更低的复杂度,考虑使用哈希,对每一个数字随机分配权值,若两段权值和相同则大概率数字集相同,数据没有特意卡,可以通过。

C++
#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)O(Tn)。

我不会告诉你不判定数字集是否相同有80pts

写在最后

总体难度,红/橙-/黄+/绿。

感谢报名&阅读,我们会做得更好。

原文
存档评论1 条抓取于