3.栈,优先队列,map,set,bitset 3.栈,优先队列,map,set,bitset 栈 123456stack<int> stk; // 栈的定义,括号内为数据类型stk.empty() // 当栈内为空,返回truestk.top() // 返回栈顶元素stk.push() // 将括号内的元素压入栈中stk.pop() // 将栈顶元素弹出 12345678910111213141516171819202122232425262728//www.starrycoding.com/problem/38#include<bits/stdc++.h>using namespace std;const int N = 1e5+9;using ll = long long;ll a[N];int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int n;cin >> n; for(int i = 1;i <= n;++i) cin >> a[i]; stack<int> stk; int pos = 1; for(int i = 1;i <= n;++i) { while(pos <= n && ( stk.empty() || stk.top() != i ) ) stk.push(a[pos++]); if(stk.top() == i)stk.pop(); else { cout << "No" << '\n'; return 0; } } cout << "Yes" << '\n';} 2.优先队列 123456789101112131415priority_queue<ll> pq //优先队列的定义,括号内为参数类型,默认是大根堆pq.top() //返回优先队列的顶部元素pq.pop() //弹出优先队列的顶部元素pq.push() //将括号内的元素压入优先队列 priority_queue<ll,vector<ll>,greater<ll> > pq //换为小根堆,从大到小另一种切换为小根堆的方法:struct cmp{ bool operator()(const ll &u, const ll & v)const { return u > v; }}priority_queue<ll,vector<ll>,greater<ll> > pq 123456789101112131415161718192021222324252627282930//www.starrycoding.com/problem/58#include<bits/stdc++.h>using namespace std;const int N = 1e5+9;using ll = long long;ll a[N];int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int q;cin >> q; priority_queue<ll> pq; ll sum = 0; while(q--) { int n;cin >> n; if(n == 1) { ll x;cin >> x; pq.push(x); sum+=x; } else if(!pq.empty()) { sum -= pq.top(); pq.pop(); } } cout << sum << '\n';} 3.map 1234567891011121314151617181920212223242526#include<bits/stdc++.h>using namespace std;int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); int T;cin >> T; map<string, int> mp; vector<string> v; while(T--) { map<string, int> mp; vector<string> v; int n;cin >> n; for(int i = 1;i <= n;++i) { string s;cin >> s; if(mp.count(s)) mp[s]++; else{ v.push_back(s); mp[s] = 1; } } for(auto &i : v) cout << i << ' ' << mp[i] << '\n'; }} 4.set 123456789101112131415#include<bits/stdc++.h>using namespace std;using ll = long long;int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); set<ll> st; ll n;cin >> n; for(int i = 1;i <= n;++i) { ll x;cin >> x; st.insert(x); } for(auto &i : st) cout << i << ' ';} 5.bitset 12345678910111213141516171819#include<bits/stdc++.h>using namespace std;using ll = long long;const int N = 5e3+9, M = 5e5+9;ll a[N];int main(){ ios::sync_with_stdio(0),cin.tie(0),cout.tie(0); ll n;cin >> n; bitset<M> bt; bt[0] = 1; for(int i= 1;i <= n;++i)cin >> a[i]; for(int i = 1;i <= n;++i) { bt |= (bt << a[i]); } cout << bt.count() << '\n';} C++ > 算法 #C++ #算法 3.栈,优先队列,map,set,bitset https://hainoir.github.io/posts/aa3f72d6.html 作者 hainoir 发布于 2026年2月5日 许可协议 React 上一篇 2.位运算双指针排序二分 下一篇