Blue Bridge Cup learning programming on the eighth day

Tarsus query interval k

Resource limitation
time limit: 1.0s memory limit: 256.0MB
problem description
to a given sequence, each of the query sequence to the r number of the number l of K in which a large number.

Input format
The first line contains a number n, the sequence length.

The second line contains n positive integer representing a given sequence.

The third contains a positive integer m, the number indicating an inquiry.

Next m lines of three numbers l, r, K, from left to right represents the query sequence to the number of the l-r in number, from large to small number of large K is which. Reference numeral sequence elements from the beginning.

Output formats
total output m lines, each a number representing the answer to the inquiry.
Sample input
. 5
. 1. 5. 4. 3 2
2
. 1. 5 2
2 2. 3
sample output
. 4
2
data size and conventions
for 30% of data, n, m <= 100;

To 100% of the data, n, m <= 1000;

Ensure k <= (r-l + 1), the sequence number <= 106.

Problem-solving ideas:
I direct method using bubbling number between l and r sort, to the final output rk

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt(), b;
        int aa[] = new int[a];
        for (int i = 0; i < a; i++)
            aa[i] = sc.nextInt();
        b = sc.nextInt();
        int bb[][] = new int[b][3];
        for (int i = 0; i < b; i++)
            for (int j = 0; j < 3; j++)
                bb[i][j] = sc.nextInt();
        for(int i=0;i<b;i++)
            System.out.println(fun(bb[i][0],bb[i][1],bb[i][2],aa));

    }
    static int fun(int l,int r,int k,int[] b){
        int t;
        int[] a=new int[b.length];
        for(int i=l-1;i<r;i++) {
            a[i]=b[i];
            for (int j = l-1; j < r; j++) {
                if (a[i] < a[j]) {
                    t = a[i];
                    a[i] = a[j];
                    a[j] = t;
                }
            }
        }
        return a[r-k];
    }
}
Released nine original articles · won praise 0 · Views 97

Guess you like

Origin blog.csdn.net/weixin_42616905/article/details/104518779