牛客周賽85 題解 Java ABCDEFG

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.pswp.cn/web/72721.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/72721.shtml
英文地址,請注明出處:http://en.pswp.cn/web/72721.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

python——UI自動化(1) selenium之介紹和環境配置

一、selenium介紹 selenium是一個第三方庫&#xff0c;python有很多庫&#xff1b; 1、什么是ui自動化? 通過模擬手工操作用戶ui頁面的方式&#xff0c;用代碼去實現自動化操作和驗證的行為。 2、ui自動化的優點&#xff1f; &#xff08;1&#xff09;解決重復性的功能測…

Can通信流程

下面給出一個更詳細的 CAN 發送報文的程序流程說明&#xff0c;結合 HAL 庫的使用及代碼示例&#xff0c;幫助你了解每一步的具體操作和內部原理。 一、系統與外設初始化 1.1 HAL 庫初始化 在 main() 函數開頭&#xff0c;首先調用 HAL 庫初始化函數&#xff1a; HAL_Init()…

15 數據結構及算法應用

15 數據結構及算法應用 15.1 算法策略區分 15.1.1、分治法 特征:把一個問題拆分成多個小規模的相同子問題&#xff0c;一般可用遞歸解決。 經典問題:斐波那契數列、歸并排序、快速排序、矩陣乘法、二分搜索、大整數乘法、漢諾塔。 15.1.2、貪心法 (一般用于求滿意解) …

基于大模型的唇裂手術全流程預測與應用研究報告

目錄 一、引言 1.1 研究背景與意義 1.2 研究目標與內容 二、唇裂相關醫學知識概述 2.1 唇裂的定義、分類與發病原因 2.2 唇裂對患者生理與心理的影響 2.3 傳統唇裂治療方法與局限性 三、大模型技術原理與應用基礎 3.1 大模型概述 3.2 適用于唇裂預測的大模型類型及特…

環境變量設置異常導致UOS文件管理器無法正常運行

編譯QT5.15.2&#xff0c;與UOS20.9的QT依賴沖突 現象原因解決方法 現象 重啟系統后UOS桌面變成黑色&#xff0c;沒有任何圖標&#xff0c;任務欄的應用本來是有預覽的&#xff0c;但也變得不可用。 原因 找了很久&#xff0c;查到原來是dde-file-manager未能正常啟動。直接…

《認知覺醒》改變的核心方法論

《認知覺醒》改變的核心方法論 一、認知覺醒的核心目標 改變 → 提升能力 → 獲得更好生活 二、大腦運作機制 腦區運算速度作用特點本能腦1.1億次/秒自動化反應&#xff0c;能量消耗低情緒腦1.1億次/秒情感驅動型決策?理智腦?40次/秒戰略指揮官角色 關鍵差異&#xff1a…

Python中的字典:深度解析與應用實踐

一、字典的本質與特性 Python字典&#xff08;Dictionary&#xff09;是以**鍵值對&#xff08;Key-Value Pair&#xff09;**形式存儲數據的無序集合&#xff0c;使用大括號{}定義。其核心特性包括&#xff1a; 快速查找&#xff1a;基于哈希表實現&#xff0c;通過鍵&#…

【藍橋杯python研究生組備賽】005 數學與簡單DP

題目1 01背包 有 N 件物品和一個容量是 V 的背包。每件物品只能使用一次。 第 i 件物品的體積是 vi&#xff0c;價值是 wi。 求解將哪些物品裝入背包&#xff0c;可使這些物品的總體積不超過背包容量&#xff0c;且總價值最大。 輸出最大價值。 輸入格式 第一行兩個整數&a…

2024年國賽高教杯數學建模E題交通流量管控解題全過程文檔及程序

2024年國賽高教杯數學建模 E題 交通流量管控解題 原題再現 隨著城市化進程的加快、機動車的快速普及&#xff0c;以及人們活動范圍的不斷擴大&#xff0c;城市道路交通擁堵問題日漸嚴重&#xff0c;即使在一些非中心城市&#xff0c;道路交通擁堵問題也成為影響地方經濟發展和…

穿越是時空之門(java)

emm&#xff0c;之前做過一道類似的題目&#xff0c;但是這次又忘了 一開始的錯誤代碼 package Lanqiao;import javax.swing.plaf.synth.SynthTextAreaUI; import java.math.BigInteger;/*** author zb* date2025/3/19 21:33*/ public class L19701 {public static void main…

npm : 無法加載文件 C:\Program Files\nodejs\npm.ps1,因為在此系統上禁止運行腳本的處理方法

1、安裝了node.js后&#xff0c;windows powershell中直接輸入npm&#xff0c;然后就報錯 2、出現原因&#xff1a;權限不夠 系統禁用了腳本的執行&#xff0c;所以我們在windows powershell輸入npm -v的時候&#xff0c;就會報上面的錯誤。 3、解決 Set-ExecutionPolicy Un…

藍橋杯單片機之AT24C02(基于自己對AT24C02的學習和理解)

一、先用抽象法說明原理&#xff0c;讓原理變得簡單易懂&#xff1a; 1、向AT24C02寫入數據&#xff1a; 有個關系戶&#xff0c;他想安排自己的兒子進某個大廈里某個樓層的公司&#xff0c;那么他就要先找到這個公司的地址&#xff0c;然后再找到該公司是第幾樓&#xff0c;最…

Java面試易忽略知識點

1. CompletableFuture中thenApply()與thenCompose()的區別 考察點&#xff1a;組合式異步編程 解析&#xff1a; ?**thenApply()**&#xff1a;接收前序任務結果&#xff0c;返回普通對象&#xff08;同步轉換&#xff09;&#xff0c;適用簡單數據處理。?**thenCompose()*…

VLLM專題(十九)—兼容 OpenAI 的服務器

vLLM 提供了一個 HTTP 服務器,能夠實現 OpenAI 的 Completions API、Chat API 等功能! 您可以通過 vllm serve 命令啟動服務器,或者通過 Docker 啟動: vllm serve NousResearch/Meta-Llama-3-8B-Instruct --dtype auto --api-key token-abc123要調用服務器,您可以使用官…

【云原生之kubernetes實戰】在k8s環境中高效部署minio對象存儲(詳細教程)

【云原生之kubernetes實戰】在k8s環境中高效部署minio對象存儲(詳細教程) 前言一、minio介紹1.1 MinIO簡介1.2 主要特點1.3 主要使用場景二、相關知識介紹2.1 本次實踐存儲介紹2.2 k8s存儲介紹三、本次實踐介紹3.1 本次實踐簡介3.2 本次環境規劃3.3 部署前需準備工作四、檢查…

【高項】信息系統項目管理師(八)項目質量管理【3分】

項目質最管理包括把組織的質量政策應用于規劃、管理、控制項目和產品質量要求。以滿足干系人目標的各個過程。項目質量管理以執行組織的名義支持過程的持續改進活動,項目質量管理需要兼顧項目管理與項目可交付成果兩個方面,它適用于所有項目無論項目的可付成果具有何種特性。質…

python-leetcode 48.括號生成

題目&#xff1a; 數字n代表生成括號的對數&#xff0c;設計一個函數&#xff0c;用于生成所有可能并且有效的括號組合。 方法一&#xff1a;回溯 可以生成所有 2**2n 個 ‘(’ 和 ‘)’ 字符構成的序列&#xff0c;然后檢查每一個是否有效即可 為了生成所有序列&#xff0c…

TDE透明加密技術:免改造實現華為云ECS中數據庫和文件加密存儲

在數字經濟與云計算深度融合的今天&#xff0c;華為云ECS&#xff08;彈性云服務器&#xff09;已成為企業數字化轉型的核心載體&#xff0c;承載著數據庫、文件存儲、AI訓練等關鍵業務。然而&#xff0c;云上數據安全形勢日益嚴峻&#xff1a;2024年全球云環境勒索攻擊同比激增…

3D點云數據處理中的聚類算法總結

1.歐式聚類&#xff1a; 基于點的空間距離&#xff08;歐幾里得距離&#xff09;來分割點云&#xff0c;將距離較近的點歸為同一簇。 歐式聚類需要的參數&#xff1a;鄰域半徑R,簇的最小點閾值minPts&#xff0c;最大點數閾值maxPts。 實現效率&#xff1a; O(n * log n) 實現…

PCL--點云可視化

用于單個顯示、多個顯示的頭文件<visual_.h> visual_.h #pragma once #include <iostream> #include <thread> #include <pcl/visualization/pcl_visualizer.h>using namespace std::chrono_literals;/********************************************…