赞
踩
MVP只争夺战
知识点DFS搜索
时间限制: 1s 空间限制: 256MB 限定语言: 不限
题目描述:
在星球争霸篮球赛对抗赛中,强大的宇宙战队,希望每个人都能拿到MVP.
MVP的条件是,单场最高分得分获得者,可以并列,所以宇宙战队决定在比赛中,尽可能让更多的队员上场,且让所有有得分的队员得分都相同。
然而比赛过程中的每一分钟的得分都只能由某一个人包揽
输入描述:
输入第一行为一个数字t,表示有得分的分钟数 ( 1<= t <= 50),第二行为t个数字,代表每一分钟的得分p(1 = p<= 50)
输出描述:
输出有得分的队员都是MVP时最少的MVP得分
示例1
输入:
9
5 2 1 5 2 1 5 2 1
输出:
6
说明:
样例解释:一共4人得分,分别都为6分
5 + 1
5 + 1
5 + 1
解题思路:
import java.util.*; public class My { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int len = sc.nextInt(); sc.nextLine(); String[] p = sc.nextLine().split(" "); int[] scores = new int[len]; for (int i = 0; i < p.length; i++) { scores[i] = Integer.parseInt(p[i]); } int count = Arrays.stream(scores).sum(); Arrays.sort(scores); //对数组进行排序, int min = scores[len - 1]; //求出数组中最大值,为MVP最低得分,因为每个MVP的得分是一样的 int res = 0; boolean[] used = new boolean[scores.length]; //以2个人平分的分数为边界 for (int i = min; i <= count / 2; i++) { if (count % i == 0) { //得分总数可以整除得分 int score = i; //当前平均分 Arrays.fill(used, false); //num表示可以由几组数组成 int num = count / i; boolean canCombine = true; //要求每一组都能够从scores数组里找到组合,否则就不符合 for (int j = 0; j < num; j++) { canCombine = canCombine(scores, used, score, len - 1); //只要由一组没能组合完成,说明不符合 if (!canCombine) { break; } } if (canCombine) { res = score; break; } } } System.out.println(res == 0 ? count : res); //分数平分不成功则输出总分 } public static boolean canCombine(int[] scores, boolean[] used, int score, int index) { if (score == 0) { return true; } for (int i = index; i >= 0; i--) { //已经使用,进入下一个循环 if (used[i]) { continue; } //当前的分数比目标分值大,不符合 int curVal = scores[i]; if (curVal > score) { continue; } //记录下标已经被使用 used[i] = true; return canCombine(scores, used, score - curVal, i - 1); } return false; } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。