1. 合唱队
题目 大意: 将数组分成若干个数组 ,子数组内部从小到大排序,使所有子数组整体按非递减顺序,求子数组最大数量;
输入
4
2 1 3 2
输出
2
输入
10
69 230 77 650 440 270 750 760 990 880
输出
6
代码:
import java.util.Scanner; public class B9 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); sc.nextLine(); String[] str = sc.nextLine().split(" "); long[] val = new long[n]; for (int i = 0; i < n; i++) { val[i] = Long.valueOf(str[i]); } int[] positions = new int[n]; int count = 0; int index = 0; while(index< n-1){ long min = Long.MAX_VALUE; int i = index; for (; i < n; i++) { if(min> val[i]){ min = val[i]; positions[count]= i; } } if(positions[count]==index){ index = index+1; }else{ index = positions[count]+1; } count++; } System.out.println(count); } }
2. 考场安排
考场不允许两个很熟的异性朋友存在,学习希望通过搬出一部分学生来解决问题,问
第二题,输入n和m,然后输入的是m行对应朋友关系,希望一个教师中容下的学生尽可能多,搬出学生尽可能少,求搬出人数最少且字典序最小的方案
输入:
2 2
1 3
1 4
输出:
1
1
import java.util.Arrays; import java.util.LinkedList; import java.util.Scanner; public class B8 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(); int m = sc.nextInt(); sc.nextLine(); int len = 2*n +1; int[] du = new int[len]; int count = m; boolean[][] relation = new boolean[len][len]; for (int i = 0; i < m; i++) { String[] str = sc.nextLine().split(" "); int val1 = Integer.valueOf(str[0]); int val2 = Integer.valueOf(str[1]); int boy = Math.min(val1, val2); int girl = Math.max(val1, val2); relation[boy][girl] = true; du[boy]++; du[girl]++; } LinkedList<Integer> result = new LinkedList<>(); while(count>0){ int[] tempdu = Arrays.copyOf(du,len); Arrays.sort(tempdu); int maxdu = tempdu[len-1]; if(maxdu==0){ break; } for (int i = 1; i < len; i++) { if(du[i]==maxdu){ for (int j = n; j < len; j++) { if(relation[i][j]){ relation[i][j]=false; du[i]--; du[j]--; count--; } } result.add(i); } } } int alen = result.size(); System.out.println(alen); for (int i = 0; i < alen; i++) { System.out.print(result.get(i)); if(i==alen-1){ System.out.print(" "); } } } }