贝壳java岗笔试第四题

通过率只有50%
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = sc.nextInt();
        int[][] arr = new int[2 * n][2];
        for (int i = 0; i < 2 * n; i++) {
            arr[i][0] = sc.nextInt();
        }
        for (int i = 0; i < 2 * n; i++) {
            arr[i][1] = sc.nextInt();
        }
        int left = n - 1;
        int right = n;
        int flag = 1;
        int res = 1;
        while (left >= 0 && right < 2 * n){
            if(arr[left][1] - arr[left][0] > arr[right][1] - arr[right][0]){
                if(res <= arr[left][0]){
                    flag += arr[left][0] - res + 1;
                    res = arr[left][1] + 1;
                }else {
                    res = res - arr[left][0] + arr[left][1];
                }
                left--;
            }else {
                if(res <= arr[right][0]){
                    flag += arr[right][0] - res + 1;
                    res = arr[right][1] + 1;
                }else {
                    res = res - arr[right][0] + arr[right][1];
                }
                right++;
            }
        }
        while (left >= 0){
            if(res <= arr[left][0]){
                flag += arr[left][0] - res + 1;
                res = arr[left][1] + 1;
            }else {
                res = res - arr[left][0] + arr[left][1];
            }
            left--;
        }
        while (right < 2 * n){
            if(res <= arr[right][0]){
                flag += arr[right][0] - res + 1;
                res = arr[right][1] + 1;
            }else {
                res = res - arr[right][0] + arr[right][1];
            }
            right++;
        }
        System.out.println(flag);
    }
}

#贝壳找房##笔试题目#
全部评论

相关推荐

点赞 1 评论
分享
牛客网
牛客企业服务