zoukankan      html  css  js  c++  java
  • 二叉树P4715 【深基16.例1】淘汰赛【数塔思想】

    题目

    https://www.luogu.com.cn/problem/P4715

    分析

     这道题我是使用了动态规划思想的数塔的做法,从数塔底层开始比较,一层一层网上找,找到第二层在比较找出亚军(数组的最低维来表示index与国家的能力值)

    但是最简单的思路:么的把 n 支队伍分成两个区间,一个上半区,一个下半区。那么上半区最强者与下半区最强者,必是一冠一亚。

    代码

    #include<iostream>
    #include<cstdio>
    #include<cmath>
    #include<algorithm>
    using namespace std;
    int list[200][200][2];
    int temp[200][2];
    int main()
    {
        int n;
        scanf("%d", &n);
        int amount = pow(2, n);
        n++;
        for (int i = 1; i <= amount; i++)
        {
            int t;
            scanf("%d", &t);
            temp[i][1] = i;
            temp[i][0]=t;
            
        }
        for (int j = 1; j <= amount; j++)
        {
            list[n][j][0] = temp[j][0];
            list[n][j][1] = temp[j][1];
        }
        for (int i = n; i > 2; i--)
            for (int j = 1; j <= pow(2, i-1); j += 2)
            {
                if (list[i][j][0] > list[i][j+1][0])
                {
                    list[i - 1][(j+1)/2][0] = list[i][j][0];
                    list[i - 1][(j+1)/2][1] = list[i][j][1];
                }
                else
                {
                    list[i - 1][(j+1)/2][0] = list[i][j+1][0];
                    list[i - 1][(j+1)/2][1] = list[i][j+1][1];
                    
                }
            }
        if (list[2][1][0] > list[2][2][0])printf("%d", list[2][2][1]);
        else printf("%d", list[2][1][1]);
    }
  • 相关阅读:
    vmware12中安装MAC OS X 10.10
    AOL search
    [python]错误检测及异常处理try-except
    [推荐]
    [python]打开文件操作open用法
    MAC使用小技巧
    [推荐]
    [python]python中的if, while, for
    [python]python字典
    [python]python列表、元组
  • 原文地址:https://www.cnblogs.com/Jason66661010/p/13197939.html
Copyright © 2011-2022 走看看