Directory
1 Problem Description
2 Solutions
1 problem description Problem DescriptionThe first minute, God said, "have a question." And then there's l,y,m,c.
for the first two minutes, Lyc said, "There is a vector." So there's a vector with a length of n full of random integers.
the first three minutes, Yuhch said: To have a query. So there is a Q query, the minimum value of the element in the interval of the query vector
the first four minutes, Mzc said: Limited. And then there's the data range.
The first five minutes, CS said: To have to do the problem. Say the gods hubbub, and leave you to pick up the problem .Input Formattwo positive integers n and Q for the first row, indicating the length of the vector and the number of queries
The next line of n integers, in turn, corresponds to the elements in the vector: a[0],a[1],...,a[n-1]
The next Q line, two positive integers per line lo,hi, represents the minimum value in the query interval [lo, hi], which is min (a[lo],a[lo+1],..., A[hi]). output FormatThe total Q line, in turn, corresponds to the result of each query, that is, the minimum value of the vector in the corresponding query interval. Sample Input7 4
1-1-4 8 1 2-7
0 0
1 3
4 5
0 6Sample Output1
-4
1
-7Sample DescriptionThe first query [0,0] means min{a[0]}=min{1}=1
The second query [1,3] means min{a[1],a[2],a[3]}=min{-1,-4,8}=-4
The third query [4,5] means min{a[4],a[5]}=min{1,2}=1
the fourth query [0,6] means querying the entire vector, seeking min{a[0..6]}=min{1,-1,-4,8,1,2,-7}=-7data size and conventions1<=n<=1984,1<=q<=1988, the absolute value of a random integer in a vector does not exceed
2 Solutions
The specific code is as follows:
ImportJava.util.Scanner; Public classMain {//Gets the array subscript of the smallest element in array a subscript z between A~b Public intGetminarray (int[] A,intAintb) {intMin =A; for(inti = A;i <= b;i++) { if(A[min] >a[i]) min=i; } returnmin; } Public Static voidMain (string[] args) {main test=NewMain (); Scanner in=NewScanner (system.in); intn =In.nextint (); intQ =In.nextint (); int[] A =New int[n]; int[] result =New int[Q]; for(inti = 0;i < n;i++) A[i]=In.nextint (); for(inti = 0;i < q;i++) { intA =In.nextint (); intb =In.nextint (); Result[i]=Test.getminarray (A, A, b); } //Output Results for(inti = 0;i < q;i++) System.out.println (A[result[i]); }}
Algorithm note _105: Blue Bridge cup practice algorithm to improve God's problem five minutes (Java)