求最多可以派出多少支团队
标题:求最多可以派出多少支团队 | 时间限制:1秒 | 内存限制:262144K | 语言限制:不限
用数组代表每个人的能力,一个比赛活动要求参赛团队的最低能力值为N,每个团队可以由1人或2人组成,且1个人只能参加1个团队,请计算出最多可以派出多少支符合要求的团队
import java.io.*; import java.util.*; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int personCnt = sc.nextInt(); int[] powers = new int[personCnt]; for (int i = 0; i < personCnt; i++) { powers[i] = sc.nextInt(); } int powerReq = sc.nextInt(); Arrays.sort(powers); int i = 0; int j = powers.length - 1; int count = 0; while(i < j) { if (powers[j] >= powerReq) { count++; j--; } else if (powers[i] + powers[j] >= powerReq) { count++; i++; j--; } else { i++; } } if (powers[j] >= powerReq) { count++; } System.out.println(count); } }