Codeforces-1260-E. Tournament贪心

2023-11-18

题目描述:

You are organizing a boxing tournament, where n boxers will participate (n is a power of 2), and your friend is one of them. All boxers have different strength from 1 to n, and boxer i wins in the match against boxer j if and only if i is stronger than j.
The tournament will be organized as follows: n boxers will be divided into pairs; the loser in each pair leaves the tournament, and n2 winners advance to the next stage, where they are divided into pairs again, and the winners in all pairs advance to the next stage, and so on, until only one boxer remains (who is declared the winner).
Your friend really wants to win the tournament, but he may be not the strongest boxer. To help your friend win the tournament, you may bribe his opponents: if your friend is fighting with a boxer you have bribed, your friend wins even if his strength is lower.
Furthermore, during each stage you distribute the boxers into pairs as you wish.

The boxer with strength i can be bribed if you pay him ai dollars. What is the minimum number of dollars you have to spend to make your friend win the tournament, provided that you arrange the boxers into pairs during each stage as you wish?

Input

The first line contains one integer n (2≤n≤218) — the number of boxers. n is a power of 2.

The second line contains n integers a1, a2, …, an, where ai is the number of dollars you have to pay if you want to bribe the boxer with strength i. Exactly one of ai is equal to −1 — it means that the boxer with strength i is your friend. All other values are in the range [1,109].

Output

Print one integer — the minimum number of dollars you have to pay so your friend wins.

Examples
Input

4
3 9 1 -1

Output
0
Input

8
11 -1 13 19 24 7 17 5

Output
12
Note

In the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.
In the second test case you can distribute boxers as follows (your friend is number 2):
1:2,8:5,7:3,6:4 (boxers 2,8,7 and 6 advance to the next stage);
2:6,8:7 (boxers 2 and 8 advance to the next stage, you have to bribe the boxer with strength 6);
2:8 (you have to bribe the boxer with strength 8);

题意:
有n个人,第i个人有力量值i,这n个人中,每次对局两两之间进行solo,如果说一个人的力量之大于他的对手,这个人是赢的,赢得比赛的选手会进入下一轮比赛中。
然而里面有一个人为你的朋友,他或许并不是里面最强的人,要想让朋友成为冠军,就需要对必要的人进行贿赂,求出最少需要花费多少才能使得朋友成为冠军。在每次对局中,都可以进行任意的对局安排,对局安排只取决于自己

题解:
首先放上qsc学长的B站链接

首先有几个性质:
① 如果说朋友在进行比赛的时候干掉了三个人:
a[p1] a[p2] a[p3] ,那么从贪心的角度来讲,当p1 < p2 < p3的时候,这种情况才是最优的
因为第一个人a[p1] 相比较于a[p2]来讲,更容易死,换句话说就是第二个人更容易活下来,在接下来的对局中就有可能再次被选
② 如果说朋友不是最强的,那么必然要贿赂最后一个人a[n]
因为只要有最后一个人存在,这个人就会阻止朋友成为冠军
③ 在第k轮中,前面的2k - 1个人是不能够选择的,只能够选择后面的半部分,并且在每次中,只能够选择区间内的最小值来办证贪心的最优
(比如在第一次对局中,我可以安排朋友选择任意一个人来进行对局,但是在第二次对局中,我就不能够再次选择1号人物,因为这个人并不能够活到第二次对局,在第一次对局就已经被ko了,不是被自己ko就是被比1强的其他人ko)
Code:
要注意只在二的幂次的时候进行选择

ll a[maxn];
multiset<ll> s;
int main() {
    int n; cin >>n;
    for(int i=1;i<=n;i++) {
        a[i] = read;
    }
    ll sum = 0;
    for(int i = n;i >= 1;i --){
        if(a[i] == -1) break;
        s.insert(a[i]);
        if((i & (i - 1)) == 0){
            sum += *s.begin();
            s.erase(s.begin());
        }
    }
    cout << sum << endl;
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Codeforces-1260-E. Tournament贪心 的相关文章

  • 数据库 --- 约束

    一 什么是约束 常见的约束有那些 约束是在创建表的时候 可以给表的字段添加相应的约束 添加约束的目的是为了保证表中数据的合法性 有效性 完整性 常见的约束有 非空约束 not null 唯一约束 unique 主键约束 primary ke
  • Some Tips in Life

    How to Find Digital Books 1 http so baiduyun me 百度云搜索 2 http www zhaofile com 找文件 3 http www cnepub com 掌上书苑 4 http vdis
  • java定义时钟类clock_Java 编程题,定义一个时钟类(Clock)

    题目 Java 编程题 定义一个时钟类 Clock 要求如下 1 存储时钟的时hour 0 23 分minute 0 59 秒second 0 59 2 创建新对象时默认为0时0分0秒 3 设置时钟为指定的时间 4 使时钟前进1秒钟的功能i
  • jstat 命令

    NAME jstat Monitors Java Virtual Machine JVM statistics This command is experimental and unsupported SYNOPSIS jstat Opti
  • mongoDB数据库----简介

    目录 目录 一 NoSQL 1 关系型数据库遵循ACID规则 2 分布式系统 3 分布式计算的优点 4 分布式计算的缺点 5 什么是NoSQL 6 NoSQL 简史 7 NoSQL的优点 缺点 8 NoSQL 数据库分类 二 MongoDB
  • 你知道ChatGPT有哪些商业价值吗?不知道,那没意思

    这段时间 热度zui大的是什么 答案是 ChatGPT 去年11月底上线 当时仅在AI和科技圈内小火了一把 没想到在今年春节后 火爆出圈 ChatGPT的爆火 对商家和品牌方 还有投资创业者来说 是个机遇 普通人虽然很难参与到这些高科技的投
  • Python 求两个正整数的最大公约数

    辗转相除法 思路 1 将两整数求余 a b x 2 如果x 0 则b为最大公约数 3 如果x 0 则 a b b x 继续从1开始执行 4 也就是说该循环的是否继续的判断条件就是x是否为0 代码如下 def main a int input
  • javascript经典代码推荐

  • 基于Matlab的高精度轨道传播器模拟

    基于Matlab的高精度轨道传播器模拟 传播器模拟是一种常见的工程方法 用于预测和分析卫星 火箭或其他天体在轨道上的运动 在这篇文章中 我们将使用Matlab编写一个高精度轨道传播器模拟器 并提供相应的源代码 轨道传播器模拟器的主要目标是根
  • FRP服务器搭建成功后,配置多个客户端使用

    FRP内网穿透服务器搭建成功后 在服务器后台启动FRP 然后还需要两步 第一 在域名购买的网站 比如阿里云 配置一条所有子域名到服务器IP的规则 第二 配置多个客户端 A电脑的配置信息如下 common server addr 服务器IP

随机推荐

  • 前端八股文系列(四)4 JavaScript

    文章目录 前端八股文系列 四 4 JavaScript JS中的8种数据类型及区别 JS中的数据类型检测方案 1 typeof 2 instanceof 3 Object prototype toString call instanceof
  • LeetCode-1781. 所有子字符串美丽值之和【哈希表,字符串,计数】

    LeetCode 1781 所有子字符串美丽值之和 哈希表 字符串 计数 题目描述 解题思路一 简单暴力 双层循环 重点是分别记录子字符串 i j 的最大最小频率 注意这里当i变的时候 所有字符出现的频率就清理 否则在原来的基础上加就行 解
  • 栈的应用一之括号匹配问题

    括号匹配问题 给一个类似这样的字符串 char a abc 检测三种括号的左右括号是否匹配 分析 先取出一个字符 并判断是不是括号 任意括号 1 不是括号 取下一个字符 2 是括号 1 是左括号 压栈 2 是右括号 和栈顶元素比较 栈空 前
  • 教程:使用C#实现PDF文件和字节数组的相互转换

    字节数组有助于存储或传输数据 同样 PDF文件格式因其功能和兼容性而广受欢迎 可以使用C 语言将PDF文件转换为字节数组 也可以将字节数组转换为PDF文件 这可以帮助更有效地在数据库中存储和归档PDF文件 还可以通过使用字节数组来序列化数据
  • CMake中target_compile_definitions的使用

    CMake中的target compile definitions命令用于向target添加编译定义 其格式如下 target compile definitions
  • 什么是DDoS攻击?如何抵御DDos攻击?

    什么是DDoS攻击 如何抵御DDos攻击 单纯的土豆 2016 05 23 安全报道显示2015年DDoS攻击强度创下新纪录 那么DDoS到底是什么呢 了解一些 对产品经理与后台的同事沟通有好处 分布式拒绝服务 DDoS Distribut
  • mac 卸载 XCode

    1 卸载之前的XCode 命令行执行下面命令 sudo Developer Library uninstall devtools mode all sudo Developer Library uninstall developer fol
  • Springboot 集成 minio分享以及小坑 和 单机部署

    第一步先引入minio依赖
  • C#读取文本文件

    根据文件名到对应文件夹中读取对应文本文件 txt 并返回数据集合 使用流读取类StreamReader 一行一行读取 ReadLine 文本格式 public static List
  • 使用connect by进行级联查询

    connect by可以用于级联查询 常用于对具有树状结构的记录查询某一节点的所有子孙节点或所有祖辈节点 来看一个示例 现假设我们拥有一个菜单表t menu 其中只有三个字段 id name和parent id 它们是具有父子关系的 最顶级
  • Ubuntu22安装Redis

    Redis 是一个开源的在内存存储键值对数据的存储程序 它可以被用作数据库 缓存 信息暂存 并且支持各种数据结构 例如 字符串 哈希值 列表 集合等等 Redis 通过 Redis Sentinel 和 Redis 集群中多个 Redis
  • 解决谷歌人机验证(Captcha)显示问题

    文章目录 前言 一 Header Editor 下载 安装与配置 1 插件下载 2 插件安装 3 插件配置 前言 由于谷歌服务在国内不可用 所以正常访问时某些网址时 经常会出现需要人机验证的问题 影响正常使用 在不使用科学上网的情况下 我们
  • Web启动项目走Https协议(Webpack版,Umi版和Host代理版)

    需求 Web项目的启动 一般是默认的http协议 在某些业务需求时 需要走https来调试 Webpack版本 只需在webpack的devServer中配置就可以了 devServer host 0 0 0 0 port 8080 htt
  • html代码制作的个人简历源代码

  • python requests.get post

    get 方式 首先导入requests库 import requests 定义url url https baidu com 定义请求头 注意的是headers在真实环境中是有很多数据的 我们通过python传输这个数据就要以字典的方式来定
  • windows10系统提示不允许使用你正在尝试的登录方式,请联系网络管理员了解详细信息

    故障截图如图所示 排查方法 1 检查AD域用户账号登录到是否受限 2 在运行框中输入gpedit msc查看组策略 计算机配置 windows设置 本地配置 用户权限分配 拒绝本地登录 guest 参考是否与正常登录的用户电脑设置一致 允许
  • 使用ESP8266接入“天猫精灵”控制七彩灯(WS2812)的颜色/亮度-开源

    目录 演示视频 1 准备工作 1 1 原理 1 2 使用的硬件以及硬件连接图 1 3 开发环境准备 Arduino开发环境 安装ESP8266的扩展 安装blinker Arduino库 安装blinker APP 下载ws2812的驱动库
  • 树莓派_超声波传感器_三色LED

    import RPi GPIO as GPIO import time TRIG 26 ECHO 19 GREEN 6 YELLOW 5 RED 13 GPIO setmode GPIO BCM GPIO setwarnings False
  • 仿射系统和非仿射系统的数学定义

    一 非仿射系统 非仿射系统是指系统的输入是以非线性的形式出现的 例如 u 2 sin u 等 12 非仿射系统可以用下面的一般形式表示 x t f x t g x t h u t 其中 x t 是状态变量 u t 是控制输入 f x 和 g
  • Codeforces-1260-E. Tournament贪心

    题目描述 You are organizing a boxing tournament where n boxers will participate n is a power of 2 and your friend is one of