20240806
Minimum MEX
用双指针,如果 \(mex\) 小于了就往大扩展
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int t, n, a[N], cnt[N], ans;
void Solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
if (a[i] <= n) {
cnt[a[i]]++;
}
}
int p;
for (int i = 0; i <= n + 1; i++) {
if (!cnt[i]) {
p = i;
break;
}
}
ans = 1e9;
fill(cnt, cnt + n + 2, 0);
for (int i = 1, j = 1, tmp = 0; i <= n; i++) {
while (j <= n && tmp < p) {
tmp += (!cnt[a[j]] && a[j] < p);
if (a[j] < p) {
cnt[a[j]]++;
}
j++;
}
if (tmp == p) {
ans = min(ans, j - i);
}
if (a[i] < p) {
cnt[a[i]]--;
if (!cnt[a[i]]) {
tmp--;
}
}
}
if (p == 0) {
cout << "1\n";
return ;
}
cout << ans << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
Solve();
}
return 0;
}
Range Contradiction
将数组排序,然后我们可以得出数组 \(a\) 与 \(b\) 中一定有一个为连续的,那么即可暴力求解
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1e5 + 5;
int t, n, a[N], ans[N];
void Solve() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
sort(a + 1, a + n + 1);
int p = 1, mini = (a[n / 2] - a[1]) * (a[n] - a[n / 2 + 1]);
for (int i = 2; i + n / 2 <= n; i++) {
if (mini > (a[n / 2 + i - 1] - a[i]) * (a[n] - a[1])) {
p = i;
mini = (a[n / 2 + i - 1] - a[i]) * (a[n] - a[1]);
}
}
for (int i = p; i <= p + n / 2 - 1; i++) {
ans[(i - p) * 2 + 1] = a[i];
}
int tot = 2;
for (int i = 1; i < p; i++) {
ans[tot] = a[i];
tot += 2;
}
for (int i = p + n / 2; i <= n; i++) {
ans[tot] = a[i];
tot += 2;
}
for (int i = 1; i <= n; i++) {
cout << ans[i] << " ";
}
cout << '\n';
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
Solve();
}
return 0;
}
/*
1
10
5 6 1 5 1 5 8 4 7 1
*/
Not a Segment Tree
考虑将反向操作,将 \(b\) 还原回 \(a\),那么如果 \(b_i\)的值大于\(b_{i - k} +...+ b_{i + k} - b_i\),那么 \(b_i\) 一定将被还原,可以使用优先队列,因为如果最大的值都无法还原,那么就意味着 \(b_i\) 无法还原回 \(a_i\)
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 2e6 + 5;
struct node {
long long x, id;
bool operator < (const node _y) const {
return x < _y.x;
}
};
int t, n, k;
long long a[N], b[N], tr[N * 4], ans;
void build(int i, int l, int r) {
if (l == r) {
tr[i] = b[l];
return ;
}
int mid = (l + r) >> 1;
build(i * 2, l, mid);
build(i * 2 + 1, mid + 1, r);
tr[i] = tr[i * 2] + tr[i * 2 + 1];
}
int query(int i, int l, int r, int x, int y) {
if (r < x || l > y) {
return 0;
}
if (l >= x && r <= y) {
return tr[i];
}
int mid = (l + r) >> 1;
return query(i * 2, l, mid, x, y) + query(i * 2 + 1, mid + 1, r, x, y);
}
int query(int l, int r) {
if (l > r) {
return 0;
}
return query(1, 1, n, max(1ll, l), min(n, r));
}
void modify(int i, int l, int r, int p, int x) {
if (l == r) {
tr[i] = x;
return ;
}
int mid = (l + r) >> 1;
if (mid >= p) {
modify(i * 2, l, mid, p, x);
}
else modify(i * 2 + 1, mid + 1, r, p, x);
tr[i] = tr[i * 2] + tr[i * 2 + 1];
}
void Solve() {
priority_queue<node> q;
cin >> n >> k;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
q.push({b[i], i});
}
build(1, 1, n);
ans = 0;
while (!q.empty() && k) {
node cur = q.top();
q.pop();
long long sum = 0;
sum += query(cur.id - k, cur.id + k) - b[cur.id];
sum += query(1, cur.id + k - n);
sum += query(n - (k - cur.id + 1) + 1, n);
if (!sum) {
continue;
}
long long p = (b[cur.id] - a[cur.id]) / sum;
ans += p;
b[cur.id] -= p * sum;
modify(1, 1, n, cur.id, b[cur.id]);
if (p) {
q.push({b[cur.id], cur.id});
}
}
for (int i = 1; i <= n; i++) {
if (b[i] != a[i]) {
cout << "-1\n";
return ;
}
}
cout << ans << "\n";
}
signed main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> t;
while (t--) {
Solve();
}
return 0;
}