1、vector
简单的用,在头插可能退化到,用时722MS
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; const ll MOD = 1e9 + 7; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int N = 1e5 + 7; vector<int> v; int main() { int n = read(), m = read(); while (m--) { int op = read(), x; if (op == 1) x = read(), v.insert(v.begin(), x); else if (op == 2) v.erase(v.begin()); else if (op == 3) x = read(), v.push_back(x); else if (op == 4) v.pop_back(); else if (op == 5) reverse(v.begin(), v.end()); else if (op == 7) sort(v.begin(), v.end()); else { printf("%d\n", 1 * v.size()); for (auto it = v.begin(); it != v.end(); ++it) printf("%d%c", *it, " \n"[it == v.end() - 1]); } } return 0; }
2、手动数组
用时222MS,
#include <bits/stdc++.h> #pragma GCC optimize(2) #pragma GCC optimize(3) using namespace std; #define js ios::sync_with_stdio(false);cin.tie(0); cout.tie(0) typedef long long ll; const ll MOD = 1e9 + 7; inline ll read() { ll s = 0, w = 1; char ch = getchar(); while (ch < 48 || ch > 57) { if (ch == '-') w = -1; ch = getchar(); } while (ch >= 48 && ch <= 57) s = (s << 1) + (s << 3) + (ch ^ 48), ch = getchar(); return s * w; } inline void write(ll x) { if (!x) { putchar('0'); return; } char F[200]; ll tmp = x > 0 ? x : -x; if (x < 0)putchar('-'); int cnt = 0; while (tmp > 0) { F[cnt++] = tmp % 10 + '0'; tmp /= 10; } while (cnt > 0)putchar(F[--cnt]); } inline ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : x; } ll qpow(ll a, ll b) { ll ans = 1; while (b) { if (b & 1) ans *= a; b >>= 1; a *= a; } return ans; } ll qpow(ll a, ll b, ll mod) { ll ans = 1; while (b) { if (b & 1)(ans *= a) %= mod; b >>= 1; (a *= a) %= mod; }return ans % mod; } inline int lowbit(int x) { return x & (-x); } const int N = 4e5 + 7; int a[N]; int main() { int n = read(), m = read(); int l = 200001, r = 200001; //[l,r)这个地方效仿一下STL while (m--) { int op = read(), x; if (op == 1) x = read(), a[--l] = x; else if (op == 2) ++l; else if (op == 3) x = read(), a[r++] = x; else if (op == 4) --r; else if (op == 5) reverse(a + l, a + r); else if (op == 6) { printf("%d\n", r - l); for (int i = l; i < r; ++i) printf("%d%c", a[i], " \n"[i + 1 == r]); } else sort(a + l, a + r); } return 0; }