很裸的单调队列问题,不过O(n)的算法写出来5188+ms,超5s了。。。谁能告诉我500+ms的神级代码是什么。。.T_T
My Code:
#include#include #include using namespace std; const int N = 1000005; struct node { int i; int num; }q[N]; int ans[N]; int b[N]; int main() { //freopen("data.in", "r", stdin); int n, k, i, f, r; while(~scanf("%d%d", &n, &k)) { getchar(); for(i = 0; i < n; i++) scanf("%d", &b[i]); for(f = r = i = 0; i < n; i++) { if(f < r && q[f].i <= i - k) f++; while(f < r && q[r-1].num >= b[i]) r--; q[r].i = i; q[r].num = b[i]; r++; ans[i] = q[f].num; } for(i = k-1; i < n; i++) { printf("%d", ans[i]); if(i != n-1) putchar(''); else putchar('\n'); } for(f = r = i = 0; i < n; i++) { if(f < r && q[f].i <= i - k) f++; while(f < r && q[r-1].num <= b[i]) r--; q[r].i = i; q[r].num = b[i]; r++; ans[i] = q[f].num; } for(i = k-1; i < n; i++) { printf("%d", ans[i]); if(i != n-1) putchar(''); else putchar('\n'); } } return 0; }