牛客周赛85 题解 Java ABCDEFG

news2025/3/17 20:38:42

A小紫的均势博弈

判断输入的 n 是奇数还是偶数

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {

    static IoScanner sc = new IoScanner();
    static final int mod=(int) (1e9+7);
    
	static void solve() throws IOException {
    	
		int n=sc.nextInt();
		if(n%2==0) {
			dduoln("yukari");
		}else {
			dduoln("kou");
		}
		
    } 
    
    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());
    }
}

B小紫的劣势博弈

排序 然后一人拿一个

import java.io.*;
import java.math.*;
import java.util.*;

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 arr[]=new long[n];
		
		for(int i=0;i<n;i++) {
			arr[i]=sc.nextLong();
		}
		
		Arrays.sort(arr);
		
		long ans=0;
		
		for(int i=0;i<n;i++) {
			if(i%2==0) {
				ans+=arr[i];
			}else {
				ans+=arr[i]*(-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(){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());
    }
}

C小紫的01串操作

通过观察我们发现

操作 1001 等效于操作 101

操作 100010011000 等效于操作 101010

然后发现经过小红和小紫操作

10 可以 10 -> 11

101 可以 101 -> 111

1010 可以 1010 -> 1110 -> 1111

10101 可以 10101 -> 10001 -> 11111

101010 不可以
...

所以只要判断出现多少次 01 就可以

import java.io.*;
import java.math.*;
import java.util.*;

public class Main {

    static IoScanner sc = new IoScanner();
    static final int mod=(int) (1e9+7);
    
	static void solve() throws IOException {
    	
		String str=sc.next();
		
		int a=1;
		
		for(int i=1;i<str.length();i++) {
			if(str.charAt(i)!=str.charAt(i-1)) {
				a++;
			}
		}
		
		if(a<=5) {
			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(){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());
    }
}

D小紫的优势博弈

准备先暴力一发的 结果直接过了

10010

小红操作会有 5 种情况 这等于 n

0010

010

10

1

我的想法是一个一个从头开始遍历这些字符串

统计 1 0 出现的次数

如果都出现偶数次 那么小紫获胜

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();
		String str=sc.next();
		int cnt=0;
		
		for(int i=1;i<n;i++) {
			int cnt0=0;
			int cnt1=0;
			for(int j=i;j<n;j++) {
				if(str.charAt(j)=='1')cnt1++;
				if(str.charAt(j)=='0')cnt0++;
				if(cnt0!=0||cnt1!=0) {
					if(cnt0%2==0&&cnt1%2==0) {
						cnt++;
						break;
					}
				}
			}
		}
		
		dduoln((double)cnt/(double)n);
		
		
    } 
    
    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());
    }
}

E小紫的线段染色

首先要知道的是 有符合的情况

一部分染紫色 一部分染红色 等效于 一部分染红色 一部分染紫色

我们可以将结构体排序

int arr []{起始 l,结束 r,当前次序 i}

按照 l 从小到大 r 从小到大次之

第一段为红色 那么红色的末尾为 arr[0][2]

然后 从第二段 进行遍历

如果第二段的起始 l 小于红色末尾

那必须染成紫色

但如果起始 l 小于紫色末尾

那就无法染成了(输出-1 return)

如果 l 大于紫色末尾 则可以染成紫色 更新紫色末尾为第二段结束的 r

同时记录当前 arr[][3]

继续遍历

如果第三段的起始 l 大于红色末尾 更新红色末尾为第二段结束的 r

...

这边有个坑 就是

得 必须 选出一个染成紫色...

不能全是红色

import java.util.*;
import java.io.*;
import java.math.*; 
 
public class Main {
	
	static IoScanner sc = new IoScanner();
	static final int mod=(int) (1e9+7);
    
    public static void main(String[] args) throws IOException {
        
    	int n=sc.nextInt();
    	List<Long []>list=new ArrayList<>();
    	
    	for(long i=0;i<n;i++) {
    		long l=sc.nextLong();
    		long r=sc.nextLong();
    		list.add(new Long[] {l,r, i+1});
    	}
    	
    	Collections.sort(list,(o1,o2)->{
    		if(o1[0]==o2[0]) {
    			return Long.compare(o1[1], o2[1]);
    		}else {
    			return Long.compare(o1[0], o2[0]);
    		}
    	});
    	
    	ArrayList<Long> anslist=new ArrayList<>();
    	
    	long hongend=list.get(0)[1];
    	long ziend=-1;
    	
    	for(int i=1;i<n;i++) {
    		long start=list.get(i)[0];
    		long end=list.get(i)[1];
    		if(start<=hongend) { // 要变成紫色
    			if(ziend==-1) {
    				// 直接赋值
    				ziend=end;
    			}else {
    				if(start<=ziend) {
        				dduoln("-1");
        				return;
        			}else {
        				ziend=end;
        			}
    			}
    			// 变成紫色
    			anslist.add((list.get(i)[2]));
    		}else {
    			// 继续红色
    			hongend=end;
    		}
    		
    	}
    	
    	if(anslist.size()==0) {
    		anslist.add((long) 1);
    	}
    	
    	dduoln(anslist.size());
    	
    	Collections.sort(anslist);
    	for(long i:anslist) {
    		dduo(i+" ");
    	}
    	
    }
    
    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小紫的树上染色

import java.util.*; 
import java.io.*;
import java.math.*; 
 
public class Main {
	
	static IoScanner sc = new IoScanner();
	static final int mod=(int) (1e9+7);
	
    static int n;
    static int k;
	// 邻边矩阵
    static List<List<Integer>> list;
    
    public static void main(String[] args) throws IOException {
        
    	n = sc.nextInt(); 
        k = sc.nextInt(); 
        
        if(k==n) {
        	dduoln("0");
        	return;
        }
        
        list = new ArrayList<>();
        for (int i = 0; i <= n; 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);
        }
 
        int low = 1;
        int high = n;
        int ans=n;
       
        while (low <= high) {
            int mid = (low + high) >> 1;
            if (check(mid, k)) {
                ans = mid;
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        dduoln(ans); 
    }
 
    static boolean check(int mid, int k) {
        int[] res = new int[n+5];
        int cnt = 0;
        Deque<Object[]> stack = new ArrayDeque<>();
        stack.push(new  Object[]{1, -1, false});
 
        while (!stack.isEmpty())  {
            Object[] cur = stack.pop(); 
            
            int u = (Integer) cur[0];
            int p = (Integer) cur[1];
            boolean judge = (Boolean) cur[2];
 
            if (judge==false) { // 未染色
            	
                stack.push(new  Object[]{u, p, true});
                List<Integer> children = new ArrayList<>();
                for (int v : list.get(u))  { // v邻点  u当前顶点  p父顶点
                    if (v != p) {
                        children.add(v); 
                    }
                }
//                for(Integer i:children) {
//                	dduo(i);
//                }
//                dduoln();
                Collections.reverse(children); 
                for (int v : children) {
                    stack.push(new  Object[]{v, u, false});
                }
                
            } else { // 染色
            	
                int sum = 0;
                for (int v : list.get(u))  {  // v邻点  u当前顶点  p父顶点
                    if (v != p) {
                        sum += res[v];
                    }
                }
                if (sum + 1 > mid) {
                    cnt++;
                    res[u] = 0;
                } else {
                    res[u] = sum + 1;
                }
                
            }
        }
        if(cnt <= k)return true;
        else return false;
    }
    
    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.coloradmin.cn/o/2316804.html

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

相关文章

# RAG 框架 # 一文入门 全链路RAG系统构建与优化 —— 架构、策略与实践

本文全面阐述了RAG系统从数据收集、数据清洗&#xff08;包括领域专有名词处理&#xff09;、智能数据分块与QA对生成&#xff0c;到向量化、向量数据库选择与配置&#xff0c;再到检索方式及重排序&#xff0c;直至整合输出、监控反馈和安全保障的全流程。通过这一完整方案&am…

【Golang】第二弹-----变量、基本数据类型、标识符

笔上得来终觉浅,绝知此事要躬行 &#x1f525; 个人主页&#xff1a;星云爱编程 &#x1f525; 所属专栏&#xff1a;Golang &#x1f337;追光的人&#xff0c;终会万丈光芒 &#x1f389;欢迎大家点赞&#x1f44d;评论&#x1f4dd;收藏⭐文章 目录 一、变量 1.1基本介绍…

linux系统CentOS 7版本搭建NFS共享存储

一、什么是NFS共享存储方式 NFS共享存储方式 是一种分布式文件系统协议&#xff0c;允许客户端通过网络访问远程服务器上的文件&#xff0c;就像访问本地文件一样。 二、 NFS的基本概念 &#xff08;1&#xff09;服务器端&#xff1a;提供共享存储的机器&#xff0c;负责导…

Matlab 基于SVPWM的VF三电平逆变器异步电机速度控制

1、内容简介 略 Matlab 167-基于SVPWM的VF三电平逆变器异步电机速度控制 可以交流、咨询、答疑 2、内容说明 略 3、仿真分析 略 4、参考论文 略

(一)微服务初见之 Spring Cloud 介绍

微服务架构简介 从单体应用架构发展到SOA架构&#xff0c;再到微服务架构&#xff0c;应用架构经历了多年的不断演进。微服务架构不是凭空产生的&#xff0c;而是技术发展的必然结果&#xff0c;分布式云平台的应用环境使得微服务代替单体应用成为互联网大型系统的架构选择。目…

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

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

【RNN神经网络】序列模型与RNN神经网络

前言 清库存。正式切入大模型后&#xff0c;打算把基础知识都梳理一遍&#xff0c;然后写了两篇就发现写不动了&#xff0c;后面就捡重要的记录。RNN知识仅此一篇记录&#xff0c;扫盲记录。 【自然语言处理】 &#xff08;Natural Language Processing&#xff0c;NLP&#xf…

Python文件管理

目录 一、文本文件读写 1、相关函数 2、读写文件 3、使用readline读取一行 4、读写文件的异常处理 5、添加内容 二、文本文件的编码 1、常见的编码 2、Python程序的编码 3、指定编码 三、文件的路径 1、相对路径 2、绝对路径 3、路径的改变 四、文件夹操作 五、…

vue3 前端路由权限控制与字典数据缓存实践(附Demo)

目录 前言1. 基本知识2. Demo3. 实战 前言 &#x1f91f; 找工作&#xff0c;来万码优才&#xff1a;&#x1f449; #小程序://万码优才/r6rqmzDaXpYkJZF 从实战中出发&#xff1a; 1. 基本知识 Vue3 和 Java 通信时如何进行字典数据管理 需要了解字典数据的结构。通常&#x…

基于javaweb的SpringBoot精美物流管理系统设计与实现(源码+文档+部署讲解)

技术范围&#xff1a;SpringBoot、Vue、SSM、HLMT、Jsp、PHP、Nodejs、Python、爬虫、数据可视化、小程序、安卓app、大数据、物联网、机器学习等设计与开发。 主要内容&#xff1a;免费功能设计、开题报告、任务书、中期检查PPT、系统功能实现、代码编写、论文编写和辅导、论…

DeepSeek进阶应用(二):结合Kimi制作PPT(双AI协作教程)

&#x1f31f;引言&#xff1a; DeepSeek作为国产AI大模型&#xff0c;以强大的逻辑推理和结构化内容生成能力著称&#xff0c;擅长根据用户需求生成PPT大纲或Markdown文本&#xff1b;Kimi的PPT助手则能解析结构化内容并套用模板快速生成美观的PPT&#xff0c;两者结合实现“内…

SpringBoot——Maven篇

Spring Boot 是一个用于快速开发基于 Spring 框架的应用程序的工具。它具有许多特性&#xff0c;其中一些重要的特性包括&#xff1a; 1. 自动配置&#xff1a;Spring Boot 提供了自动配置的机制&#xff0c;可以根据应用程序的依赖和环境自动配置应用程序的各种组件&#xff…

卷积神经网络(知识点)

一、为了使特征图变小&#xff1a; 由两种方法&#xff1a;1.增大步长&#xff1a;卷积的时候不是一次一步&#xff0c;而是一次多步&#xff0c;类似一张图片&#xff0c;在原来的像素基础上&#xff0c;每隔一个取一个像素点。 其中S就是步长 注意&#xff1a;扩大步长不经…

Vision Transformer (ViT):将Transformer带入计算机视觉的革命性尝试(代码实现)

Vision Transformer (ViT)&#xff1a;将Transformer带入计算机视觉的革命性尝试 作为一名深度学习研究者&#xff0c;如果你对自然语言处理&#xff08;NLP&#xff09;领域的Transformer架构了如指掌&#xff0c;那么你一定不会对它在序列建模中的强大能力感到陌生。然而&am…

特殊 IP 地址

文章目录 特殊IP地址概述受限广播地址&#xff08;Limited Broadcast Address&#xff09;直接广播地址&#xff08;Directed Broadcast Address&#xff09;多播地址&#xff08;Multicast Address&#xff09;环回地址&#xff08;Loopback Address&#xff09;本网络本主机&…

数学——A. K-divisible Sum + D. Exam in MAC

A. K-divisible Sum 题目&#xff1a; 思路&#xff1a; 以下 “[xxx]” 符号均代表向上取整 我们假设总和是sum&#xff0c;那么就有sum k * cnt 要想最大值最小&#xff0c;肯定是要让sum尽可能小&#xff0c;这样每个元素都能变小 最小情况是 sum 恰好等于 n 时&#…

【DeepSeek应用】本地部署deepseek模型后,如何在vscode中调用该模型进行代码撰写,检视和优化?

若已成功在本地部署了 DeepSeek 模型(例如通过 vscode-llm、ollama 或私有 API 服务),在 VS Code 中调用本地模型进行代码撰写、检视和优化的完整流程如下: 1. 准备工作:确认本地模型服务状态 模型服务类型: 若使用 HTTP API 服务(如 FastAPI/Flask 封装),假设服务地址…

网络编程、URI和URL的区别、TCP/IP协议、IP和端口、URLConnection

DAY12.1 Java核心基础 网络编程 在互联网时代&#xff0c;网络在生活中处处可见&#xff0c;javaWeb占据了很大一部分 那如何实现javaWeb编程呢&#xff1f; Web编程就是运行在同一个网络下面的终端&#xff0c;使得它们之间可以进行数据传输 计算机网络基本知识 计算机网络…

自探索大语言模型微调(一)

一、数据 1.1、失败案例 Hugging Face&#xff1a; 根据B站上搜索到的资料&#xff0c;datasets这个库可以直接下载丰富的数据集合和与训练模型&#xff0c;调用也非常的简单&#xff0c;唯一的缺点就是&#xff0c;需要外网&#xff08;翻墙&#xff09;&#xff0c;用国内的…

Unity 和 Python 的连接(通过SocketIO)附源码

在游戏或者项目开发中&#xff0c;Unity 通常用于创建前端&#xff0c;而 Python 则因其强大的数据处理能力常被用作后端。通过 Socket.IO&#xff0c;我们可以轻松地实现 Unity 和 Python 的实时通信。本文将介绍如何通过 Socket.IO 连接 Unity 和 Python&#xff0c;并附上完…