题解 | 牛客周赛82 Java ABCDEF

目录

题目地址

做题情况

A 题

B 题

C 题

D 题

E 题

F 题

牛客竞赛主页

题目地址

牛客竞赛_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ

做题情况

A 题

判断字符串第一个字符和第三个字符是否相等

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;

    public static void solve() throws IOException {
       String str=sc.next();
       if(str.charAt(0)==str.charAt(2)) {
    	   dduoln("YES");
       }else {
    	   dduoln("NO");
       }
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
//         t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

B 题

找是否出现相同元素

将元素放到 TreeSet 集合 里面

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;
    public static void solve() throws IOException {
        
    	int n=sc.nextInt();
    	TreeSet<Integer>ts=new TreeSet<>();
    	for(int i=0;i<n;i++) {
    		int a=sc.nextInt();
    		ts.add(a);
    	}
    	
    	if(ts.size()!=n) {
    		dduoln("NO");
    		return;
    	}
    	
    	dduoln("YES");
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
//        t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

C 题

在 B 题的基础上进行结构体排序

按照索引大小的规则来排序元素

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;
    public static void solve() throws IOException {
        
    	int n=sc.nextInt();
    	ArrayList<int[]>l=new ArrayList<>();
    	TreeSet<Integer>ts=new TreeSet<>();
    	for(int i=0;i<n;i++) {
    		int a=sc.nextInt();
    		ts.add(a);
    		l.add(new int[] {i+1,a});
    	}
    	
    	if(ts.size()!=n) {
    		dduoln("NO");
    		return;
    	}
    	
    	dduoln("YES");
    	Collections.sort(l,(a,b) -> {
    		return a[1]-b[1];
    	});
    	
    	for(int p[]:l) {
    		dduo(p[0]+" ");
    	}
        
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
//        t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

D 题

首先找规律

我们发现给出的元素只能是递减的

最后一个元素只能是 1

之后我们可以确定的是

如果一个元素与前面这个元素不同 这个数就是确定的 而后面跟这个数相同的数就都是不确定的

我们只需要统计这段重复的数字和可以填入的数字的排列组合就行

其中可选的数是 排列的最大值n - 比重复数字小的数(不能填) -前面有多少数 (注意要把重复的数空下来)

重复的数我们计数出来的

然后组合数 Anm

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;

    public static void solve() throws IOException {
        
    	int n=sc.nextInt();
    	long arr[]=new long[n];
    	for(int i=0;i<n;i++) {arr[i]=sc.nextLong();}
        
    	long ans=arr[0];
    	long cnt=1;
    	
    	long a=0; // 重复的数
    	
    	for(int i=1;i<n;i++) {
    		if(arr[i]>ans) {
    			dduoln("0");
    			return;
    		}
    		if(arr[i]==ans) {
    			a++;
    		}
    		if(arr[i]<ans) {
    			long b= (n-arr[i-1]) -(i-a) +1; // 可选的数
    			if(b<a) {
    				dduoln("0");
        			return;
    			}else if(a!=0&&b!=0){
    				for(long j=b;j>=b-a+1;j--) {
    					cnt*=j;
    					cnt%=MOD;
    				}
    			}
    			ans=arr[i];
    			a=0;
    		}
    	}
    	
    	if(ans!=1) {
    		dduoln("0");
			return;
    	}
    	
    	for(int i=1;i<=a;i++) {
    		cnt*=i;
			cnt%=MOD;
    	}
    	
    	dduoln(cnt);
    	
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
        t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

E 题

使用优先队列来维护状态

优先队列采用的是大顶堆(数值大的元素优先级高)

通过优先队列给数组赋值

对于数组 a 我们维护一个数组

数组的索引i 的值表示的是前 i 个元素最小的 m 个数的和

通过优先队列筛选出前 i 个元素数值大的元素并进行移除

数组 b 反向操作就行

最后跑一遍 n 更新最大值

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;

    public static void solve() throws IOException {
        
        int n = sc.nextInt();
        int m = sc.nextInt();

        long[] a = new long[n+1];
        long[] b = new long[n+1];
        long[] pre = new long[n+1];
        long[] suf = new long[n+1];
        
        for (int i = 1; i <= n; i++) {
            a[i] = sc.nextLong();
        }

        for (int i = 1; i <= n; i++) {
            b[i] = sc.nextLong();
        }

        PriorityQueue<Long> q1 = new PriorityQueue<>(Collections.reverseOrder());
        long sum1 = 0;
        for (int i = 1; i <= n; i++) {
            q1.add(a[i]);
            sum1 += a[i];
            if (q1.size() > m) {
                sum1 -= q1.poll();
            }
            if (q1.size() == m) {
                pre[i] = sum1;
            }
        }

        PriorityQueue<Long> q2 = new PriorityQueue<>(Collections.reverseOrder());
        long sum2 = 0;
        for (int i = n; i >= 1; i--) {
            q2.add(b[i]);
            sum2 += b[i];
            if (q2.size() > m) {
                sum2 -= q2.poll();
            }
            if (q2.size() == m) {
                suf[i] = sum2;
            }
        }

        long ans = Long.MAX_VALUE;
        for (int k = m; k <= n - m; k++) {
            ans = Math.min(ans, pre[k] + suf[k + 1]);
        }

        dduoln(ans);
    	
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
//        t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

F 题

相邻的两个数不能一样

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {
    static IOS sc=new IOS();
    static final int MOD = 998244353;
    public static void solve() throws IOException {
        
    	int n = sc.nextInt();

        ArrayList<Integer> a = new ArrayList<>();
        a.add(1);

        int j = 2; 	// 种类数
        while (a.size() < n) {
            a.add(j);
            j++;
            int nn = a.size();
            for (int i = 0; i < nn - 1; i++) {
                a.add(a.get(i));
            }
        }

        System.out.println(j - 1);
        for (int i = 0; i < n; i++) {
            System.out.print(a.get(i) + " ");
        }
    	
    }
    
    public static void main(String[] args) throws Exception {
        int t = 1;
//        t = sc.nextInt();
        while (t-- > 0) {solve();}
    }
    
    static <T> void dduo(T t) {System.out.print(t);}
    static <T> void dduoln(T t) {System.out.println(t);}

}

class IOS{
    BufferedReader bf;
    StringTokenizer st;
    BufferedWriter bw;
    public IOS(){
        bf=new BufferedReader(new InputStreamReader(System.in));
        st=new StringTokenizer("");
        bw=new BufferedWriter(new OutputStreamWriter(System.out));
    }
    public String nextLine() throws IOException{
        return bf.readLine();
    }
    public String next() throws IOException{
        while(!st.hasMoreTokens()){
            st=new StringTokenizer(bf.readLine());
        }
        return st.nextToken();
    }
    public char nextChar() throws IOException{
        return next().charAt(0);
    }
    public int nextInt() throws IOException{
        return Integer.parseInt(next());
    }
    public long nextLong() throws IOException{
        return Long.parseLong(next());
    }
    public double nextDouble() throws IOException{
        return Double.parseDouble(next());
    }
    public float nextFloat() throws IOException{
        return Float.parseFloat(next());
    }
    public BigInteger nextBigInteger() throws IOException{
        return new BigInteger(next());
    }
    public BigDecimal nextDecimal() throws IOException{
        return new BigDecimal(next());
    }
}

牛客竞赛主页

她说喜欢是装的的比赛主页

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/980597.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

Redis 高可用性:如何让你的缓存一直在线,稳定运行?

&#x1f3af; 引言&#xff1a;Redis的高可用性为啥这么重要&#xff1f; 在现代高可用系统中&#xff0c;Redis 是一款不可或缺的分布式缓存与数据库系统。无论是提升访问速度&#xff0c;还是实现数据的高效持久化&#xff0c;Redis 都能轻松搞定。可是&#xff0c;当你把 …

uniapp-原生android插件开发摘要

uni-app在App侧的原生扩展插件&#xff0c;支持使用java、object-c等原生语言编写&#xff0c;从HBuilderX 3.6起&#xff0c;新增支持了使用uts来开发原生插件。 基础项目 UniPlugin-Hello-AS工程请在App离线SDK中查找 基础项目(App离线SDK)已经配置好了自定义插件所需要的…

【定昌Linux系统】部署了java程序,设置开启启动

将代码上传到相应的目录&#xff0c;并且配置了一个.sh的启动脚本文件 文件内容&#xff1a; #!/bin/bash# 指定JAR文件的路径&#xff08;如果JAR文件在当前目录&#xff0c;可以直接使用文件名&#xff09; JAR_FILE"/usr/local/java/xs_luruan_client/lib/xs_luruan_…

SpringBoot源码解析(十):应用上下文AnnotationConfigServletWebServerApplicationContext构造方法

SpringBoot源码系列文章 SpringBoot源码解析(一)&#xff1a;SpringApplication构造方法 SpringBoot源码解析(二)&#xff1a;引导上下文DefaultBootstrapContext SpringBoot源码解析(三)&#xff1a;启动开始阶段 SpringBoot源码解析(四)&#xff1a;解析应用参数args Sp…

Unity小功能实现:鼠标点击移动物体

1、功能描述 当玩家点击鼠标时&#xff0c;场景中的物体会移动到鼠标点击的位置。这个功能可以用于控制角色移动、放置物体等场景。 2、实现步骤 创建Unity项目&#xff1a;首先&#xff0c;打开Unity并创建一个新的3D项目。 添加3D物体&#xff1a;在场景中创建一个3D物体&am…

游戏引擎学习第127天

仓库:https://gitee.com/mrxiao_com/2d_game_3 为本周设定阶段 我们目前的渲染器已经实现了令人惊讶的优化&#xff0c;经过过去两周的优化工作后&#xff0c;渲染器在1920x1080分辨率下稳定地运行在60帧每秒。这个结果是意料之外的&#xff0c;因为我们没有预计会达到这样的…

Opencv 图像基本操作

1.1 数据读取-图像 opencv读取的格式是BGR而不是RGB import cv2 import matplotlib.pyplot as plt import numpy as np %matplotlib inline # 在Notebook的输出单元格内嵌入绘制的图形&#xff0c;而不在新窗口中显示img cv2.imread(cat.jpg) # cv2.IMREAD_COLOR&#xff1a…

【微知】ssh如何指定免密的2种简单方式?(vim ~/.ssh/authorized_keys、ssh-copy-id)

背景 ssh通过存储公钥到远端服务器&#xff0c;可以完成本端访问远端服务器的时候免密。免密原理是本端使用私钥&#xff0c;远端公钥&#xff0c;远端可以进行鉴权 方法1&#xff1a; vim ~/.ssh/authorized_keys 将本地电脑的pub的key直接copy到远端 ~/.ssh/authorized_ke…

Skywalking介绍,Skywalking 9.4 安装,SpringBoot集成Skywalking

一.Skywalking介绍 Apache SkyWalking是一个开源的分布式追踪与性能监视平台&#xff0c;特别适用于微服务架构、云原生环境以及基于容器&#xff08;如Docker、Kubernetes&#xff09;的应用部署。该项目由吴晟发起&#xff0c;并已加入Apache软件基金会的孵化器&#xff0c;…

卷积神经网络(cnn,He初始化+relu+softmax+交叉熵+卷积核,六)

He初始化relusoftmax交叉熵卷积核&#xff0c;才是cnn&#xff0c;我们推导的公式&#xff1a; **** &#xff08;p【k】-y【k】&#xff09;*drelu(yi[k])*w2[j, k]*drelu(hi[j])*x【i】 只能满足&#xff1a;He初始化relusoftmax交叉熵。 我们参考&#xff1a; cnn突破七…

【区块链 + 智慧政务】 伽罗华域:区块链数据溯源系统 | FISCO BCOS 应用案例

由北京伽罗华域科技有限公司打造的区块链数据溯源系统&#xff0c; 实现了数据从生产、管理到共享的全流程可追溯性和安全审计。系统支持数据的全生命周期管理&#xff0c; 包括数据采集、生产、共享等关键流程&#xff0c; 并通过智能合约自动执行数据的存证、共享与安全审计&…

矩阵基本概念

前言 本文隶属于专栏《机器学习数学通关指南》&#xff0c;该专栏为笔者原创&#xff0c;引用请注明来源&#xff0c;不足和错误之处请在评论区帮忙指出&#xff0c;谢谢&#xff01; 本专栏目录结构和参考文献请见《机器学习数学通关指南》 正文 一句话理解矩阵 矩阵是数据排…

本地部署大语言模型-DeepSeek

DeepSeek 是国内顶尖 AI 团队「深度求索」开发的多模态大模型&#xff0c;具备数学推理、代码生成等深度能力&#xff0c;堪称"AI界的六边形战士"。 Hostease AMD 9950X/96G/3.84T NVMe/1G/5IP/RTX4090 GPU服务器提供多种计费模式。 DeepSeek-R1-32B配置 配置项 规…

xss自动化扫描工具-DALFox

声明&#xff01;本文章所有的工具分享仅仅只是供大家学习交流为主&#xff0c;切勿用于非法用途&#xff0c;如有任何触犯法律的行为&#xff0c;均与本人及团队无关&#xff01;&#xff01;&#xff01; 工具&#xff1a;https://pan.quark.cn/s/3d824b8637f1 目录标题 一、…

P8720 [蓝桥杯 2020 省 B2] 平面切分--set、pair

P8720 [蓝桥杯 2020 省 B2] 平面切分--set、pair 题目 分析一、pair1.1pair与vector的区别1.2 两者使用场景两者组合使用 二、set2.1核心特点2.2set的基本操作2.3 set vs unordered_set示例&#xff1a;统计唯一单词数代码 题目 分析 大佬写的很明白&#xff0c;看这儿 我讲讲…

vue3学习-1(基础)

vue3学习-1&#xff08;基础&#xff09; 1. 开始API 风格选项式 API (Options API)组合式 API (Composition API) 快速创建个应用 2.基础1. 创建个应用2.模板语法3.响应式基础reactive() 的局限性[](https://cn.vuejs.org/guide/essentials/reactivity-fundamentals.html#limi…

eMMC安全简介

1. 引言 术语“信息安全”涵盖多种不同的设计特性。一般而言&#xff0c; 信息安全是指通过实践防止信息遭受未经授权的访问、使用、披露、中断、篡改、检查、记录或销毁。 信息安全的三大核心目标为 机密性&#xff08;Confidentiality&#xff09;、完整性&#xff08;Integr…

SpringBoot新闻推荐系统设计与实现

随着信息时代的快速发展&#xff0c;新闻推荐系统成为用户获取个性化内容的重要工具。本文将介绍一个幽络源的基于SpringBoot开发的新闻推荐系统&#xff0c;该系统功能全面&#xff0c;操作简便&#xff0c;能够满足管理员和用户的多种需求。 管理员模块 管理员模块为系统管…

zookeeper-docker版

Zookeeper-docker版 1 zookeeper概述 1.1 什么是zookeeper Zookeeper是一个分布式的、高性能的、开源的分布式系统的协调&#xff08;Coordination&#xff09;服务&#xff0c;它是一个为分布式应用提供一致性服务的软件。 1.2 zookeeper应用场景 zookeeper是一个经典的分…

Java 调试模式下 Redisson 看门狗失效

一、场景分析 前几天在做分布式锁测试&#xff1a; 在调试模式下&#xff0c;lock.lock() 之后打上断点&#xff0c;想测试一下在当前线程放弃锁之前&#xff0c;别的线程能否获取得到锁。 发现调试模式下&#xff0c;看门狗机制失效了&#xff0c;Redis 上 30 秒后&#xff0…