模版题,就是维护一个合法区间的最值。C++写的莫名的TLE了, 换成C就过了。
#include <stdio.h> #define maxn 1010000 struct Element { int pos, val; }maxque[maxn], minque[maxn]; int maxhead,maxtail; int minhead,mintail; int n, k; int A[maxn],B[maxn]; int main() { // freopen("in.txt","r",stdin); // freopen("out.txt","w",stdout); int i, cur, val; while(~scanf("%d%d",&n,&k) ) { maxhead = maxtail = 0; minhead = mintail = 0; cur = 0; for (i = 0;i < k;++i) { scanf("%d",&val); while(maxhead < maxtail && maxque[maxtail - 1].val <= val) maxtail--; maxque[maxtail].val = val; maxque[maxtail].pos = i; maxtail++; while(minhead < mintail && minque[mintail - 1].val >= val) mintail--; minque[mintail].val = val; minque[mintail].pos = i; mintail++; } for (i = k;i < n;++i) { A[cur] = maxque[maxhead].val; B[cur] = minque[minhead].val; cur++; scanf("%d",&val); while(maxhead < maxtail && i - maxque[maxhead].pos >= k) maxhead++; while(maxhead < maxtail && maxque[maxtail - 1].val <= val) maxtail--; maxque[maxtail].val = val; maxque[maxtail].pos = i; maxtail++; while(minhead < mintail && i - minque[minhead].pos >= k) minhead++; while(minhead < mintail && minque[mintail - 1].val >= val) mintail--; minque[mintail].val = val; minque[mintail].pos = i; mintail++; } A[cur] = maxque[maxhead].val; B[cur] = minque[minhead].val; cur++; for (i = 0;i < cur;++i) printf("%d%c", B[i], i == cur - 1?'\n':' '); for (i = 0;i < cur;++i) printf("%d%c", A[i], i == cur - 1?'\n':' '); } return 0; }
#include <stdio.h> #define lson(x) ((x) << 1) #define rson(x) ((x) << 1 | 1) #define inf 1 << 30 #define INF 0x3f3f3f3f #define maxn 1000010 int a[maxn]; struct Node { int Min, Max; int l, r; }p[maxn*4]; int mid; int max(int x,int y) { return x > y?x:y; } int min(int x,int y) { return x < y?x:y; } void up(int rt) { p[rt].Max = max(p[lson(rt)].Max, p[rson(rt)].Max); p[rt].Min = min(p[lson(rt)].Min, p[rson(rt)].Min); } void build(int rt,int L,int R) { p[rt].l = L, p[rt].r = R; if (L == R) { p[rt].Min = p[rt].Max = a[L]; return ; } //mid = (L + R) >> 1; p[rt].Min = INF; p[rt].Max = 0; build(lson(rt), L, (L+R) >> 1); build(rson(rt), ((L + R) >> 1) + 1, R); up(rt); } int Max, Min; void QueryMax(int rt,int L,int R) { if (L <= p[rt].l && p[rt].r <= R) { Max = max(Max, p[rt].Max); return ; } // mid = (p[rt].l + p[rt].r) >> 1; if (L <= (p[rt].l + p[rt].r) >> 1) QueryMax(lson(rt), L, R); if (R > (p[rt].l + p[rt].r) >> 1) QueryMax(rson(rt), L, R); } void QueryMin(int rt,int L,int R) { if (L <= p[rt].l && p[rt].r <= R) { Min = min(Min, p[rt].Min); return ; } //mid = (p[rt].l + p[rt].r) >> 1; if (L <= (p[rt].l + p[rt].r) >> 1) QueryMin(lson(rt),L,R); if (R > (p[rt].l + p[rt].r) >> 1) QueryMin(rson(rt),L,R); } int main() { // freopen("in.txt","r",stdin); int n, k, i; while(scanf("%d%d",&n,&k) == 2) { for (i = 1;i <= n;++i) scanf("%d",&a[i]); build(1, 1, n); for (i = k;i <= n;++i) { Min = inf; QueryMin(1,i - k + 1,i); printf("%d%c",Min,i==n?'\n':' '); } for (i = k;i <= n;++i) { Max = -inf; QueryMax(1, i - k + 1,i); printf("%d%c", Max,i == n?'\n':' '); } } return 0; }