2021年9月第23次CCF-CSP认证Java版本题解

题解

第一题暴力,第二题不用三分直接暴力,待补充。

代码

第一题

import java.util.*;

public class csp202109_01 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();

        int minSum = 0, maxSum = 0, tempMax = -1;
        for (int i = 0; i < len; i++){
            int num = sc.nextInt();
            maxSum += num;
            if(num > tempMax) minSum += num;
            // tempMax = Math.max(num, tempMax);
            tempMax = num;
        }
        System.out.println(maxSum);
        System.out.println(minSum);
    }
}

第二题

import java.util.*;

public class csp202109_02 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int len = sc.nextInt();
        int[] nums = new int[len + 2];
        nums[0] = 0;
        nums[len + 1] = 0;
        Map<Integer, LinkedList<Integer>> map = new HashMap<>(); // unsorted keys

        // [1, len]
        for (int i = 1; i < len + 1; i++) {
            nums[i] = sc.nextInt();
            if (nums[i] != 0) {
                LinkedList<Integer> temp;
                if (map.containsKey(nums[i])) {
                    temp = map.get(nums[i]);
                } else {
                    temp = new LinkedList<>();
                }
                temp.add(i);
                map.put(nums[i], temp);
            }
        }

        // sorted keys
        LinkedList<Integer> keys = new LinkedList<>(map.keySet());
        Collections.sort(keys);

        // scan
        int countInterval = getInterval(nums);
        int max = countInterval;
        for(int key : keys){
            LinkedList<Integer> list = map.get(key);
            for (int index : list) {
                // set zero
                nums[index] = 0;
                // change the current countInterval
                if ((nums[index - 1] != 0) && (nums[index + 1] != 0)) {
                    countInterval++;
                } else if (nums[index - 1] == 0 && nums[index + 1] == 0) {
                    countInterval--;
                } // else{ 0a1 1a0 does not matter}
            }
            max = Math.max(countInterval, max);
        }

        System.out.println(max);
    }

    private static int getInterval(int[] nums) {
        int count = 0;
        int lenNums = nums.length;
        for (int i = 0; i < lenNums; i++) {
            if (nums[i] == 0) {
                continue;
            } else {
                count++;
            }
            while (i < lenNums && nums[i] != 0) i++;
        }

        return count;
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,CSP-J2021复赛有两道目,分别是分糖果和小熊的果篮。 对于第一分糖果,目来源是CCF,难度为入门。根据给出的代码,这是一个基于循环的算法,通过遍历[l,r]区间内的数,计算数对n取模后的最大值。具体的实现细节可以参考引用中的代码。这道目属于入门级别,比较简单。 第二是关于小熊的果篮。给定一个长度为n的数组a,其中连续的相同元素被视为一个块,要求按照块的顺序输出每个块的头元素,并删除已输出的元素。具体的实现细节可以参考引用中的代码。这道目需要使用双链表来处理,时间复杂度为O(n)。 综上所述,CSP-J2021复赛的目包括分糖果和小熊的果篮,具体的解思路和代码实现可以参考上述引用内容。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [[CSP-J 2021]比赛题解](https://blog.csdn.net/weixin_56550385/article/details/126811201)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [新鲜出炉的 CSP-J 2021 复赛题解](https://blog.csdn.net/qq_23109971/article/details/121024436)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值