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

2024CCPC吉林省赛长春邀请赛 Java 做题记录

目录

I. The Easiest Problem

G. Platform Game

L. Recharge

E. Connected Components


I. The Easiest Problem

签到题 直接输出 21 即可

// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;/*** 题目地址**/// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static final int mod = (int) (1e9 + 7);
//    static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();static int map[][];/*** @throws IOException*/private static void solve() throws IOException {// todoString str="Scan the QR code to sign in now.";int cnt=0;for(int i=0;i<str.length();i++) {char c=str.charAt(i);if(c>='a'&&c<='z')cnt++;}dduoln(cnt);}// Java // 判断是不是回文字符串public static boolean isPalindrome(String str) {if (str == null) {return false;}int left = 0;int right = str.length() - 1;while (left < right) {if (str.charAt(left) != str.charAt(right)) {return false;}left++;right--;}return true;}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);}
}/*** IoScanner类** @author Dduo* @version 1.0* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。*/
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());}
}

G. Platform Game

模拟题 自定义比较器

// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;/*** 题目地址**/// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static final int mod = (int) (1e9 + 7);
//    static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();static int map[][];/*** @throws IOException*/private static void solve() throws IOException {// todoint n=sc.nextInt();ArrayList<Long[]>list=new ArrayList<>();for(int i=0;i<n;i++) {long l=sc.nextLong(); // 左端long r=sc.nextLong(); // 右端long y=sc.nextLong(); // 高度list.add(new Long[] {l,r,y});}Collections.sort(list,(o1,o2)->{if(o1[2]==o2[2]) {return Long.compare(o1[0], o2[0]);}else {return Long.compare(o2[2], o1[2]);}});//    	for(Long arr[]:list) {
//    		dduoln(arr[0]+" "+arr[1]+" "+arr[2]);
//    	}long x=sc.nextLong(); // 初始横坐标long h=sc.nextLong(); // 初始高度for(Long arr[]:list) {// 当前平台的高度long height = arr[2];if(height>h)continue;else h=height;// 当前平台的左端点long left = arr[0]; // 当前平台的右端点long right = arr[1];if(x>left&&x<right) {x=right;}else {continue;}}dduoln(x);}/*274 6 112 14 25 11 31 6 411 13 44 7 53 5 64 710 4 11 5* */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);}
}/*** IoScanner类** @author Dduo* @version 1.0* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。*/
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());}
}

L. Recharge

一道贪心模拟

// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;/*** 题目地址**/// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static final int mod = (int) (1e9 + 7);
//    static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();static int map[][];/*** @throws IOException*/private static void solve() throws IOException {// todolong k=sc.nextLong(); // 容量long x=sc.nextLong(); // 小房间long y=sc.nextLong(); // 大房间long cnt=0;if(k==1) {cnt+=x;cnt+=y;dduoln(cnt);return;}if(k%2==0) {// 偶数long a1=k/2; // 需要a1个大房间 正好进行充能//        	dduoln(a1);if(y%a1==0) {// y被全耗尽cnt+=y/a1;// 最后操作yy=0; }else {// y还剩下全耗尽long a2 = y / a1; // 可以充能几组cnt += a2;y -= a2*a1; // 大房间还剩下多少个}// 首先把大房间消耗完long a3=k-y*2; // 需要多少个小房间抵消大房间 //        	dduoln(a3);if(a3>x) {}else {cnt++;x-=a3;cnt+=x/k;}}else if(k%2!=0){// 奇数 较为复杂// 9 4*2+1// 凑最优解// 凑整 long a4=(k-1)/2; // 需要a4个大房间  1个小房间 正好进行充能long min1=x/1;long min2=y/a4;long min=Math.min(min1,min2);cnt+=min;x-=min*1;y-=min*a4;//        	dduoln(x+" "+y);//        	dduoln(cnt);if(y==0) {// 没有大房间了cnt+=x/k;}else if(x==0){// 没有小房间了cnt+=y/(k/2+1);}else {// 大房间 小房间都有// 先凑一个//        		dduoln(x+" "+y);long a5=k-y*2; // 需要a5个小房间凑整if(a5>x) {}else {cnt++;x-=a5;y=0;cnt+=(x/k);}}//        	dduoln(cnt);}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() {System.out.println("");}static <T> void dduoln(T t) {System.out.println(t);}
}/*** IoScanner类** @author Dduo* @version 1.0* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。*/
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. Connected Components

数据结构

单调栈 模版题

// @github https://github.com/Dddddduo
// @github https://github.com/Dddddduo/acm-java-algorithm
// @github https://github.com/Dddddduo/Dduo-mini-data_structure
import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
import java.time.*;/*** 题目地址**/// xixi♡西
public class Main {static IoScanner sc = new IoScanner();static final int mod = (int) (1e9 + 7);
//    static final int mod = (int) (998244353);static int n;static int arr[];static boolean visited[];static ArrayList<ArrayList<Integer>> adj = new ArrayList<>();static int map[][];/*** @throws IOException*/private static void solve() throws IOException {// todoint n=sc.nextInt();ArrayList<Long []>list=new ArrayList<>();for(int i = 1 ; i<=n ; i++) {long a=sc.nextLong();long b=sc.nextLong();list.add(new Long[]{ (a-i) , (b-i) });}Collections.sort(list,(o1,o2)->{if(o1[0]==o2[0]) {return Long.compare(o2[1], o1[1]);}return Long.compare(o1[0], o2[0]);});//    	for(Long[] arr : list) {
//    		dduoln(arr[0]+" "+arr[1]);
//    	}// 单调栈Stack<Long> stack=new Stack<>();for (int i = 0; i < n; i++) {Long min = (long) -1e18;Long num=list.get(i)[1];int flag = 0;if (!stack.empty()) {min = stack.peek();}if (!stack.empty() && stack.peek() >= num) {flag = 1;stack.pop();}if (flag == 1) {stack.push(min);} else if(flag == 0){stack.push(num);}}dduoln(stack.size());}/**4-6 361 2114 910 8*/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);}
}/*** IoScanner类** @author Dduo* @version 1.0* @description 通过IO流操作缓冲区减少了与底层输入输出设备的交互次数,旨在简化 Java 中的标准输入读取操作。*/
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());}
}

相关文章:

  • MAC常用操作整理
  • codeup添加流水线docker自动化部署
  • RSP-BSP-1
  • 使用 nvm 管理 Node.js 和 npm 版本
  • 《Effective Python》第三章 循环和迭代器——在遍历参数时保持防御性
  • 前端(vue)学习笔记(CLASS 6):路由进阶
  • Redis有哪些常用应用场景?
  • MySQL企业版免费开启,强先体验
  • 【Vue篇】潮汐中的生命周期观测站​
  • 深入掌握MyBatis:连接池、动态SQL、多表查询与缓存
  • ubuntu下配置vscode生成c_cpp_properties.json
  • Unity 如何使用Timeline预览、播放特效
  • 【NLP】36. 从指令微调到人类偏好:构建更有用的大语言模型
  • AI大模型从0到1记录学习numpy pandas day25
  • 两数之和 - 简单
  • 面试题之进程 PID 分配与回收算法:从理论到 Linux 内核实现
  • 【NLP】35. 构建高质量标注数据
  • 质检LIMS系统检测数据可视化大屏 全流程提效 + 合规安全双保障方案
  • 本地部署Immich系统结合Cpolar技术实现安全跨设备影像管理方案
  • 【爬虫】DrissionPage-8.1
  • 贵州茅台:支持工作餐不上酒的规定,请投资者相信茅台创新和自我调节能力
  • 这位中国电影早期的全能奇才,90年前唱响国歌
  • 河南通报部分未检疫生猪流入:立案查处,涉案猪肉被封存
  • 国家统计局:4月全国城镇调查失业率为5.1%,比上月下降0.1个百分点
  • 以色列称“将立即允许恢复”人道主义物资进入加沙
  • 慢品巴陵,看总编辑眼中的岳阳如何书写“山水人文答卷”