牛客周赛84 题解 Java ABCDEFG AK实录

ops/2025/3/17 17:27:42/

目录

题目地址

做题情况

A 题

B 题

C / D 题

E 题

F / G 题

题目地址

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

做题情况

A 题

import java.io.*;
import java.math.*;
import java.util.*;// xixi♡西
public class Main {static IOS sc=new IOS();static void solve() throws IOException {int a1=sc.nextInt();int a2=sc.nextInt();int a3=sc.nextInt();dduoln(Math.abs(a1-a2)+Math.abs(a2-a3)==0?"Yes":"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 题

import java.io.*;
import java.math.*;
import java.util.*;// xixi♡西
public class Main {static IOS sc=new IOS();static void solve() throws IOException {int n=sc.nextInt();int arr[]=new int[n];HashMap< Integer , Integer >hm=new HashMap<>();for(int i=0;i<n;i++) {arr[i]=sc.nextInt();hm.put(arr[i], hm.getOrDefault(arr[i], 0)+1);}Arrays.sort(arr);int cnt=0;for(int i=0;i<n-1;i++) {cnt+=Math.abs(arr[i]-arr[i+1]);}if(hm.size()==1) {dduoln("1 "+cnt);}else {dduoln("2 "+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());}
}

C / D 题

import java.io.*;
import java.math.*;
import java.util.*;// xixi♡西
public class Main {static IOS sc=new IOS();static void solve() throws IOException {int n=sc.nextInt();int k=sc.nextInt();String str=sc.next();long arr[]=new long[n-1];for(int i=0;i<n-1;i++) {arr[i]=Math.abs(str.charAt(i+1)-str.charAt(i));}long ans=0;// 双指针int i=0;int j=n-2;long a=1;long cnt=0;if(k>n/2) {k=n-k+1;}else {k--;}while(i<=j) {
//			dduoln(a);if(i==j) {cnt+=arr[i]*a;}else {cnt+=arr[i]*a;cnt+=arr[j]*a;	}if(a<k) {a++;}i++;j--;}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 题

import java.io.*;
import java.math.*;
import java.util.*;// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static int MOD=(int) (1e9+7);static ArrayList< ArrayList<Integer> > list;static long dp[]; // 当前节点往下的陡峭值static int father[]; // 当前节点父节点的值static void solve() throws IOException {int n=sc.nextInt();list = new ArrayList<>();for(int i=0;i<n+5;i++) {list.add(new ArrayList<>());}for(int i=0;i<n-1;i++) {int u=sc.nextInt();int v=sc.nextInt();list.get(u).add(v);list.get(v).add(u);}dp=new long[n+1];father=new int[n+1];dfs(1,0);long min=Long.MAX_VALUE;for(int i=2;i<=n;i++) {long t1=dp[i];long t2=dp[1]-dp[i]-Math.abs(i-father[i]);min=Math.min(min, Math.abs(t1-t2));}dduoln(min);} /*** * @param u 当前节点* @param p 父节点*/static void dfs(int u,int p) {father[u]=p;for(int v:list.get(u)) { // v 相邻节点if(v==p)continue;dfs(v,u);dp[u]+= Math.abs(u-v) + dp[v];}}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(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {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 / G 题

import java.io.*;
import java.math.*;
import java.util.*;// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static final int mod=(int) (1e9+7);static void solve() throws IOException {int n=sc.nextInt();long a[]=new long[n];for(int i=0;i<n;i++){a[i]=sc.nextLong();}Arrays.sort(a);long ans=0,sum=0;for(int i=0;i<n;i++){ans+=a[i]*i-sum;sum+=a[i];ans%=mod;sum%=mod;}long fenzi=ans%mod*2%mod;long fenmu=n%mod;dduoln(fenzi%mod*pow(fenmu,mod-2,mod)%mod);} // 计算 q 在模 MOD 下的逆元 public static long modInverse(long q) { return pow(q, mod - 2, mod); } // 快速幂取模函数 public static long pow(long base, long exponent, long mod) { long result = 1; base = base % mod; while (exponent > 0) { if ((exponent & 1) == 1) { result =(long)((long) result * base % mod); } exponent = exponent >> 1; base = (long) ((long) base * base % mod); } return result; } 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(){System.out.println("");}static <T> void dduoln(T t){System.out.println(t);}}class IoScanner {BufferedReader bf;StringTokenizer st;BufferedWriter bw;public IoScanner() {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/ops/166567.html

相关文章

​​​​​​​大语言模型安全风险分析及相关解决方案

大语言模型的安全风险可以从多个维度进行分类。 从输入输出的角度来看,存在提示注入、不安全输出处理、恶意内容生成和幻觉错误等风险; 从数据层面来看,训练数据中毒、敏感信息泄露和模型反演攻击是主要威胁; 模型自身则面临拒绝服务和盗窃的风险; 供应链和插件的不安全引…

三角函数:从宇宙法则到AI革命的数学密钥

——跨越三千年的数学语言与现代科技全景透视 一、数学本质&#xff1a;宇宙的波动密码 1.1 拓扑学视角下的三角函数 三角函数本质是单位圆上点的坐标参数化&#xff0c;其数学表达可抽象为&#xff1a; { x cos ⁡ θ ℜ ( e i θ ) y sin ⁡ θ ℑ ( e i θ ) \begin…

平板作为笔记本副屏使用spacedesk

平板作为笔记本的一块副屏使用 软件 spacedesk 已上传&#xff0c;可自行下载。&#xff08;上传需要审核且只能绑定一个资源&#xff0c;可在官网自行下载&#xff0c;或私聊我&#xff09; PC版 移动版 spacedesk-2-1-17.apk 电脑版按照提示一步一步安装节即可移动端直接…

Centos离线安装openssl

文章目录 Centos离线安装openssl1. openssl是什么&#xff1f;2. openssl下载地址3. openssl-devel安装4. 安装结果验证5. 版本查看 Centos离线安装openssl 1. openssl是什么&#xff1f; OpenSSL 是一个开源的、跨平台的 加密工具库 和 命令行工具集&#xff0c;广泛用于实现…

架构思维:软件建模与架构设计的关键要点

文章目录 1. 软件建模的核心概念2. 七种常用UML图及其应用场景类图时序图组件图部署图用例图状态图活动图 3. 软件设计文档的三阶段结构4. 架构设计的关键实践1. 用例图&#xff1a;核心功能模块2. 部署图&#xff1a;架构演进阶段3. 技术挑战与解决方案4. 关键架构图示例5. 架…

Manus 一码难求,MetaGPT、OpenManus、Camel AI 会是替代方案吗?

Manus 一码难求&#xff0c;MetaGPT、OpenManus、Camel AI 会是替代方案吗&#xff1f; 一、Manus 的现象与问题 Manus 作为一款号称“全球首个通用 AI 智能体”的产品&#xff0c;凭借其强大的功能和新颖的营销策略迅速走红。然而&#xff0c;其封闭的邀请码机制和高昂的使用…

失败的面试经历(ʘ̥∧ʘ̥)

一.面向对象的三大特性 1.封装&#xff1a;将对象内部的属性私有化&#xff0c;外部对象不能够直接访问&#xff0c;但是可以提供一些可以使外部对象操作内部属性的方法。 2.继承&#xff1a;类与类之间会有一些相似之处&#xff0c;但也会有一些异处&#xff0c;使得他们与众…

分别用树型和UML结构展示java集合框架常见接口和类

树型结构展示java集合框架常见接口和类 Java 集合框架中的接口和子类关系可以用树形结构来展示。以下是一个简化的树形结构&#xff0c;展示了主要的接口和一些重要的实现类&#xff1a; java.util.Collection ├── java.util.List │ ├── java.util.ArrayList │ ├…