首页 > 其他分享 >Codeforces1260 E Tournament(贪心)

Codeforces1260 E Tournament(贪心)

时间:2023-02-03 12:35:08浏览次数:53  
标签:贪心 int scanf Tournament boxers Codeforces1260 include ll define


Description:

Codeforces1260 E Tournament(贪心)_ciYou are organizing a boxing tournament, where n boxers will participate (Codeforces1260 E Tournament(贪心)_#define_02 is Codeforces1260 E Tournament(贪心)_#include_03 power of Codeforces1260 E Tournament(贪心)_ci_04), 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 Codeforces1260 E Tournament(贪心)_#include_05 if and only if Codeforces1260 E Tournament(贪心)_#include_06 is stronger than Codeforces1260 E Tournament(贪心)_#include_05.

Codeforces1260 E Tournament(贪心)_ciThe tournament will be organized as follows: Codeforces1260 E Tournament(贪心)_#define_02 boxers will be divided into pairs; the loser in each pair leaves the tournament, and Codeforces1260 E Tournament(贪心)_#include_10

Codeforces1260 E Tournament(贪心)_ciYour 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 Codeforces1260 E Tournament(贪心)_#include_03

Codeforces1260 E Tournament(贪心)_ciFurthermore, during each stage you distribute the boxers into pairs as you wish.

Codeforces1260 E Tournament(贪心)_ciThe 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

Codeforces1260 E Tournament(贪心)_ciThe first line contains one integer Codeforces1260 E Tournament(贪心)_#define_16 — the number of boxers. Codeforces1260 E Tournament(贪心)_#define_02 is a power of Codeforces1260 E Tournament(贪心)_ci_04.

Codeforces1260 E Tournament(贪心)_ciThe second line contains n integers Codeforces1260 E Tournament(贪心)_#include_20 where Codeforces1260 E Tournament(贪心)_#include_21 is the number of dollars you have to pay if you want to bribe the boxer with strength Codeforces1260 E Tournament(贪心)_#include_06. Exactly one of ai is equal to −1 — it means that the boxer with strength Codeforces1260 E Tournament(贪心)_#include_06 is your friend. All other values are in the range Codeforces1260 E Tournament(贪心)_ci_24.

Output

Codeforces1260 E Tournament(贪心)_ciPrint 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

Codeforces1260 E Tournament(贪心)_ciIn the first test case no matter how you will distribute boxers into pairs, your friend is the strongest boxer and anyway wins the tournament.

Codeforces1260 E Tournament(贪心)_ciIn 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);
    Codeforces1260 E Tournament(贪心)_#define_28题意就是n个拳手下标就是他们的力量值,每场都是二对二的比赛,稳想要使得你的朋友获胜你最少花多少钱贿赂别的拳手。
    Codeforces1260 E Tournament(贪心)_#define_28这里先了解一个知识 Codeforces1260 E Tournament(贪心)_#define_30&Codeforces1260 E Tournament(贪心)_#include_31 的作用是将i的二进制表示中的最低位为 Codeforces1260 E Tournament(贪心)_ci_32的改为 Codeforces1260 E Tournament(贪心)_ci_33。而且如果Codeforces1260 E Tournament(贪心)_#define_30是二的次幂的话返回 Codeforces1260 E Tournament(贪心)_ci_33
    Codeforces1260 E Tournament(贪心)_#define_28比朋友力量值小的我们不需要考虑,只要考虑比他力量值大的就行。如果朋友的力量值不是最大的那么最大的那个人肯定要被贿赂一次。每次以二的次幂为一个段,只要贿赂大于朋友拳手当前段的最小的就行了。我们可以让这个力量值大的在前面的比赛中肯定不需要花费钱来贿赂。只要最后和朋友拳手比赛的时候贿赂就行了。

AC代码:

#include <cstdio>
#include <vector>
#include <queue>
#include <cstring>
#include <cmath>
#include <map>
#include <set>
#include <string>
#include <iostream>
#include <algorithm>
#include <iomanip>
using namespace std;
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define pd(n) printf("%d\n", n)
#define pc(n) printf("%c", n)
#define pdd(n,m) printf("%d %d", n, m)
#define pld(n) printf("%lld\n", n)
#define pldd(n,m) printf("%lld %lld\n", n, m)
#define sld(n) scanf("%lld",&n)
#define sldd(n,m) scanf("%lld%lld",&n,&m)
#define slddd(n,m,k) scanf("%lld%lld%lld",&n,&m,&k)
#define sf(n) scanf("%lf",&n)
#define sc(n) scanf("%c",&n)
#define sff(n,m) scanf("%lf%lf",&n,&m)
#define sfff(n,m,k) scanf("%lf%lf%lf",&n,&m,&k)
#define ss(str) scanf("%s",str)
#define rep(i,a,n) for(int i=a;i<=n;i++)
#define per(i,a,n) for(int i=n;i>=a;i--)
#define mem(a,n) memset(a, n, sizeof(a))
#define debug(x) cout << #x << ": " << x << endl
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define mod(x) ((x)%MOD)
#define gcd(a,b) __gcd(a,b)
#define lowbit(x) (x&-x)
typedef pair<int,int> PII;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
const int MOD = 1e9 + 7;
const double eps = 1e-9;
const int maxn = 3e5 + 5;
const ll INF = 0x3f3f3f3f3f3f3f3fll;
const int inf = 0x3f3f3f3f;
inline int read()
{
int ret = 0, sgn = 1;
char ch = getchar();
while(ch < '0' || ch > '9')
{
if(ch == '-')
sgn = -1;
ch = getchar();
}
while (ch >= '0' && ch <= '9')
{
ret = ret*10 + ch - '0';
ch = getchar();
}
return ret*sgn;
}
inline void Out(int a) //Êä³öÍâ¹Ò
{
if(a>9)
Out(a/10);
putchar(a%10+'0');
}

int qpow(int m, int k, int mod)
{
int res = 1, t = m;
while (k)
{
if (k&1)
res = res * t % mod;
t = t * t % mod;
k >>= 1;
}
return res;
}

ll gcd(ll a,ll b)
{
return b==0?a : gcd(b,a%b);
}

ll lcm(ll a,ll b)
{
return a*b/gcd(a,b);
}

ll v[290010],n,ans,i;
multiset<int> r;
int main()
{
cin>>n;
for(i=1; i<=n; i++)
cin>>v[i];
for(i=n; i>0&&v[i]!=-1; i--)
{
r.insert(v[i]);
if(!(i&(i-1)))
{
ans+=*r.begin();
r.erase(r.begin());
}
}
cout<<ans<<endl;
return 0;
}


标签:贪心,int,scanf,Tournament,boxers,Codeforces1260,include,ll,define
From: https://blog.51cto.com/u_15952369/6035768

相关文章

  • POJ 1456 Supermarket (贪心+并查集优化)
    DescriptionAsupermarkethasasetProdofproductsonsale.Itearnsaprofitpxforeachproductx∈Prodsoldbyadeadlinedxthatismeasuredasanintegral......
  • CF1778F Maximizing Root 【贪心】【数学】
    容易想到,选的点应该是和\(1\)连通的连通块。最后的答案是\(a[1]\)乘以\(a[1]\)的因子。所以我们枚举\(a[1]\)的因子,然后判断是否能让所有点都变成\(a[1]\)的因子的倍数。......
  • Educational Codeforces Round 126 (Rated for Div. 2) D. Progressions Covering(贪心
    题目https://codeforces.com/problemset/problem/1661/D题意给一个长度为n的数组a,和一个正数k,每次在数组a中选取连续的k个元素每个元素减去1,2,3……k问至少要......
  • CF1400E Clear the Multiset 题解 贪心+分治
    题目链接:http://codeforces.com/problemset/problem/1400/E题目大意:给定一个长度为\(n\)数列\(\{a_n\}\),你可以进行如下操作:操作1:任意选择一个区间\([l,r]\),使区间内......
  • POJ--3253 Fence Repair(贪心/优先队列)
    记录23:572023-1-25http://poj.org/problem?id=3253reference:《挑战程序设计竞赛(第2版)》2.2.4p47DescriptionFarmerJohnwantstorepairasmalllengthofth......
  • Codeforces Round #661 (Div. 3) D. Binary String To Subsequences(贪心/思维)
    https://codeforces.com/contest/1399/problem/D题目大意:长度为n的字符串s只由0和1组成。我们要把s分成最小数量的子序列,使得每一个子序列看起来像“010101...”或......
  • POJ--3069 Saruman's Army(贪心)
    记录0:332023-1-24http://poj.org/problem?id=3069reference:《挑战程序设计竞赛(第2版)》2.2.4p45DescriptionSarumantheWhitemustleadhisarmyalongastra......
  • 贪心
    后悔操作的实现Problem-C1-CodeforcesProblem-C2-Codeforcesin64-41-31-3out5给出一个数组题目让你求出在所加的值不小于0的情况下求出最......
  • Codeforces Round #834 (Div. 3) D. Make It Round(贪心/数论)
    https://codeforces.com/contest/1759/problem/D题目大意:给定一个数字n,要求扩大至多m倍,求最大的并且最多0的数字。input106115431354161005012345264......
  • 贪心算法
    1.什么是贪心算法贪心算法,又称贪婪算法,是一种在解决问题的过程中追求局部最优的算法,对于一个有多种属性的事物来说,贪心算法会优先满足某种条件,追求局部最优的同时希......