CodeForces 1260E Tournament(贪心+优先队列)

Tournament

time limit per test:2 seconds
memory limit per test:256 megabytes

Problem Description

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 n 2 \frac{n}{2} 2n 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 (2n218) — 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.

Sample Input

4
3 9 1 -1

Sample Output

0

题意

有n个人( n = 2 k n = 2^k n=2k)进行比赛,每次比赛两个人中胜者晋级,继续进行下一轮比赛,直到最后决定冠军。n个人每个人有排名,比赛排名高的一定获胜,但可以收买排名高的人,让其输掉比赛,你想让其中一人获胜你可以安排比赛,求最少的花费使你需要的那个人成为冠军。

题解:

设让s号成为冠军,则需要考虑收买部分比 s 强的人。所有人中最强的人一定需要被收买,因为他与其他任何人比都会晋级。所以不如把他安排在决赛,让他打倒更多比s强的人(那样s就可以安排更弱的对手了),总共能打倒 2 k − 1 − 1 2^{k-1}-1 2k11个人,暂且认为他淘汰了最大的 2 k − 1 − 1 2^{k-1}-1 2k11个人。半决赛,与s对战的人,则一定是前 2 k − 1 2^{k-1} 2k1中最大的人(暂且不考虑收买的问题),因为其他人是无法晋级到半决赛的,理由同决赛,其实能做到还有被最强的打倒的那 2 k − 1 − 1 2^{k-1}-1 2k11个人也是可以做到的。所以综合考虑这些人中,收买费用最小的人,安排他作为半决赛对手即可。同理可以求出其他轮次需要收买的人。

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define dbg(x) cout<<#x<<" = "<
#define INF 0x3f3f3f3f
#define eps 1e-8
 
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int maxn = 1000100;
const int mod = 1e9+7;
int a[maxn], b[maxn];

int main()
{
	priority_queue<int, vector<int>, greater<int> > que;
	int n, i, j, m=0;
	LL ans = 0;
	scanf("%d", &n);
	for(i=0;i<n;i++){
		scanf("%d", &a[i]);
	}
	//依次考虑决赛、半决赛.....
	while(n > 1)
	{
		//当取到-1时不用再考虑花费了,因为剩下的人都比他如弱
		if(a[n-1] == -1)break;
		que.push(a[n-1]);
		ans += que.top();que.pop();
		for(i=n-2;i>=n/2;i--)
		{
			if(a[i] == -1)break;
			que.push(a[i]);
		}
		if(i >= n/2)break;
		n /= 2;
	}
	printf("%I64d\n", ans);
	return 0;
}

你可能感兴趣的:(CodeForces 1260E Tournament(贪心+优先队列))