(一)线性表
1
#include <bits/stdc++.h>
using namespace std;int main() {int n;multiset<int> ms;cin >> n;for(int i = 1;i <= n;i ++) {int x;cin >> x;ms.insert(x);}int x;cin >> x;ms.insert(x);for(int i:ms) cout << i << ",";return 0;
}
2
#include <bits/stdc++.h>
using namespace std;int main() {int n;map<int , int> mp;set<int> s;cin >> n;for(int i = 1;i <= n;i ++) {int x , y;cin >> x >> y;s.insert(y);mp[y] += x;}cin >> n;for(int i = 1;i <= n;i ++) {int x , y;cin >> x >> y;s.insert(y);mp[y] += x;}if(s.size() == 0) {cout << "0 0";return 0;}bool flag = 0;for(auto it = s.end();;) {it --;int i = *it;if(mp[i] != 0) {flag = 1;cout << mp[i] << ' ' << i << ' ';}if(it == s.begin()) break;}if(flag == 0) cout << 0 << ' ' << 0;return 0;
}
3
#include <bits/stdc++.h>
using namespace std;int main() {int n;cin >> n;vector<int>a(n + 1);for(int i = 1;i <= n;i ++) cin >> a[i];for(int i = n;i >= 1;i --) cout << a[i] << ' ';return 0;
}
4
#include <bits/stdc++.h>
using namespace std;const int N = 1e6 + 100;
int pre[N] , lst[N] , a[N];void del(int x) {int p = pre[x] , l = lst[x];pre[l] = p;lst[p] = l;
}void solve() {int n;cin >> n;for(int i = 1;i <= n;i ++) cin >> a[i];for(int i = 2;i < n;i ++) {pre[i] = i - 1;lst[i] = i + 1;}pre[1] = n;lst[1] = 2;lst[n] = 1;pre[n] = n - 1;int now = 1 , syg;for(int i = 1;i <= 5;i ++) now = lst[now];cout << now << " ";syg = a[now];del(now);n --;while(n --) {while(syg --) now = lst[now];syg = a[now];del(now);cout << now << ' ';}cout << "\n";
}int main() {int t;cin >> t;while(t --) solve();return 0;
}
(二)堆栈
1
#include <bits/stdc++.h>
#define int long long
using namespace std;bool solve() {string s;cin >> s;int len = s.size();s = " " + s;stack<char> st;for(int i = 1;i <= len;i ++) {if(s[i] == '(' || s[i] == '[' || s[i] == '{') {st.push(s[i]);} else {if(s[i] == ')') {if(st.top() == '(') st.pop();else return false;}if(s[i] == ']') {if(st.top() == '[') st.pop();else return false;}if(s[i] == '}') {if(st.top() == '{') st.pop();else return false;}}}if(st.size() == 0) return true;return false;
}signed main() {ios::sync_with_stdio(false);cin.tie(0) , cout.tie(0);cout << (solve()?"True":"False");return 0;
}
2
#include <bits/stdc++.h>
#define int long long
using namespace std;const int N = 50;
char ch[N] = {'0' , '1' , '2' , '3' , '4' , '5' , '6' , '7' , '8' , '9' , 'A' , 'B' , 'C' , 'D' , 'E' , 'F'};void work(int x,int c) {if(x == 0) return ;work(x / c , c);cout << ch[x % c];
}signed main() {ios::sync_with_stdio(false);cin.tie(0) , cout.tie(0);int n , m;cin >> n >> m;if(n == 0) cout << 0;elsework(n , m);return 0;
}
3
#include <bits/stdc++.h>using namespace std;#define SZ(x) ((int)((x).size()))
#define lb(x) ((x) & (-(x)))
#define bp(x) __builtin_popcount(x)
#define bpll(x) __builtin_popcountll(x)
#define mkp make_pair
#define pb push_back
#define fi first
#define se second
#define int long long
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, int> pli;
typedef pair<ll, ll> pll;
typedef pair<double, int> pdi;ll ksm(ll x, ll y) {ll res = 1;while (y) {if (y & 1) {res = res * x;}x = x * x;y >>= 1;}return res;
}void solve() {string s;cin >> s;int cur = 0;vector<pii> a;for (int i = 0; i < SZ(s); i++) {char c = s[i];if (isdigit(c)) {cur = cur * 10 + (c - '0');if (i == SZ(s) - 1 || !isdigit(s[i + 1])) {a.push_back({cur, 0});cur = 0;}} else {a.push_back({c, 1});}}stack<int> num;stack<char> opt;map<char, int> mp;mp['+'] = mp['-'] = 1;mp['*'] = mp['/'] = 2;mp['^'] = 3;mp['('] = 0;auto calc = [&](int x, int y, char c) -> int {if (c == '+') {return x + y;} else if (c == '-') {return x - y;} else if (c == '*') {return x * y;} else if (c == '/') {return x / y;} else if (c == '^') {return ksm(x, y);} else {assert(false);}return 114514;};int n = SZ(a);for (int i = 0; i < n; i++) {if (a[i].se == 0) {num.push(a[i].fi);} else {char c = a[i].fi;if (c == '(') {opt.push(c);} else if (c == ')') {while (true) {char op = opt.top();opt.pop();if (op == '(') {break;}int y = num.top(); num.pop();int x = num.top(); num.pop();int res = calc(x, y, op);num.push(res);}} else {if (opt.empty()) {opt.push(c);} else if (mp[c] > mp[opt.top()]) {opt.push(c);} else {while (!opt.empty() && mp[opt.top()] >= mp[c]) {int op = opt.top();opt.pop();int y = num.top(); num.pop();int x = num.top(); num.pop();int res = calc(x, y, op);num.push(res);}opt.push(c);}}}}while (!opt.empty()) {int op = opt.top();opt.pop();int y = num.top(); num.pop();int x = num.top(); num.pop();int res = calc(x, y, op);num.push(res);}assert(SZ(num) == 1);cout << num.top() << '\n';
}signed main() {ios::sync_with_stdio(false);cin.tie(0), cout.tie(0);int T = 1;while (T--) solve();return 0;
}