cf Educational Codeforces Round 77 E. Tournament

原题:
E. Tournament
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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≤2^18) — 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,10^9].

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个人参加比赛,每个人都给出可以被贿赂的金额,每个人的顺序按照能力值排序。
现在让你安排比赛,即两两配对赛,输的淘汰。最少贿赂多少钱能拿冠军。 -1表示自己

代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
 
typedef pair<int,int> pii;
const int maxn = 1000001;
 
int n;
ll a[maxn];
map<int,bool> match;
int main()
{
    ios::sync_with_stdio(false);
    while(cin>>n)
    {
        match.clear();
        for(int i=1;i<=n;i++)
            cin>>a[i];
        priority_queue<ll,vector<ll>,greater<ll>> Q;
 
        for(int i=1;i<=n;i<<=1)
            match[i]=true;
        ll ans = 0;
        for(int i=n;i>=1&&a[i]>0;i--)
        {
            Q.push(a[i]);
            if(match[i])
            {
                int top = Q.top();
                Q.pop();
                ans+=top;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}

解答:

第一轮,可以挑所有选手作为对手。
第二轮,可以挑第1个到第n作为,对手,因为第一个选手能力最弱,无论和谁比赛都会被淘汰。
第三轮,可以挑[5,n]个,因为前四个选手肯定会被淘汰,以此类推

每次在对应轮数的比赛时挑选剩余的人中被贿赂的钱最少的人比赛即可。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章