题解 | 牛客周赛82 Java ABCDEF

embedded/2025/3/4 15:52:27/

目录

题目地址

做题情况

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.ppmy.cn/embedded/169957.html

相关文章

LangChain原理解析及开发实战指南(2025年最新版)

一、LangChain核心架构解析 1.1 框架设计理念 LangChain是基于提示工程(Prompt Engineering)构建的LLM应用开发框架&#xff0c;其核心思想是通过模块化组件实现大语言模型与业务系统的无缝对接。该框架采用分层设计&#xff1a; 接口层&#xff1a;统一对接OpenAI、DeepSee…

c++ 内存管理系统之智能指针

1.c内存管理 1.代码区 也称Text Segment&#xff0c;存放可执行程序的机器码。 2 数据区&#xff1a; 存放已初始化的全局和静态变量&#xff0c; 常量数据&#xff08;如字符串常量&#xff09;。 存放未初始化的全局和静态变量 无疑解释静态变量的来源&#xff1a; 局…

工地视频考勤打卡(电子工牌)数据结构

【项目背景】 我现在需要帮助用户设计一个完整的员工考勤打卡系统的数据表结构。用户之前已经得到了一个业务逻辑架构方案&#xff0c;现在他需要更详细的数据库设计&#xff0c;包括所有相关的数据表和字段。 首先系统包括早班、中班、晚班等多个班次&#xff0c;每个班次的…

视频教育网站开源系统的部署安装 (roncoo-education)服务器为ubuntu22.04.05

一、说明 前端技术体系&#xff1a;Vue3 Nuxt3 Vite5 Vue-Router Element-Plus Pinia Axios 后端技术体系&#xff1a;Spring Cloud Alibaba2021 MySQL8 Nacos Seata Mybatis Druid redis 后端系统&#xff1a;roncoo-education&#xff08;核心框架&#xff1a;S…

⭐算法OJ⭐矩阵的相关操作【动态规划 + 组合数学】(C++ 实现)Unique Paths 系列

文章目录 62. Unique Paths动态规划思路实现代码复杂度分析 组合数学思路实现代码复杂度分析 63. Unique Paths II动态规划定义状态状态转移方程初始化复杂度分析 优化空间复杂度状态转移方程 62. Unique Paths There is a robot on an m x n grid. The robot is initially lo…

跨部门沟通与团队协作

【跨部门协作&#xff1a;破局之道在冰山之下】 感谢太原市组织部信任&#xff0c;上海财经大学邀约 今日为财务精英拆解《跨部门沟通与团队协作》迷局。从本位思维到共同愿景&#xff0c;用因果回路图透视冲突本质&#xff0c;当财务人开始用"延迟反馈"视角看预算博…

在 ArcGIS Pro 中描绘和绘制流域

查找数字高程模型 (DEM) 对于 DEM&#xff0c;我使用了USGS Lidar Explorer 地图。该地区有 10m 分辨率的 DEM。 设置坐标系 将坐标系设置为 UTM&#xff0c;以尽量减少失真&#xff0c;并使工具在后续过程中进行更精确的计算。对于俄勒冈州&#xff0c;这是 UTM 区域 10。 …

【新人系列】Golang 入门(二):基本数据类型

✍ 个人博客&#xff1a;https://blog.csdn.net/Newin2020?typeblog &#x1f4dd; 专栏地址&#xff1a;https://blog.csdn.net/newin2020/category_12898955.html &#x1f4e3; 专栏定位&#xff1a;为 0 基础刚入门 Golang 的小伙伴提供详细的讲解&#xff0c;也欢迎大佬们…