51nod-加农炮

本文介绍了如何模拟加农炮射击过程,考虑地形高度数组A和炮弹高度数组B,当地形高度大于等于炮弹高度时,炮弹会被挡住。讨论了初始的直接遍历方法及其效率问题,并提出了利用二分查找优化的解决方案。
摘要由CSDN通过智能技术生成

一个长度为M的正整数数组A,表示从左向右的地形高度。测试一种加农炮,炮弹平行于地面从左向右飞行,高度为H,如果某处地形的高度大于等于炮弹飞行的高度H(A[i] >= H),炮弹会被挡住并落在i - 1处,则A[i - 1] + 1。如果H <= A[0],则这个炮弹无效,如果H > 所有的A[i],这个炮弹也无效。现在给定N个整数的数组B代表炮弹高度,计算出最后地形的样子。
例如:地形高度A = {1, 2, 0, 4, 3, 2, 1, 5, 7}, 炮弹高度B = {2, 8, 0, 7, 6, 5, 3, 4, 5, 6, 5},最终得到的地形高度为:{2, 2, 2, 4, 3, 3, 5, 6, 7}。

模拟题,刚开始的直接模拟整个的过程,但是直接超市,第一次写的代码

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) throws IOException {

        Scanner cin = new Scanner(System.in);
        // BufferedReader reader = new BufferedReader(new
        // InputStreamReader(System.in), 1 << 16);
        // BufferedWriter writer = new BufferedWriter(new
        // OutputStreamWriter(System.out), 1 << 16);
        //
        // String s = null;
        // String[] ss = null;
        // s = reader.readLine();
        // ss = s.split(" ");

        int m = cin.nextInt();
        int n = cin.nextInt();
        int[] a = new int[m];
        int[] b = new int[n];
        // HashMap<Integer, PriorityQueue<Integer>> site = new
        // HashMap<>();//对应数值到位置的映射函数
        // PriorityQueue<Integer> minQ = new PriorityQueue<>();
        // PriorityQueue<Integer> maxQ = new PriorityQueue<>(new
        // Comparator<Integer>() {
   
        //
        // @Override
        // public int compare(Integer o1, Integer o2) {
   
        // return o2 - o1;
        // }
        // });
        ArrayList<Integer> left = new ArrayList<>();
        ArrayList<Integer> right = new ArrayList<>();
        // ArrayList<PriorityQueue<Integer>>
        int pre = 0, next = 0;
        a[0] = cin.nextInt();
        for (int i = 1; i < a.length; ++i) {
            a[i] = cin.nextInt();
            if (a[i] > a[i 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值