这题
贪心第一步
排序
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int m = sc.nextInt();
int n = sc.nextInt();
int x[] = new int[n];
for (int i = 0; i < n; i++) {
x[i] = sc.nextInt();
}
Arrays.sort(x);
int a = 0; //数列的列头
int b = x.length - 1; //数列的排尾
int c = 0; //用于记录配的组数
while (a <= b) {
if (x[a] + x[b] > m) { //两个排不了就一个一组
b--;
c++;
} else {
b--;
a++;
c++;
}
}
System.out.println(c);
}
}
}
头一次写的轻松;