当前位置: 首页 > news >正文

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

目录

题目地址

做题情况

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());
    }
}

相关文章:

  • Tauri + Vite + SvelteKit + TailwindCSS + DaisyUI 跨平台开发详细配置指南(Windows)
  • langchain4j对接阿里云百炼平台
  • Java 学习记录:基础到进阶之路(二)
  • 解锁MySQL 8.0.41源码调试:Mac 11.6+CLion 2024.3.4实战指南
  • 63. Three.js案例-不同材质属性来增强3D对象的真实感
  • [c语言日寄]浮点数的排序
  • 鸿蒙next 多行文字加图片后缀实现方案
  • goweb中文件上传和文件下载
  • [特殊字符] 深度实战:Android 13 系统定制之 Recovery 模式瘦身指南
  • 【Agent】OpenManus-Flow组件详细分析
  • PTA 7-12 排序
  • Linux 线程控制
  • VBA第二十六期 VBA用调试语句做进度条
  • 魔法协议Magic-MCP:开启AI智能体「万能互联」的新时代
  • 基于Yocto项目与SSD MobileNet的树莓派AI视觉系统构建指南*
  • 山东大学计算机组成与设计第五章习题解析
  • conda的基本使用及pycharm里设置conda环境
  • SpringData JPA事务管理:@Transactional注解与事务传播
  • Docker 镜像优化:如何避免重复安装软件,加速服务的构建与部署
  • 全面对比分析:HDMI、DP、DVI、VGA、Type-C、SDI视频接口特点详解
  • 上海平台网站制作公司哪家好/网站模板下载免费
  • 东莞 网站 建设 雕塑/网站搭建平台都有哪些
  • 西安公司网站建设哪家专业/教育机构退费纠纷找谁
  • 慈溪网站建设/百度sem是什么
  • 北京上地网站建设/亚马逊关键词搜索工具
  • 郑州网站制作哪家好/seo推广是做什么