const int maxn = 300000 + 10; const int inf = 0x3f3f3f3f; int n, m, S[maxn], q[maxn];
void solve() { int l = 1, r = 1, ans = -inf; q[1] = 0; for (int i = 1; i <= n; i++) { while (l <= r && i - q[l] > m) l++; ans = max(ans, S[i] - S[q[l]]); while (l <= r && S[q[r]] >= S[i]) r--; q[++r] = i; } printf("%d\n", ans); }
int main() { freopen("input.txt", "r", stdin); cin >> n >> m; for (int i = 1; i <= n; i++) { int x; scanf("%d", &x); S[i] = S[i-1] + x; }
const int maxn = 200000 + 10; const int inf = 0x3f3f3f3f; typedef pair<int, int> PII; PII a[maxn]; int n;
void solve() { sort(a, a+n);
int last = inf, dir = -1, ans = 1; for (int i = 0; i < n; ) { int j = i; while (j+1 < n && a[j].first == a[j+1].first) j++; // [i, j] same value
int minv = a[i].second, maxv = a[j].second; if (dir == -1) { if (maxv < last) last = minv; else last = maxv, dir = -dir; } else { if (minv > last) last = maxv; else { ans++; last = minv; dir = -dir; } } i = j+1; } printf("%d\n", ans); }
void init() { // }
int main() { freopen("input.txt", "r", stdin);
// get data cin >> n; for (int i = 0; i < n; i++) { scanf("%d", &a[i].first); a[i].second = i; }
// solve solve(); }
链表:数组模拟
链表用数组模拟,需要全局维护两个值
l(i),r(i)i表示数组下标,l(i),r(i)分别表示左,右两个位置
特别地,可以这么理解数组模拟
L[p]=q看成是一个作用,将p这个位置,左边连到qR[p]=q同理,将p这个位置,右边连到q
1 2 3 4 5 6 7 8 9 10
void init() { for (int i = 1; i <= n; i++) { L[i] = i-1, R[i] = i+1; } } // remove i for (int i = n; i >= 1; i--) { int left = L[i], right = R[i]; L[right] = left, R[left] = right; }
int L[maxn], R[maxn], mp[maxn]; for (int i = 1; i <= n; i++) { L[i] = i-1, R[i] = i+1; mp[a[i].second] = i; }
for (int id = n; id > 1; id--) { int i = mp[id], left = L[i], right = R[i]; ll lval = abs(a[left].first - a[i].first); ll rval = abs(a[right].first - a[i].first);