题解 | 牛客周赛82 Java ABCDEF

news2025/3/4 10:41:32

目录

题目地址

做题情况

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.coloradmin.cn/o/2309449.html

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

相关文章

命名管道——进程间通信

个人主页&#xff1a;敲上瘾-CSDN博客 匿名管道&#xff1a;进程池的制作&#xff08;linux进程间通信&#xff0c;匿名管道... ...&#xff09;-CSDN博客 一、命名管道的使用 1.创建命名管道 1.1.在命令行中&#xff1a; 创建&#xff1a; mkfifo 管道名 删除&#xff1a…

高频 SQL 50 题(基础版)_1141. 查询近30天活跃用户数

1141. 查询近30天活跃用户数 select activity_date day,count(distinct user_id) active_users from Activity where (activity_date<2019-07-27 and activity_date>DATE_sub(2019-07-27,INTERVAL 30 DAY)) group by(activity_date)

Yocto + 树莓派摄像头驱动完整指南

—— 从驱动配置、Yocto 构建&#xff0c;到 OpenCV 实战 在树莓派上运行摄像头&#xff0c;在官方的 Raspberry Pi OS 可能很简单&#xff0c;但在 Yocto 项目中&#xff0c;需要手动配置驱动、设备树、软件依赖 才能确保摄像头正常工作。本篇文章从 BSP 驱动配置、Yocto 关键…

计算最大海岛面积

最大海岛面积问题的不同解法 问题举例 给定一个包含了一些 0 和 1 的非空二维数组 matrix 。 一个岛屿是由一些相邻的 1 (代表土地) 构成的组合&#xff0c;这里的「相邻」要求两个 1 必须在水平或者竖直方向上相邻。你可以假设matrix的四个边缘都被 0&#xff08;代表水&am…

Spark核心之02:RDD、算子分类、常用算子

spark内存计算框架 一、目标 深入理解RDD弹性分布式数据集底层原理掌握RDD弹性分布式数据集的常用算子操作 二、要点 ⭐️1. RDD是什么 RDD&#xff08;Resilient Distributed Dataset&#xff09;叫做**弹性分布式数据集&#xff0c;是Spark中最基本的数据抽象&#xff0c…

[Windows] 批量为视频或者音频生成字幕 video subtitle master 1.5.2

Video Subtitle Master 1.5.2 介绍 Video Subtitle Master 1.5.2 是一款功能强大的客户端工具&#xff0c;能够批量为视频或音频生成字幕&#xff0c;还支持批量将字幕翻译成其他语言。该工具具有跨平台性&#xff0c;无论是 mac 系统还是 windows 系统都能使用。 参考原文&a…

进来了解一下python的深浅拷贝

深浅拷贝是什么&#xff1a;在Python中&#xff0c;理解深拷贝&#xff08;deep copy&#xff09;和浅拷贝&#xff08;shallow copy&#xff09;对于处理复杂的数据结构&#xff0c;如列表、字典或自定义对象&#xff0c;是非常重要的。这两种拷贝方式决定了数据在内存中的复制…

解锁GPM 2.0「卡顿帧堆栈」|代码示例与实战分析

每个游戏开发者都有一个共同的愿望&#xff0c;那就是能够在无需复现玩家反馈的卡顿现象时&#xff0c;快速且准确地定位卡顿的根本原因。为了实现这一目标&#xff0c;UWA GPM 2.0推出了全新功能 - 卡顿帧堆栈&#xff0c;旨在为开发团队提供高效、精准的卡顿分析工具。在这篇…

微服务,服务治理nacos,负载均衡LOadBalancer,OpenFeign

1.微服务 简单来说&#xff0c;微服务架构风格[1]是一种将一个单一应用程序开发为一组小型服务的方法&#xff0c;每个服务运行在 自己的进程中&#xff0c;服务间通信采用轻量级通信机制(通常用HTTP资源API)。这些服务围绕业务能力构建并 且可通过全自动部署机制独立部署。这…

栈和队列的模拟实现

文章目录 一. 回顾栈和队列二. stack的模拟实现stack.hstack.cpp 三. queue的模拟实现queue.htest.cpp 四. 了解dequeuevector和list都有各自的缺陷deque 总结 一. 回顾栈和队列 回顾一下栈和队列 栈&#xff1a;stack&#xff1a;后进先出 _ 队列&#xff1a;queue&#xf…

unity pico开发 四 物体交互 抓取 交互层级

文章目录 手部设置物体交互物体抓取添加抓取抓取三种类型抓取点偏移抓取事件抓取时不让物体吸附到手部 射线抓取交互层级 手部设置 为手部&#xff08;LeftHandController&#xff09;添加XRDirInteractor脚本 并添加一个球形碰撞盒&#xff0c;勾选isTrigger,调整大小为0.1 …

【PromptCoder + Cursor】利用AI智能编辑器快速实现设计稿

【PromptCoder Cursor】利用AI智能编辑器快速实现设计稿 官网&#xff1a;PromptCoder 在现代前端开发中&#xff0c;将设计稿转化为可运行的代码是一项耗时的工作。然而&#xff0c;借助人工智能工具&#xff0c;这一过程可以变得更加高效和简单。本文将介绍如何结合 Promp…

MySQL面试01

MySQL 索引的最左原则 &#x1f370; 最左原则本质 ͟͟͞͞( •̀д•́) 想象复合索引是电话号码簿&#xff01; 索引 (a,b,c) 的排列顺序&#xff1a; 先按a排序 → a相同按b排序 → 最后按c排序 生效场景三连&#xff1a; 1️⃣ WHERE a1 ✅ 2️⃣ WHERE a1 AND b2 ✅ 3️…

webpack一篇

目录 一、构建工具 1.1简介 二、Webpack 2.1概念 2.2使用步骤 2.3配置文件&#xff08;webpack.config.js&#xff09; mode entry output loader plugin devtool 2.4开发服务器&#xff08;webpack-dev-server&#xff09; grunt/glup的对比 三、Vite 3.1概念 …

健康饮食,健康早餐

营养早餐最好包含4大类食物&#xff1a;谷薯类&#xff1b;碳水&#xff1b;蛋白质&#xff1b;膳食纤维。 1.优质碳水 作用&#xff1a;提供持久的能量&#xff0c;避免血糖大幅波动等 例如&#xff1a;全麦面包、红薯&#x1f360;、玉米&#x1f33d;、土豆&#x1f954;、…

【经验分享】Ubuntu20.04 vmware虚拟机存储空间越来越小问题(已解决)

【经验分享】Ubuntu20.04 vmware虚拟机存储空间越来越小问题&#xff08;已解决&#xff09; 前言一、问题分析二、解决方案 前言 我们在使用虚拟机过程中&#xff0c;经常会碰到即使删除了一些文件&#xff0c;但是存储空间还是越来越小的问题。今天我们来解决下这个问题。 一…

Jenkins-自动化部署-通知

场景 使用jenkins部署&#xff0c;但有时不能立马部署&#xff0c;需要先通知相关人员&#xff0c;再部署&#xff0c;如果确实不能部署&#xff0c;可以留时间撤销。 方案 1.开始前我们添加&#xff0c;真正开始执行的等待时间&#xff1b;可供选择&#xff08;Choice Param…

Qt 文件操作+多线程+网络

文章目录 1. 文件操作1.1 API1.2 例子1&#xff0c;简单记事本1.3 例子2&#xff0c;输出文件的属性 2. Qt 多线程2.1 常用API2.2 例子1&#xff0c;自定义定时器 3. 线程安全3.1 互斥锁3.2 条件变量 4. 网络编程4.1 UDP Socket4.2 UDP Server4.3 UDP Client4.4 TCP Socket4.5 …

《基于Hadoop的青岛市旅游景点游客行为分析系统设计与实现》开题报告

目录 一、选题依据 1.选题背景 2.国内外研究现状 &#xff08;1&#xff09;国内研究现状 &#xff08;2&#xff09;国外研究现状 3.发展趋势 4.应用价值 二、研究内容 1.学术构想与思路 2. 拟解决的关键问题 3. 拟采取的研究方法 4. 技术路线 (1)旅游前准备阶段 …

pycharm debug卡住

pycharm debug时一直出现 collecting data, 然后点击下一行就卡住。 勾选 Gevent compatible解决 https://stackoverflow.com/questions/39371676/debugger-times-out-at-collecting-data