数列描述

  • 【题目描述】
  • 给定两个长度为 n 的整数数列 A 和 B。再给定 q 组查询,每次查询给出两个整数 x 和 y,
  • 求满足 Ai >= x 且 Bi >= y 这样的 i 的数量。
  • 输入格式
  • 第一行给定两个整数 n 和 q。
  • 第二行给定数列 A,包含 n 个整数。
  • 第三行给定数列 B,包含 n 个整数。
  • 接下来 q 行,每行两个整数 x 和 y,意义如上所述。
  • 输出格式
  • 对于每组查询,输出所求的下标数量。
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        scanner.useDelimiter("\n");
        boolean isLoop = true;
        String[] nq = new String[2];
        while (isLoop) {
            String next = scanner.next();
            nq = next.split(" ");
            if (nq.length != 2) {
                System.out.println("输入的参数有误,请重新输入,不得超过两位");
            } else {
                isLoop = false;
            }
        }
        List<String> listAB = new ArrayList<>();
        String strAB;
        while (listAB.size() < 2) {
            strAB = scanner.next();
            String[] split = strAB.split(" ");
            if (split.length == Integer.parseInt(nq[0])) {
                listAB.add(strAB);
            } else {
                System.out.println("输入的数列元素数量有误");
            }
        }
        List<String> list = new ArrayList<>();
        int num = Integer.parseInt(nq[1]);
        while (num > 0) {
            list.add(scanner.next());
            num--;
        }
        new Main().answer(listAB, list);
    }
    /**
     * @param listAB 给定的数列AB,以集合的形式呈现
     * @param list   查询的q个队列
     */
    public void answer(List<String> listAB, List<String> list) {
        //获取两个数列
        String strA = listAB.get(0);
        String strB = listAB.get(1);
        String[] arrA = strA.split(" ");
        String[] arrB = strB.split(" ");
        for (String string : list) {
            String[] splitArr = string.split(" ");
            int count = 0;
            for (int j = 0; j < arrA.length; j++) {
                if (Integer.parseInt(arrA[j]) >= Integer.parseInt(splitArr[0])
                        && Integer.parseInt(arrB[j]) >= Integer.parseInt(splitArr[1])) {
                    count++;
                }
            }
            System.out.println(count);
        }
    }

}

全部评论

相关推荐

迷茫的大四🐶:那你问他上班之后老实了没
点赞 评论 收藏
分享
评论
点赞
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务