Creative Snap(分治)

                                                                    C. Creative Snap

Thanos wants to destroy the avengers base, but he needs to destroy the avengers along with their base.

Let we represent their base with an array, where each position can be occupied by many avengers, but one avenger can occupy only one position. Length of their base is a perfect power of 2 . Thanos wants to destroy the base using minimum power. He starts with the whole base and in one step he can do either of following:

  • if the current length is at least 2 , divide the base into 2 equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takes AA amount of power, otherwise it takes his B⋅na⋅lB⋅na⋅l amount of power, where nana is the number of avengers and ll is the length of the current base.

Output the minimum power needed by Thanos to destroy the avengers' base.

Input

The first line contains four integers nn , kk , AA and BB (1≤n≤30 , 1≤k≤105 , 1≤A,B≤104 ), where 2^n is the length of the base, kk is the number of avengers and A and B are the constants explained in the question.

The second line contains kk integers a1,a2,a3,…,ak (1≤ai≤2n ), where aiai represents the position of avenger in the base.

Output

Output one integer — the minimum power needed to destroy the avengers base.

Examples

扫描二维码关注公众号,回复: 5620483 查看本文章

Input

Copy

2 2 1 2
1 3

Output

Copy

6

Input

Copy

3 2 1 2
1 7

Output

Copy

8

Note

Consider the first example.

One option for Thanos is to burn the whole base 1−4 with power 2⋅2⋅4=16.

Otherwise he can divide the base into two parts 1−2 and 3−4 .

For base 1−2, he can either burn it with power 2⋅1⋅2=4 or divide it into 2 parts 1−1 and 2−2 .

For base 1−1, he can burn it with power 2⋅1⋅1=2 . For 2−2 , he can destroy it with power 1 , as there are no avengers. So, the total power for destroying 1−2is 2+1=3, which is less than 4 .

Similarly, he needs 3 power to destroy 3−4 . The total minimum power needed is 6 .

题意 :   

    要摧毁复仇者的基地 ,每个基地可能有一个或多个复仇者看守,也可能没有人 ,给出复仇者的位置, 基地的总长度为2^n , 有两种摧毁策略 , 第一种是 如果当前长度至少为2,则将基础划分为2个相等的一半并单独销毁它们;若其中没有复仇者,则花费A的力量就能将其全部毁灭;若有复仇者,则需要花费B*复仇者的数目*基地的长度 的力量。求出灭霸毁灭所有基地需要的最小力量

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
using namespace std ;
typedef long long LL;
const int MAX = 1e5+10 ;
const int inf = 0x3f3f3f3f ;
LL a[MAX];
int n , k , A , B ;
LL solve(LL l ,LL r){
	LL mid = (l+r)/ 2  ;
	LL ans ;
	int num = upper_bound(a+1,a+1+k,r)-lower_bound(a+1,a+1+k,l) ;
	if(num == 0 ){
		ans = A ;
	}
	else{
		ans = B*num*(r-l+1) ;
		if(r-l>=1){
			ans = min(ans,solve(l,mid)+solve(mid+1,r)) ;
		}
	}
	return ans ;
}
int main(){
  
    cin >> n >> k >> A >>B;
    for(int i = 1 ; i<=k ; i++ ){
    	scanf("%I64d",&a[i]);
	}
	sort(a+1,a+1+k) ;
	
	cout<<solve(1,1LL<<n)<<endl ;
	return 0 ;
}

猜你喜欢

转载自blog.csdn.net/qq_41661809/article/details/88319512