- chenxueze's blog
vector
- @ 2026-3-1 18:52:32
vector 动态数组 push 1个元素,自动扩容2个元素的空间
扩容空间以双倍递增,倍增
vector<int> b; b.push_back(456) b.push_back(456)
[4]
优先队列
默认大顶堆
priority_queue<int, vector<int>,greater<int>>; // 小顶堆
#include<bits/stdc++.h>
using namespace std;
#defineIOSios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = 100010;
int main() {
int n;cin >> n;
vector<int> a(n + 1);
for (int i = 1;i <= n; i++) cin>>a[i];
sort(a.begin(),a.end);
n.push_back(1);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
struct node {
int d,p;
};
bool cmp(node a,node b) {
return a.d < b.d;
}
int main() {
int n;
cin >> n;
vector<node> b(n + 1);
for (int i = 1;i <= n; i++) cin >> b[i].d >> b[i].p;
sort(b.begin() + 1,b.end(),cmp);
priority_queue<int,vector<int>,greater<int>> p;
for (int i = 1;i <= n; i++) {
int dr = b[i].d;
int pr = b[i].p;
if (p.size() < dr) {
p.push(pr);
}else if (!p.empty() && pr > p.top()) { // 比较报酬是否比堆头小
p.pop(); // 弹出
p.push(pr); //将报酬设为堆头
}
}
long long tot = 0;
while (!p.empty()) {
tot += p.top();
p.pop();
}
cout << tot;
return 0;
}