第一题
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String[] str;
while(sc.hasNextLine()){
str= sc.nextLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int m = Integer.parseInt(str[1]);
int a = Integer.parseInt(str[2]);
int b = Integer.parseInt(str[3]);
str= sc.nextLine().trim().split(" ");
int[] weight = new int[m];
for (int i = 0; i < m; i++) {
weight[i] = Integer.parseInt(str[i]);
}
Arrays.sort(weight);
if(a>b){
int t = b;
b = a;
a = t;
}
if(a>weight[0] || b<weight[m-1]){
System.out.println("NO");
}else if(a==weight[0] && b==weight[m-1]){
System.out.println("YES");
}else if(a==weight[0] && n-m>=1){
System.out.println("YES");
}else if(b==weight[m-1] && n-m>=1){
System.out.println("YES");
}else if(n-m>=2){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
sc.close();
}
}
第二题
import java.io.IOException;
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
String[] str;
while(sc.hasNextLine()){
str = sc.nextLine().trim().split(" ");
int n = Integer.parseInt(str[0]);
int x = Integer.parseInt(str[1]);
str = sc.nextLine().trim().split(" ");
int[] score = new int[n];
for (int i = 0; i < n; ++i) {
score[i] = Integer.parseInt(str[i]);
}
Arrays.sort(score);
if(x==0){
System.out.println(0);
break;
}
if(score[n-1]==0){
System.out.println(0);
break;
}
if(score[0]==score[n-1]){
System.out.println(n);
break;
}
if(score[n-x]==0){
for (int i = n-x; i < n; ++i) {
if(score[i] !=0){
System.out.println(n-i);
break;
}
}
}else{
int count=0;
for(int i=0;i<n-x;++i){
if(score[i]==score[n-x]){
count+=1;
}
}
System.out.println(count+x);
}
}
sc.close();
}
}
第三题
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int T = Integer.parseInt(br.readLine().trim());
while(T-- > 0){
int n = Integer.parseInt(br.readLine().trim());
String[] strArr = br.readLine().trim().split(" ");
int[] yummy = new int[n];
int sum = 0;
for(int i = 0; i < n; i++){
yummy[i] = Integer.parseInt(strArr[i]);
sum += yummy[i];
}
int max = yummy[0];
int min = yummy[0];
int dpMax = yummy[0];
int dpMin = yummy[0];
for(int i = 1; i < n; i++){
dpMax = Math.max(dpMax + yummy[i], yummy[i]);
max = Math.max(max, dpMax);
dpMin = Math.min(dpMin + yummy[i], yummy[i]);
min = Math.min(min, dpMin);
}
System.out.println(Math.max(sum - min, max));
}
}
}
第四题
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int treeSize = sc.nextInt();
Map<Integer, List<Integer>> graph = new HashMap<>();
for (int i = 0; i < treeSize-1; ++i) {
int father = sc.nextInt();
int son = sc.nextInt();
List<Integer> fatherList = graph.getOrDefault(father,new ArrayList<>());
List<Integer> sonList = graph.getOrDefault(son,new ArrayList<>());
fatherList.add(son);
sonList.add(father);
graph.put(father,fatherList);
graph.put(son,sonList);
}
modify(1,graph);
Map<Integer,Integer> dictionary = new HashMap<>();
for (int i = 0; i < treeSize; ++i) {
int color = sc.nextInt();
dictionary.put(i+1,color);
}
int times = sc.nextInt();
Map<Integer,Integer> counter = new TreeMap<>();
for (int i = 0; i < times; i++) {
int root = sc.nextInt();
dfs(root,dictionary,graph,counter);
List<Integer> count = new ArrayList<>(counter.values());
Collections.sort(count);
for (Map.Entry<Integer, Integer> entry : counter.entrySet()) {
if(entry.getValue() == count.get(count.size()-1)){
System.out.println(entry.getKey());
break;
}
}
counter.clear();
}
}
private static void dfs(int root, Map<Integer, Integer> dictionary, Map<Integer, List<Integer>> graph, Map<Integer, Integer> counter) {
int color = dictionary.get(root);
counter.put(color,counter.getOrDefault(color,0)+1);
for(int son : graph.get(root)){
dfs(son,dictionary,graph,counter);
}
}
private static void modify(Integer root, Map<Integer, List<Integer>> graph) {
List<Integer> fatherList = graph.get(root);
if(fatherList.size() == 0){
return;
}
for (Integer son : fatherList) {
List<Integer> sonList = graph.get(son);
sonList.remove(root);
graph.put(son,sonList);
modify(son,graph);
}
}
}