import java.util.*;
import java.util.stream.Collectors;
class Main {
public static void main(String[] args) {
// 输入处理
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Integer[][] ranges = new Integer[n][3];
for (int i = 0; i < n; i++) {
ranges[i][0] = in.nextInt();
ranges[i][1] = in.nextInt();
ranges[i][2] = in.nextInt();
}
// 区间排序
Arrays.sort(ranges, (a, b) -> a[0] - b[0]);
HashSet
for (Integer[] se : ranges) {
points.add(se[0]);
points.add(se[1]);
}
// 点排序
Integer[] points_arr = points.toArray(new Integer[0]);
Arrays.sort(points_arr, (a, b) -> a - b);
int result = 0;
HashSet
//遍历每一个点
for (Integer point : points_arr) {
int current_count = 0;
// 判断这个点是否在此区间内
for (int i = 0; i < ranges.length; i++) {
if (ignore.contains(i)){
continue;
}
if (point < ranges[i][0]){
break;
}
else if (point < ranges[i][1]){
current_count += ranges[i][2];
}
else{
ignore.add(i);
}
}
result = Math.max(result, current_count);
}
System.out.println(result);
}
}
新解法
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
import java.math.BigInteger;
import java.util.stream.Stream;
class Main {
public static void main(String[] args) {
// 输入处理
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] ranges = new int[n][3];
for (int i = 0; i < n; i++) {
ranges[i][0] = in.nextInt();
ranges[i][1] = in.nextInt();
ranges[i][2] = in.nextInt();
}
System.out.println(minMeetingRooms(ranges));
}
public static int minMeetingRooms(int[][] ranges) {
Arrays.sort(ranges, (a, b) -> a[0] - b[0]);
PriorityQueue
int res = 0;
int temp_res = 0;
for (int i = 0; i < ranges.length; i ++ ) {
while (pq.size() > 0) {
Integer[] top = pq.peek();
if (top[0] < ranges[i][0]) {
Integer[] poll = pq.poll();
temp_res -= poll[1];
} else {
break;
}
}
pq.offer(new Integer[] {ranges[i][1], ranges[i][2]});
temp_res += ranges[i][2];
if (temp_res > res) {
res = temp_res;
}
}
return res;
}
}
import java.util.Scanner;
import java.util.*;
import java.util.stream.Collectors;
public class Main {
public static int max_machine=0;
public static void main(String[] args) {
//处理输入
Scanner in=new Scanner(System.in);
int m = in.nextInt();
int n = in.nextInt();
ArrayList
for (int i=0;i int a = in.nextInt(); weights.add(a); //System.out.println(s); } Collections.sort(weights); //第二步,左右指针向中间移动 int left=0; int right = weights.size()-1; //结果 int min_bikes = 0; //当前重量 int temp_weight = weights.get(right) + weights.get(left); // 题目中有两个隐含的条件 // 1: 一辆车最多骑两个人 // 2:人的重量不可能大于车的载重 while(left if (temp_weight > m) { right --; min_bikes += 1; temp_weight = weights.get(right) + weights.get(left); } else{ right --; left ++; min_bikes += 1; temp_weight = weights.get(right) + weights.get(left); } } // 感谢评论区老铁点拨 if (left == right) { min_bikes++; } System.out.println(min_bikes); } } import java.util.Scanner; import java.util.*; public class Main { public static int max_machine=0; public static void main(String[] args) { //处理输入 Scanner in=new Scanner(System.in); String str = in.nextLine(); String[] str1 = str.split(" "); // 第一步,单词内部调整 ArrayList for (int i=0;i char[] chars = str1[i].toCharArray(); Arrays.sort(chars); str_list.add(String.valueOf(chars)); //System.out.println(s); } //第二步,单词间调整 //先统计每个单词出现的次数 HashMap for (int i=0;i if(str_count.containsKey(str_list.get(i))) { str_count.put(str_list.get(i), str_count.get(str_list.get(i)) + 1); } else { str_count.put((str_list.get(i)), 1); } } // 按次数排序 // 通过ArrayList构造函数把 str_count.entrySet()转换成list List // 通过比较器实现比较排序 Collections.sort(str_count_list, new Comparator public int compare(Map.Entry //return ~mapping1.getValue().compareTo(mapping2.getValue()); if (a.getValue() > b.getValue()) { return -1; } else if(a.getValue() == b.getValue()) { if (a.getKey().length() > b.getKey().length()) { return 1; } else if (a.getKey().length() == b.getKey().length()) { return a.getKey().compareTo(b.getKey()); } else { return -1; } } else { return 1; } } }); String result=""; for (int i=0;i for (int j=0;j result = result +str_count_list.get(i).getKey() + " "; } } System.out.println(result); } } import java.util.Scanner; import java.util.*; class Main { public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); String param_str = in.nextLine(); String[] param_list = param_str.split(" "); int m = Integer.valueOf(param_list[0]); int n = Integer.valueOf(param_list[1]); //构造输入数据结构 ArrayList for (int i=0;i String edge_str = in.nextLine(); String[] edge_point = edge_str.split(" "); ArrayList singleEdge.add(Integer.valueOf(edge_point[0])); singleEdge.add(Integer.valueOf(edge_point[1])); edgeArrayList.add(singleEdge); } int count = 0; //遍历所有可能的组合 举例: 10001 -> i 的二进制表达 for (int i = 0; i < (1 << m); i++) { boolean flag = true; for (int j=0;j // 检测所有的边相连的是否同为红颜色 if (((i >> (m-edgeArrayList.get(j).get(0))) & 1) == 1 && ((i >> (m-edgeArrayList.get(j).get(1))) & 1)==1) { flag = false; break; } } if(flag) { count++; } } System.out.println(count); } } import java.util.Scanner; import java.util.*; class Main { public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); String param_str = in.nextLine(); int count = Integer.valueOf(param_str); //构造输入数据结构,并求和 int[] nums = new int[count]; String num_str = in.nextLine(); int sum = 0; String[] num_list = num_str.split(" "); for (int i=0;i nums[i] = Integer.valueOf(num_list[i]); sum += Integer.valueOf(num_list[i]); } // 最大可以等分为m个子数组 for (int i=count;i>0;i--) { //从最大的可能行开始,满足条件即为为最小的情况 if (canPartitionKSubsets(nums, i, sum)) { System.out.println(sum / i); break; } } } public static boolean canPartitionKSubsets(int[] nums, int k, int all) { if (all % k != 0) { return false; } int per = all / k; Arrays.sort(nums); int n = nums.length; if (nums[n - 1] > per) { return false; } boolean[] dp = new boolean[1 << n]; int[] curSum = new int[1 << n]; dp[0] = true; for (int i = 0; i < 1 << n; i++) { if (!dp[i]) { continue; } for (int j = 0; j < n; j++) { if (curSum[i] + nums[j] > per) { break; } if (((i >> j) & 1) == 0) { int next = i | (1 << j); if (!dp[next]) { curSum[next] = (curSum[i] + nums[j]) % per; dp[next] = true; } } } } return dp[(1 << n) - 1]; } } import java.util.Scanner; import java.util.*; class Main { // 节点定义 public static class ListNode { public int value; public int next; public ListNode(int value, int next) { this.value = value; this.next = next; } } public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); String head_node_str = in.nextLine(); String[] head_info = head_node_str.split(" "); int head_addr = Integer.valueOf(head_info[0]); int count = Integer.valueOf(head_info[1]); HashMap for (int i=0;i String node_str = in.nextLine(); String[] node_list_info = node_str.split(" "); int addr = Integer.valueOf(node_list_info[0]); int val = Integer.valueOf(node_list_info[1]); int next = Integer.valueOf(node_list_info[2]); ListNode temp_node = new ListNode(val, next); node_info.put(addr, temp_node); } // 构造链表,剔除无效节点 int size = 1, cur = 0; ListNode head = node_info.get(head_addr); ListNode thead = head; while (thead.next != -1) { size++; thead = node_info.get(thead.next); } // 找中间节点 while (head.next != -1) { if ((size / 2) == cur) { System.out.println(head.value); } head = node_info.get(head.next); cur++; } } } import java.util.Scanner; import java.util.*; class Main { public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); int left = in.nextInt(); int right = in.nextInt(); int result = right - left + 1; for (int i=left; i<=right; i++) { String number_str=Integer.toBinaryString(i); if (number_str.contains("101")) { result -= 1; } } System.out.println(result); } } 数位DP import java.util.Scanner; import java.util.*; import java.util.stream.Collectors; import java.math.BigInteger; import java.util.stream.Stream; import java.time.ZoneId; import java.time.Instant; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeFormatterBuilder; class Main { public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); int left = in.nextInt(); int right = in.nextInt(); System.out.println(dp(right)-dp(left-1)); } public static int dp(int num) { // 10 -> [1,0,1,0,0] String number_str=Integer.toBinaryString(num); Integer[] single_binary_nums = new Integer[number_str.length()]; for (int i=0;i single_binary_nums[i] = Integer.parseInt(number_str.charAt(i)+""); } int[][][] binary_dp = new int[single_binary_nums.length][2][2]; return search(0, true, binary_dp, single_binary_nums, 0, 0); } public static int search(int p, boolean flag, int[][][] binary_dp, Integer[] single_binary_nums, int pre, int prepre) { if (p == single_binary_nums.length) { return 1; } if (!flag && binary_dp[p][pre][prepre] != 0) { return binary_dp[p][pre][prepre]; } int index = flag ? single_binary_nums[p] : 1; int count = 0; for (int i = 0; i < index+1; i++) { if (i == 1 && pre == 0 && prepre == 1) { continue; } count += search(p + 1, flag && i == index, binary_dp, single_binary_nums, i, pre); } if (!flag) { binary_dp[p][pre][prepre] = count; } return count; } } import java.util.Scanner; import java.util.*; class Main { public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); String input_str = in.nextLine(); String[] input_list = input_str.split(" "); int[] cars = new int [input_list.length]; for (int i=0;i cars[i] = Integer.parseInt(input_list[i]); } String window_size_str = in.nextLine(); int window_size = Integer.valueOf(window_size_str); // 初始化滑动窗口 int[] car_count = new int[3]; for (int i=0;i car_count[cars[i]] += 1; } //滑动窗口向前滑 int max_res = Math.max( Math.max(car_count[0], car_count[1]), car_count[2]); for (int i = window_size; i < cars.length; i++) { car_count[cars[i]] += 1; car_count[cars[i-window_size]] -= 1; max_res = Math.max(max_res, Math.max( Math.max(car_count[0], car_count[1]), car_count[2])); } System.out.println(max_res); } } import java.util.Scanner; import java.util.*; import java.util.stream.Collectors; class Main { public static int min_times; public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); List while(in.hasNextLine()){ list.add(in.nextLine()); } int n = Integer.parseInt(list.get(0)); Deque int start = 1; int count = 0;//这个是移动次数 for(int j = 1;j String str = list.get(j); //如果是移除指令 if(str.equals("remove")){ if(!deque.isEmpty()){ //System.out.println("before:"); //System.out.println(deque.peekFirst()); //System.out.println(start); if(deque.peekFirst()==start){ //如果队列不为空,且移除来的就是start那么可以移除 deque.removeFirst(); start++; }else{ //此时就需要调整顺序了 List while(!deque.isEmpty()){ list1.add(deque.removeFirst()); } Collections.sort(list1); for(int temp = 0;temp deque.addLast(list1.get(temp)); } deque.removeFirst(); count++; start++; } } continue; } String[] strs = str.split(" "); //如果是添加指令 if( strs[1].equals("add")){ if(strs[0].equals("head")){ //从头部添加 deque.addFirst(Integer.parseInt(strs[2])); }else{ //从尾部添加 deque.addLast(Integer.parseInt(strs[2])); } } System.out.println(deque); } System.out.println(count); } } 非模拟方法 import java.util.Scanner; import java.util.*; import java.util.stream.Collectors; class Main { public static int min_times; public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); List while(in.hasNextLine()){ operaions.add(in.nextLine()); } int n = Integer.parseInt(operaions.get(0)); int queue_size = 0; //是否有序 boolean in_order = true; int result = 0; for (int i = 1; i < operaions.size(); i++) { String operaion_str = operaions.get(i); if (operaion_str.startsWith("head add")) { if (queue_size > 0 && in_order) { in_order = false; } queue_size++; } else if (operaion_str.startsWith("tail add")) { queue_size++; } else { if (queue_size == 0) { continue; } if (!in_order) { result++; in_order = true; } queue_size--; } } System.out.println(result); } } import java.util.Scanner; import java.util.*; import java.util.stream.Collectors; class Main { public static int min_times; public static void main(String[] args) { // 处理输入 Scanner in = new Scanner(System.in); String[] strs = in.nextLine().split(" "); // 将所有字符串放入哈希集合 HashSet for (String s : strs) { word_set.add(s); } // 真正的密码 String true_pass_word=""; //按顺序检查每一个词 for (String s : strs) { // 条件1:检查这个词所有以索引0开头的子串在数组中是否都有 boolean flag=true; for(int i=1;i // 以索引0开头的子串 String sub_str=s.substring(0,i); if(!word_set.contains(sub_str)){ flag=false; break; } } if(flag){ // 条件2:比较密码长度 if(s.length()>true_pass_word.length()) true_pass_word=s; // 条件3:比较密码字典排序 if(s.length()==true_pass_word.length()&&s.compareTo(true_pass_word)>0){ true_pass_word=s; } } } System.out.println(true_pass_word); } }3【字符串重新排列】100
4【无向图染色】200
5【等和子数组最小和】100
6-100【单向链表中间节点】
7-100【不含 101 的数】
8-100【最多颜色的车辆】
9-100【最小调整顺序次数】
10-100【真正的密码】