20250723 总结
_little_duck_ · · 个人记录
C.Gym 102961T
题意
给定
思路
对于每对时间,按开始时间排序。用优先队列维护结束时间(小根堆)。处理每对时间时,考虑优先队列中有多少个结束时间小于当前开始时间,如果这样,则记录答案弹出元素。因为要求输出方案,所以可以用 set 维护当前有那些空余点位,当元素被弹出时,释放点位到 set;当压入时,从 set 中选取点位,并更新 set。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 2e5 + 10;
int n, ans[N], mx;
struct P{
int a, b, id;
}a[N];
struct Node{
int t, id, p;
bool operator < (const Node &i) const{
return t > i.t;
}
};
priority_queue<Node> q;
set<int> s;
bool cmp(const P &a, const P &b){
return a.a < b.a;
}
int main(){
cin >> n;
for(int i = 1; i <= n; i++){
cin >> a[i].a >> a[i].b;
a[i].id = i;
}
sort(a + 1, a + 1 + n, cmp);
for(int i = 1; i <= n; i++){
while(!q.empty() && q.top().t < a[i].a){
ans[q.top().p] = q.top().id;
s.insert(q.top().id);
q.pop();
}
if(q.size() == mx){
++mx;
q.push({a[i].b, mx, a[i].id});
}
else{
q.push({a[i].b, *s.begin(), a[i].id});
s.erase(s.begin());
}
}
while(!q.empty()){
ans[q.top().p] = q.top().id;
mx = max(mx, q.top().id);
q.pop();
}
cout << mx << "\n";
for(int i = 1; i <= n; i++){
cout << ans[i] << " ";
}
return 0;
}
D.洛谷 P7913
题意
有
思路
对于国内航班和国外航班,分别做一遍 cnt1 表示国内航班前缀和,cnt2 表示国际航班前缀和):
最后输出答案。
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m1, m2, cnt1[N], cnt2[N], mx1, mx2, ans;
struct Node{
int t, id, p;
bool operator < (const Node &i) const{
return t > i.t;
}
};
struct P{
int a, b, id;
}a[N], b[N];
bool cmp(const P &a, const P &b){
return a.a < b.a;
}
priority_queue<Node> q1, q2;
set<int> s1, s2;
int main(){
cin >> n >> m1 >> m2;
for(int i = 1; i <= m1; i++){
cin >> a[i].a >> a[i].b;
a[i].id = i;
}
for(int i = 1; i <= m2; i++){
cin >> b[i].a >> b[i].b;
b[i].id = i;
}
sort(a + 1, a + 1 + m1, cmp);
sort(b + 1, b + 1 + m2, cmp);
for(int i = 1; i <= m1; i++){
while(!q1.empty() && q1.top().t < a[i].a){
cnt1[q1.top().id]++;
s1.insert(q1.top().id);
q1.pop();
}
if(q1.size() == mx1){
++mx1;
q1.push({a[i].b, mx1, a[i].id});
}
else{
q1.push({a[i].b, *s1.begin(), a[i].id});
s1.erase(s1.begin());
}
}
while(!q1.empty()){
cnt1[q1.top().id]++;
mx1 = max(mx1, q1.top().id);
q1.pop();
}
for(int i = 1; i <= n; i++){
cnt1[i] += cnt1[i - 1];
}
for(int i = 1; i <= m2; i++){
while(!q2.empty() && q2.top().t < b[i].a){
cnt2[q2.top().id]++;
s2.insert(q2.top().id);
q2.pop();
}
if(q2.size() == mx2){
++mx2;
q2.push({b[i].b, mx2, b[i].id});
}
else{
q2.push({b[i].b, *s2.begin(), b[i].id});
s2.erase(s2.begin());
}
}
while(!q2.empty()){
cnt2[q2.top().id]++;
mx2 = max(mx2, q2.top().id);
q2.pop();
}
for(int i = 1; i <= n; i++){
cnt2[i] += cnt2[i - 1];
}
for(int i = 0; i <= n; i++){
ans = max(ans, cnt1[i] + cnt2[n - i]);
}
cout << ans;
return 0;
}
B.AtCoder abc306_e
题意
给定一个长度为
思路
用两个 set,一个维护前 set。
代码
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N = 5e5 + 10;
int n, k, q, a[N], sum;
multiset<int> l, r;
signed main(){
cin >> n >> k >> q;
for(int i = 1; i <= k; i++){
l.insert(0);
}
for(int i = k + 1; i <= n; i++){
r.insert(0);
}
for(int i = 1, x, y; i <= q; i++){
cin >> x >> y;
if(l.find(a[x]) != l.end()){
sum -= a[x];
l.erase(l.find(a[x]));
a[x] = y;
r.insert(y);
sum += *r.rbegin();
l.insert(*r.rbegin());
r.erase(r.find(*r.rbegin()));
}
else{
r.erase(r.find(a[x]));
a[x] = y;
r.insert(y);
if(*l.begin() < *r.rbegin()){
sum += *r.rbegin() - *l.begin();
int tl = *l.begin(), tr = *r.rbegin();
l.erase(l.begin());
r.erase(r.find(*r.rbegin()));
l.insert(tr);
r.insert(tl);
}
}
cout << sum << "\n";
}
return 0;
}