蓝桥杯 19717 挖矿java

java 复制代码
import java.util.Scanner;

public class Main {
    public static void solve() {
        Scanner sc = new Scanner(System.in);
        
        int n = sc.nextInt();
        int m = sc.nextInt();
        
        int s = 0;
        int[] l = new int[m + 1];
        int[] r = new int[m + 1];
        //初始化,没有矿就是0
        for (int i = 0; i < n; ++i) {
            int x = sc.nextInt();
            if (Math.abs(x) <= m && x < 0) {
                l[-x]++;
            } else if (Math.abs(x) <= m && x > 0) {
                r[x]++;
            } else if (x == 0) {
                s++;
            }
        }
        //计算前缀和
        for (int i = 1; i <= m; ++i) {
            l[i] += l[i - 1];
            r[i] += r[i - 1];
        }
        
        int ans = Math.max(r[m], l[m]);//不调转方向的数量
        for (int i = 1; i <= m / 2; ++i) {
            int sr = r[i] + l[m - i * 2];
            int sl = l[i] + r[m - i * 2];
            ans = Math.max(ans, Math.max(sr, sl));
        }
        
        ans += s;
        System.out.println(ans);
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        int t = 1;
        while (t-- > 0) {
            solve();
        }
        
        sc.close();
    }
}
相关推荐
LJHclasstore_luo7 分钟前
【题解】WebGoC 102683.大雨过后
算法·goc编程
你怎么知道我是队长9 分钟前
JavaScript的介绍
开发语言·javascript·ecmascript
MrZhao4009 分钟前
Agent 如何持续工作:任务持久化、后台执行与定时唤醒
算法
用户7017207390011 分钟前
密码学之分组密码
算法
江华森18 分钟前
04-python-面向对象
开发语言·python
MrZhao40021 分钟前
Agent 长上下文处理机制:Context Compact 与 Memory 的协同
算法
AI行业学习21 分钟前
Notepad++ 官方纯净下载+完整安装教程(Windows)【2026.7.5】
开发语言·windows·python·前端框架·html·notepad++
要开心吖ZSH22 分钟前
处方物流信息同步优化:从 36 秒到亚秒级的踩坑记录
java·数据库·mysql·性能优化
Wang's Blog23 分钟前
JavaWeb快速入门: MyBatis入门与实战
java·mybatis
h_a_o777oah30 分钟前
【动态规划】区间 DP :三层循环逻辑与模板实现细节(洛谷 P1880)
c++·算法·动态规划·acm·区间dp·化环为链·石子合并