版本比较
MXOI题解记录
正文
增加 130 行删除 0 行
604 行未改动全部展开605606607608604 行未改动全部展开return 0;}```::::604 行未改动全部展开605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738604 行未改动全部展开return 0;}```::::## P14971 『GTOI - 2B』DDoS难度:普及/提高-(中位)### 分析&解析题解做法:首先,可以使用全部时间,进行 1 次操作。但可用代价可能不够,所以需要**去除一些间隔**,使得代价不大于 m;所以,去除**长度更长**的区间更优。### $Code:$::::info[$Code:$]```cpp#include <bits/stdc++.h>using namespace std;int n, m, mn = 1e9, mx;int jian[1000005];struct node {int l, r;} a[1000005];bool cmp1(node x, node y) {return x.l < y.l;}bool cmp2(int x, int y) {return x > y;}int main() {ios::sync_with_stdio(0);cin.tie(0), cout.tie(0);cin >> n >> m;for (int i = 1; i <= n; ++i) {cin >> a[i].l >> a[i].r;mn = min(mn, a[i].l);mx = max(mx, a[i].r);}sort(a + 1, a + n + 1, cmp1);int sz = 0;int nowr = a[1].r;// 处理间隔for (int i = 2; i <= n; ++i) {if (a[i].l > nowr + 1)jian[++sz] = a[i].l - nowr - 1, nowr = a[i].r;elsenowr = max(nowr, a[i].r);}if (mx - mn + 1 <= m) {cout << 1;return 0;}int sy = mx - mn + 1 - m;int ans = 1;sort(jian + 1, jian + sz + 1, cmp2);for (int i = 1; i <= sz; ++i) {sy -= jian[i];++ans;if (sy <= 0) {cout << ans;return 0;}}cout << -1;return 0;}```::::## CF1777C Quiz Master难度:普及/提高-(上位)### 分析&解析观察可以发现,我们选取的人一定是一个连续的区间。所以我们可以用双指针(Two-pointer)算法来解决这个问题。本问题需要求出每个数的因数,所以我们可以预处理($n log_2 n$)出来每个数的因数。双指针过程中,用数组 $vis_i$ 表示在 $[l,r]$ 的区间中因数 $i$ 的出现次数,$cnt$ 表示不同的因数的个数。每次循环在 $r < n$ 且 $cnt < m$ 的情况下将 $r$ 加一,并把 $a_r$ 的因数加到 $vis$ 中。如果 $cnt = m$ 就对目前的区间取极差 $(a_r - a_l)$ 做最小值。然后因为 $l$ 要加一,所以要把 $a_l$ 的因数从 $vis$ 中减去。### $Code:$::::info[$Code:$]```cpp#include <iostream>#include <vector>#include <algorithm>using namespace std;const int M = 1e5, N = 1e5 + 10;int t, n, m, cnt, ans, a[N], vis[N];vector<int> v[N];int main(){ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);cin >> t;for (int i = 1; i <= M; i++)for (int j = i; j <= M; j += i)v[j].push_back(i);while (t--){cin >> n >> m;ans = M;for (int i = 1; i <= n; i++)cin >> a[i];sort(a + 1, a + n + 1);for (int l = 1, r = 0; l <= n; l++){while (r < n && cnt < m){r++;for (int x : v[a[r]]){if (x <= m){if (!vis[x])cnt++;vis[x]++;}}}if (cnt == m)ans = min(ans, a[r] - a[l]);for (int x : v[a[l]]){if (x <= m){vis[x]--;if (!vis[x])cnt--;}}}cout << (ans < M ? ans : -1) << endl;}return 0;}```::::