T1
题目大意
有 $n$ 个数,每次选择两个数 $a$ 和 $b$ 合并,合并代价为 $a \times b$,合并后得到新数 $a + b$。重复操作直到只剩一个数,求最小总代价。如果 $op=1$ 还需要求出达到最小代价的方案数(对 $10^9+7$ 取模)。
思路
其实这道题是一个诈骗题。
这道题如果去模拟样例就会发现,这道题和顺序没啥关系,无论怎么改变顺序,最终的热度值是一样的。
然后组合计算的话是考试中没想出来的,我们可以按照石子合并的方式,每一次混合其实就是从 $n$ 杯芒果汁中选择任意两杯进行合并,所以就是 $C(n, 2)$,然后芒果汁的数量就减少了一杯,于是接下来就还有 $C(n - 1, 2)$ 这么多种选取可能。
我们只需要将这些值乘起来即可,所以选择的方案就有:
$$
\prod_{i = 2}^{n} C(i, 2)
$$
代码
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 #include <bits/stdc++.h> #define int long long using namespace std;const int mod = 1e9 + 7 ;const int N = 2e4 + 10 ;int T, n, op, a[N];void solve () { cin >> n >> op; for (int i = 1 ; i <= n; i++) cin >> a[i]; int sum = 0 , num = a[1 ], ans = 1 ; for (int i = 2 ; i <= n; i++) { ans = (ans * i * (i - 1 ) / 2 ) % mod; sum = (sum + num * a[i] % mod) % mod; num = (num + a[i]) % mod; } cout << sum << ' ' << op * ans << "\n" ; } signed main () { cin.tie (0 )->sync_with_stdio (0 ); cin >> T; while (T--) { solve (); } return 0 ; }
T2
题目大意
有 $n$ 个同学排成一列,每个同学有一个颜色 $c_i$。如果两个同学颜色不同,则他们之间存在一对亲密关系,亲密度为 $|i-j|$。
将所有亲密关系按以下规则排序:
第一关键字:亲密度(即 $|i-j|$)从小到大
第二关键字:编号较小的同学的编号从小到大
第三关键字:编号较大的同学的编号从小到大
求第 $k$ 对亲密关系(输出两个同学的编号,小在前大在后),如果不存在则输出 $-1$。
思路
这道题以亲密度为第一关键字,于是我们可以先二分亲密度,找到 $k$ 大体在哪一个区间,然后再求解出两个编号。
我们二分出一个亲密值的上届 $x$ ,那么答案就存在于 $x+1$ 这个亲密度里,然后得到了这个值之后,在用双指针来枚举出第 $k$ 对关系就行了。
现在的问题就是二分中的 check 函数如何写,如何计算 $1 \sim mid$ 这个区间内有多少个合法的亲密组合。
我们发现如果正着写的话非常不好写,好像没有办法快速的去枚举出所有的合法亲密度,
正难则反,我们可以先计算出不考虑颜色的方案数,然后减去那些不合法的方案。
统计不合法的方案数可以用一个类似于滑动窗口的东西来统计有多少个相同颜色的方案数,然后减去即可。
代码
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e5 + 10 ;int n, k, a[N];vector<int > num[N]; int check (int x) { int sum = (n - x) * x + x * (x - 1 ) / 2 ; for (int i = 1 ; i <= n; i++) { int l = 0 ; for (int j = 0 ; j < num[i].size (); j++) { while (l < num[i].size () && num[i][j] - num[i][l] > x) l++; sum -= j - l; } } return sum; } signed main () { cin >> n >> k; for (int i = 1 ; i <= n; i++) { cin >> a[i]; num[a[i]].push_back (i); } if (check (n) < k) { cout << -1 ; return 0 ; } int l = -1 , r = n + 1 , ans = n; while (l <= r) { int mid = l + r >> 1 ; if (check (mid) < k) { ans = mid; l = mid + 1 ; } else { r = mid - 1 ; } } int d = ans + 1 ; int cnt = check (ans); for (int i = 1 ; i <= n - d; i++) { if (a[i] != a[i + d]) { cnt++; } if (cnt == k) { cout << i << ' ' << i + d; return 0 ; } } return 0 ; }
T3
题目大意
给定 $t$ 次询问,每次给出一个正整数 $n$。需要找到所有正整数 $x$,使得 $x$ 的所有约数之和等于 $n$。
对于每次询问:
思路
我们可以将 $n$ 分解成唯一的一个分解成一个式子,形似于 $\prod_{i = 1}^{k} a_{i}^{p_i}$。
正约数和定理:假设 $S$ 代表 $n$ 的正约数和
$$
S = \prod_{i = 1}^{k} \sum_{j = 0}^{p_i} a_i^j = (a_1^0 + a_1^1 + \dots + a_1^{p_1}) \times (a_2^0 + a_2^1 + \dots + a_2^{p_2}) \times \dots (a_k^0 + a_k^1 + \dots + a_k^{p_k})
$$
根据这上面的两个公式,我们就可以利用 DFS 来求解即可。
代码
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e6 + 10 ;int T, n;int vis[N], cnt[N];vector<int > v, ans, prime; void init () { vis[0 ] = vis[1 ] = 1 ; for (int i = 2 ; i < 1e6 + 7 ; i++) { if (!vis[i]) { prime.push_back (i); for (int j = i + i; j < 1e6 + 7 ; j += i) { vis[j] = 1 ; } } } } bool check (int x) { if (x <= 1 ) return 0 ; for (int i = 2 ; i * i <= x; i++) { if (x % i == 0 ) return 0 ; } return 1 ; } void dfs (int now, int x, int sum) { if (now == 1 ) { ans.push_back (sum); return ; } if (check (now - 1 ) && now > prime[x]) { ans.push_back (sum * (now - 1 )); } for (int i = x; prime[i] * prime[i] <= now; i++) { int tmp = prime[i]; for (int j = prime[i] + 1 ; j <= now; tmp *= prime[i], j += tmp) { if (now % j == 0 ) { dfs (now / j, i + 1 , sum * tmp); } } } } signed main () { cin.tie (0 )->sync_with_stdio (0 ); init (); cin >> T; while (T--) { cin >> n; ans.clear (); dfs (n, 0 , 1 ); cout << ans.size () << '\n' ; sort (ans.begin (), ans.end ()); if (ans.size ()) { for (int i = 0 ; i < ans.size (); i++) { cout << ans[i] << ' ' ; } cout << '\n' ; } } return 0 ; }
T4
题目大意
给定 $n$ 个区间 $[l_i, r_i]$,你要为每个区间选一个数 $a_i \in [l_i, r_i]$,使得序列 $a_1, a_2, \dots, a_n$ 的最长严格上升子序列长度最大。输出这个最大长度。
思路
我们考虑用平常求解最长上升子序列的方式 $dp_i$,表示以 $a_i$ 作为结尾的最长上升子序列多长。
很明显,这个状态在这道题用不了,因为 $a_i$ 是不断在变动的,所以我们需要修改状态。
我们设 $dp_i$ 表示长度为 $i$ 的最长上升子序列,最后一位最小是多少。
可以得到状态转移方程:
$$
dp_i =
\begin{cases}
\min(dp_i, l) & dp_{i - 1} < l \\
dp_{i - 1} + 1 & l \le dp_{i - 1} < r
\end{cases}
$$
但是如果直接进行转移的话,所需要的时间是 $\mathcal O (n^2)$ 的,所以我们需要优化这个 $dp$ 。
我们发现我们好像无法去优化状态和转移方程了,所以我们只能去优化转移的效率。
发现 $dp$ 数组是一个单调上升不下降的一个序列,于是我们可以利用一个平衡树来优化这个状态转移。
题目中添加一个 $[l, r]$ 可以看做成将 $[l, r)$ 这样一个区间内的所有数值 $+1$ ,删除掉第一个 $\geq r$ 的数。
代码
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 #include <bits/stdc++.h> #define int long long using namespace std;const int N = 1e6 + 10 ;struct node { int l, r, val, key, lazy; } tree[N]; int n, root, l, r, p, cnt, ans;void push_down (int p) { if (tree[p].lazy) { tree[p].val += tree[p].lazy; tree[tree[p].l].lazy += tree[p].lazy; tree[tree[p].r].lazy += tree[p].lazy; tree[p].lazy = 0 ; } } void split (int u, int x, int &l, int &r) { if (!u) { l = r = 0 ; return ; } push_down (u); if (tree[u].val < x) { l = u; split (tree[u].r, x, tree[u].r, r); } else { r = u; split (tree[u].l, x, l, tree[u].l); } } int add (int x) { cnt++; tree[cnt].val = x; tree[cnt].key = rand (); return cnt; } int merge (int l, int r) { if (!l || !r) return l + r; if (tree[l].key < tree[r].key) { push_down (l); tree[l].r = merge (tree[l].r, r); return l; } else { push_down (r); tree[r].l = merge (l, tree[r].l); return r; } } void del (int &x) { if (tree[x].l) del (tree[x].l); else x = tree[x].r; } void insert (int x, int y) { split (root, x, l, p); split (p, y, p, r); tree[p].lazy++; if (r) del (r); else ans++; root = merge (l, merge (add (x), merge (p, r))); } signed main () { srand (time (0 )); cin.tie (0 )->sync_with_stdio (0 ); cin >> n; for (int i = 1 , l, r; i <= n; i++) { cin >> l >> r; insert (l, r); } cout << ans; return 0 ; }