每日一题----P4715 淘汰赛 (4.15)
题目连接:P4715 【深基16.例1】淘汰赛
这个题就是一个稍微复杂一点的模拟题,需要找出来每一次每一组中哪一组晋级了,同时还需要同时保存它的能力值和序号,因此需要创建辅助类
Node
我使用的是数组来记录所有的晋级情况,score[0]表示刚读进来的输入,score[1]表示第一轮结束后的情况,依次类推,那么score[n-1]就表示最后一轮的情况,其中能力值较小者就是我们需要的答案
对于题目所给样例中,数组中值如下(这里仅写出了能力值,另外还需要有该国家的序号)
package cn.edu.xjtu.daily.April.day_4_15;
import java.util.Scanner;
/**
* @author Hydrion-QLz
* @date 2022-04-15 9:20
* @description P4715 【深基16.例1】淘汰赛 https://www.luogu.com.cn/problem/P4715
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int num = (int) Math.pow(2, n);
// 读取输入
Node[][] score = new Node[n + 1][num];
for (int i = 0; i < num; i++) {
int ability = sc.nextInt();
score[0][i] = new Node(i + 1, ability);
}
// 数据处理
for (int i = 1; i < n; i++) {
int idx = 0;
int end = (int) Math.pow(2, n - i + 1);
for (int j = 0; j < end; j += 2) {
// 获得晋级结点
score[i][idx] = getMax(score[i - 1][j], score[i - 1][j + 1]);
// 继续看下一组
idx++;
}
}
// 输出最后一轮能力比较低的国家的序号
System.out.println(score[n - 1][1].ability > score[n - 1][0].ability ? score[n - 1][0].number : score[n - 1][1].number);
}
/**
* 获得两个结点中晋级的结点
*
* @param node1
* @param node2
* @return
*/
private static Node getMax(Node node1, Node node2) {
return node1.ability > node2.ability ? node1 : node2;
}
}
class Node {
int number;
int ability;
public Node(int number, int ability) {
this.number = number;
this.ability = ability;
}
}