比赛:
20+0+0+0
????????????
T1
赛时想的贪心是l从小到大排,对于一个l,有能匹配的就匹配
爆了,20
正解实际上是在此基础上加上一点贪心,当一个区间无法匹配时,可以找一个已匹配的r<当前r的区间进行替换,吧替换出来的区间变成备选
#include <bits/stdc++.h>
using namespace std;
long long n, cnt[400005], pos, ans, ls, tr[400005], lls;
priority_queue<long long> q1, q2;
struct sd {
long long l, r, id;
} q[400005], p[400005];
long long cmp(sd A, sd B) {
if (A.l != B.l)
return A.l < B.l;
else
return A.r > B.r;
}
int main() {
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
cin >> n;
for (long long i = 1; i <= n; i++) {
scanf("%lld%lld", &q[i].l, &q[i].r);
}
sort(q + 1, q + n + 1, cmp);
for (int i = 1; i <= n; i++) {
if (!q1.empty() && -q1.top() < q[i].l) {
ans++;
q1.pop();
q2.push(-q[i].r);
continue;
}
if (!q2.empty() && -q2.top() < q[i].r) {
q1.push(q2.top());
q2.pop();
q2.push(-q[i].r);
continue;
}
q1.push(-q[i].r);
}
cout << ans;
return 0;
}
T2
不会讲,自己看代码吧
#include <bits/stdc++.h>
using namespace std;
long long n, a[3005], flag, ls[3005], cnt, ll, rr, ls2[3005], top, x, num[3005];
vector<int> Left, Right;
mt19937 rnd(114514);
void S(int l, int r) {
if (l > r)
return;
cnt++;
Left.push_back(l);
Right.push_back(r);
top = l;
for (int j = l + 1; j <= r; j += 2) {
ls2[j] = ls[top];
top++;
}
for (int j = l; j <= r; j += 2) {
ls2[j] = ls[top];
top++;
}
for (int j = l; j <= r; j++) {
ls[j] = ls2[j];
}
}
int main() {
freopen("sort.in", "r", stdin);
freopen("sort.out", "w", stdout);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> num[i];
}
if (n == 1) {
cout << 0;
return 0;
}
while (1) {
Left.clear();
Right.clear();
cnt = 0;
for (int i = 1; i <= n; i++) {
ls[num[i]] = i;
}
for (int i = 1; i <= 114; i++) {
ll = (rnd()) % n + 1;
rr = (rnd()) % n + 1;
if (ll <= rr) {
S(ll, rr);
} else {
S(rr, ll);
}
}
// for(int i=1; i<=n; i++) {
// cout<<ls[i]<<" ";
// }
// cout<<endl;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= n; j++) {
if (ls[j] == i) {
x = j;
break;
}
}
// cout<<x<<endl;
if (x == i)
continue;
while (x * 2 <= i) {
S(1, x * 2);
// for(int i=1; i<=n; i++) {
// cout<<ls[i]<<endl;
// }
x *= 2;
}
if (x < i)
S(2 * x - i + 1, i);
// for(int j=1; j<=n; j++) {
// cout<<ls[j]<<" ";
// }
// cout<<endl;
}
if (cnt <= 6000) {
cout << cnt << endl;
for (int i = cnt - 1; i >= 0; i--) {
cout << Left[i] << " " << Right[i] << endl;
}
return 0;
}
}
return 0;
}
(或者可以看看ZGC的讲解)