/* Time limit: 5000ms Memory limit: 256mb ---------------------Copy the following code, complete it and submit--------------------- I, <Your Full Name>, am submitting the assignment for an individual project. I declare that the assignment here submitted is original except for source material explicitly acknowledged, the piece of work, or a part of the piece of work has not been submitted for more than one purpose (i.e. to satisfy the requirements in two different courses) without declaration. I also acknowledge that I am aware of University policy and regulations on honesty in academic work, and of the disciplinary guidelines and procedures applicable to breaches of such policy and regulations, as contained in the University website http://www.cuhk.edu.hk/policy/academichonesty/. It is also understood that assignments without a properly signed declaration by the student concerned will not be graded by the teacher(s). */ #include "stdio.h" #include "stdlib.h" #include "string.h" void solve(int* a, int n, int k, int* b) { // WRITE YOUR CODE HERE } // DO NOT MODIFY THE CODE BELOW int main(void) { int n, k; scanf("%d %d", &n, &k); int* a = (int*)malloc(n * sizeof(int)); for (int i = 0; i < n; i++) { scanf("%d", &a[i]); } int* b = (int*)malloc(n * sizeof(int)); solve(a, n, k, b); for (int i = 0; i < n; i++) { printf("%d ", b[i]); } printf("\n"); free(b); free(a); return 0; } ---------------------------------------End of Code--------------------------------------- ## Minimum Value in a Sliding Window (Bonus Problem) Give an array of $n$ integers and a window size $k$, for each element in the array, find the minimum value of $k$ elements in the front of it (including itself). If the number of elements in the front of it is less than $k$, then start from the first element. Formally, given an array $a$ of length $n$ and an integer $k$, calculate an array $b$ of length $n$ such that $b[i] = \min \{a[j] | j \in [\max(0, i-k+1), i]\}$ for each $i \in [0, n)$. - **Note1:** There truly exists a naive solution with time complexity $O(nk)$, but it is not efficient enough for this problem. You need to find a solution with time complexity $O(n)$. - **Note2:** Deque is a useful data structure for this problem. ### Input The first line contains two integers $n$ and $k$ $(1 \leq n \leq 10^6, 1 \leq k \leq n)$, which are separated by a space. The second line contains $n$ integers $a_1, a_2, \ldots, a_n$ $(-10^9 \leq a_i \leq 10^9)$. ### Output A single line containing $n$ integers $b_1, b_2, \ldots, b_n$, which are separated by a space. ### Example **Input** ``` 5 3 3 -1 2 4 5 ``` **Output** ``` 3 -1 -1 -1 2 ``` **Explanation** - $b[0] = \min\{a[0]\} = \min{3} = 3$ - $b[1] = \min\{a[0], a[1]\} = \min\{3, -1\} = -1$ - $b[2] = \min\{a[0], a[1], a[2]\} = \min\{3, -1, 2\} = -1$ - $b[3] = \min\{a[1], a[2], a[3]\} = \min\{-1, 2, 4\} = -1$ - $b[4] = \min\{a[2], a[3], a[4]\} = \min\{2, 4, 5\} = 2$