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]個,因爲前四個選手肯定會被淘汰,以此類推

每次在對應輪數的比賽時挑選剩餘的人中被賄賂的錢最少的人比賽即可。

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