minimum
Time Limit:
1000 ms | Memory Limit: 65535 KB
Difficulty:
3
- describe
-
Input N numbers, M queries.
Each query gives a number x.
Requirement: Each query outputs the i-th smallest number among the first x numbers. ( i is the ith query)
You can assume M <= N, Xi <= Xi+1 <= Xi+2 <= …. <= Xm (Xm <= N).
- enter
-
Line0:T
Line1: N,M
Line2…LineN+1:num1,......,numN
LineN+2…LineN+2+M:x1,……,xM
N < 30000, num < 2000000000
- output
-
Each query outputs the small number before i, on a separate line.
Please refer to the sample for detailed format. - sample input
-
1 7 4 3 1 -4 2 8 -1000 2 1 2 6 6
- Sample output
-
3 3 1 2
/** Analysis: The question is, sort the first m numbers in ascending order, and then output the value of P[pos] Note: Do not sort the original array directly **/
C/C++ code implementation:
#include <iostream> #include <algorithm> #include <cstring> #include <cmath> #include <cstdio> #include <stack> #include <queue> #include <map> using namespace std; int A[30005], B[30005]; int main () { int T; scanf ("%d", &T); while (T --) { int N, M, a; scanf ("%d%d", &N, &M); for (int i = 0; i < N; ++ i) scanf ("%d", &A[i]); for (int i = 1; i <= M; ++ i) { scanf ("%d", &a); for (int j = 0; j < a; ++ j) { B[j] = A[j]; } sort (B, B + a, less <int>()); printf ("%d\n", B[i - 1]); } } return 0; }