题解 | 牛客周赛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());}
}

牛客竞赛主页

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

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

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

vulkanscenegraph显示倾斜模型(5)-视景器准备

前言 本文在接着往下讨论视景器准备相关步骤。Vulkan相比opengl更底层,其提供了更底层的硬件控制、更高的性能以及更好的多线程支持,VSG 通过封装 Vulkan 的复杂性,提供了更简单易用的接口,同时保留了 Vulkan 的高性能和灵活性。它简化了 Vulkan 的初始化、渲染管线配置、资…

基金 word-->pdf图片模糊的解决方法

1. 首先需要Adobe或福昕等pdf阅读器。 2. word中 [文件]--[打印]&#xff0c;其中打印机选择pdf阅读器&#xff0c;例如此处我选择福昕阅读器。 3. 选择 [打印机属性]--[编辑]--[图像]&#xff0c;将所有的采样、压缩均设置为 关闭。点击[另存为]&#xff0c;保存为 基金报告…

基于RKNN的嵌入式深度学习开发(2)

上一个章节我们介绍的RKNN模型的模型转换和模型的推理&#xff0c;这一章节我们将介绍模型的量化和评估部分。 2.3 RKNN模型的量化 量化就是将浮点转换为定点运算的过程&#xff0c;或者训练后由rknn来量化。量化模型使用较低精度&#xff08;如int8/uint8/int16&#xff09;保…

单一职责原则(设计模式)

目录 问题&#xff1a; 定义&#xff1a; 解决&#xff1a; 方式 1&#xff1a;使用策略模式 示例&#xff1a;用户管理 方式 2&#xff1a;使用装饰者模式 示例&#xff1a;用户操作 方式 3&#xff1a;使用责任链模式 示例&#xff1a;用户操作链 总结 推荐 问题&a…

Java 8 到 Java 17 主要新特性

Java 8 到 Java 17 是 Java 语言的多个重要版本&#xff0c;其中每个版本都引入了新的特性和改进。下面是 Java 8 到 Java 17 中主要的新特性概览。 Java 8 新特性 1. Lambda 表达式 Lambda 表达式是 Java 8 的一个重要特性&#xff0c;它使得 Java 支持函数式编程&#xff…

Qt 中signals和slots、Q_SIGNAL和Q_LOT、Q_SIGNALS和Q_SLOTS的区别和使用

Qt 中signals和slots、Q_SIGNAL和Q_SLOT、Q_SIGNALS和Q_SLOTS的区别和使用 1.signals和slots 信号和槽函数需要在类的声明中明确声明。信号需要使用signals关键字&#xff0c;而槽函数可以使用slots关键字&#xff08;虽然在现代Qt中&#xff0c;槽函数也可以直接作为普通成员…

【极客时间】浏览器工作原理与实践-2 宏观视角下的浏览器- 2.1 Chrome架构:仅仅打开了1个页面,为什么有4个进程?

https://time.geekbang.org/column/article/113513 2.1 Chrome架构&#xff1a;仅仅打开了1个页面&#xff0c;为什么有4个进程&#xff1f; 前置&#xff1a;基于Chrome浏览器学习浏览器的工作原理 原因&#xff1a; 因为 Chrome、微软的 Edge 以及国内的大部分主流浏览器…

智能图像处理平台:图像处理配置类

这里我们先修改一下依赖&#xff0c;不用JavaCV&#xff0c;用openCV。 导入依赖&#xff1a; <!-- JavaCV 依赖&#xff0c;用于图像和视频处理 --> <!-- <dependency>--> <!-- <groupId>org.bytedeco</groupId>--> &l…

【Python 初级函数详解】—— 参数沙漠与作用域丛林的求生指南

欢迎来到ZyyOvO的博客✨&#xff0c;一个关于探索技术的角落&#xff0c;记录学习的点滴&#x1f4d6;&#xff0c;分享实用的技巧&#x1f6e0;️&#xff0c;偶尔还有一些奇思妙想&#x1f4a1; 本文由ZyyOvO原创✍️&#xff0c;感谢支持❤️&#xff01;请尊重原创&#x1…

夜天之书 #106 Apache 软件基金会如何投票选举?

近期若干开源组织进行换届选举。在此期间&#xff0c;拥有投票权的成员往往会热烈讨论&#xff0c;提名新成员候选人和治理团队的候选人。虽然讨论是容易进行的&#xff0c;但是实际的投票流程和运作方式&#xff0c;在一个成员众多的组织中&#xff0c;可能会有不少成员并不清…

【蓝桥】大小写转换

1、islower()和isupper() 1.1 islower()函数 定义&#xff1a;用于判断一个字符是否为小写字母&#xff08;即 ‘a’ 到 ‘z’&#xff09; 1.2 isupper()函数 定义&#xff1a;用于判断一个字符是否为大写字母&#xff08;即 ‘A’ 到 ‘Z’&#xff09; 1.3 基础用法 #incl…

DeepSeek开源周 Day04:从DualPipe聊聊大模型分布式训练的并行策略

DualPipe简介 今天是DeepSeek开源周的第四天&#xff0c;官方开源了一种新型并行计算优化策略——DualPipe。 其实大家阅读过Deepseek-V3技术报告的同学&#xff0c;对这个技术并不陌生。 开源地址&#xff1a;https://github.com/deepseek-ai/DualPipe 核心亮点 DualPipe&…

2025.3.1面试总结

昨天面试了一家做路由器的公司&#xff0c;问的问题还是挺多的&#xff0c;比较全面&#xff0c;也有一定的深度&#xff0c;比较注重底层原理的实现。 下面是具体问题&#xff1a; 岗位&#xff1a;嵌入式软件工程师 自我介绍&#xff1b;&#xfeff;项目介绍&#xff0c;…

React:B站评论demo,实现列表渲染、删除按钮显示和功能实现、导航栏渲染切换及高亮显示、评论区的排序

功能要求&#xff1a; 1、渲染评论列表 2、删除评论功能&#xff1a;只显示自己评论的删除按钮&#xff1b;点击删除按钮&#xff0c;删除当前评论&#xff0c;列表中不再显示。 3、渲染导航Tab&#xff08;最新 | 最热&#xff09;和其 高亮实现 4、评论排序功能实现&…

智能座舱介绍

目录 智能座舱智能座舱的核心技术组成车载信息娱乐系统(IVI)数字仪表盘与HUD(抬头显示)语音交互与AI助手多屏联动与场景化交互生物识别技术智能座舱的发展趋势沉浸式体验情感化与个性化多模态交互融合车联网(V2X)生态扩展应用场景挑战与未来硬件系统软件系统关键技术智能…

linux-docker及docker-compose相关命令

文章目录 计算机系统5G云计算LINUX Docker及docker-conpose相关命令一、Docker 常用命令1、镜像管理1.拉取镜像2.列出本地镜像3.删除镜像4.构建镜像&#xff08;从 Dockerfile&#xff09;5.推送镜像到仓库6.从 .tar 文件加载镜像7.将镜像保存为 .tar 文件8.搜索镜像9.查看镜像…

一文了解:部署 Deepseek 各版本的硬件要求

很多朋友在咨询关于 DeepSeek 模型部署所需硬件资源的需求&#xff0c;最近自己实践了一部分&#xff0c;部分信息是通过各渠道收集整理&#xff0c;so 仅供参考。 言归正转&#xff0c;大家都知道&#xff0c;DeepSeek 模型的性能在很大程度上取决于它运行的硬件。我们先看一下…

0301 leetcode - 1502.判断是否能形成等差数列、 682.棒球比赛、657.机器人能否返回原点

1502.判断是否能形成等差数列 题目 给你一个数字数组 arr 。 如果一个数列中&#xff0c;任意相邻两项的差总等于同一个常数&#xff0c;那么这个数列就称为 等差数列 。 如果可以重新排列数组形成等差数列&#xff0c;请返回 true &#xff1b;否则&#xff0c;返回 false…

C#贪心算法

贪心算法&#xff1a;生活与代码中的 “最优选择大师” 在生活里&#xff0c;我们常常面临各种选择&#xff0c;都希望能做出最有利的决策。比如在超市大促销时&#xff0c;面对琳琅满目的商品&#xff0c;你总想用有限的预算买到价值最高的东西。贪心算法&#xff0c;就像是一…

【JAVA SE基础】抽象类和接口

目录 一、前言 二、抽象类 2.1 抽象类的概念 2.2 抽象类语法 2.3 抽象类特性 2.4 抽象类的作用 三、接口 3.1 什么是接口 3.2 语法规则 3.3 接口使用 3.4 接口特性 3.5 实现多接口 3.6 接口间的继承 四、Object类 4.1 获取对象信息&#xff08; toString() &…