首页 > 其他分享 >Creative Snap(分治)

Creative Snap(分治)

时间:2023-02-17 12:56:23浏览次数:46  
标签:power LL 分治 Creative base Snap destroy avengers he


                                                                    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

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 ;
}

 

标签:power,LL,分治,Creative,base,Snap,destroy,avengers,he
From: https://blog.51cto.com/u_15970235/6063948

相关文章

  • CDQ 分治学习笔记
    CDQ分治学习笔记简介CDQ分治一般用来解决偏序问题。我们先来回忆一下:一维偏序:排序即可解决问题。二维偏序:对于\((a,b)\),我们使用排序解决\(a\),然后\(b\)使用树......
  • 基于Minimum Snap的轨迹生成
    基于MinimumSnap/Jerk的轨迹生成/优化轨迹生成/优化寻找一条路径,可以不考虑动力学约束,也可以考虑动力学约束,然后将路径所在的低维空间转到机器人运动的状态空间,称为轨迹......
  • CentOS 安装 SNAP
    一、CentOS71.安装(1)安装epel库sudoyuminstallepel-release-y(2)安装yum插件coprsudoyuminstallyum-plugin-copr-y(3)添加仓库sudoyumcopren......
  • 顺序表应用7:最大子段和之分治递归法(SDUT 3664)
    #include<bits/stdc++.h>usingnamespacestd;constintmaxn=50005;intnum=0;structnode{int*elem;intlen;};voidCreatlist(structnode&list,intn){......
  • The POM for xxxxxx:jar:1.0-SNAPSHOT is missing, no dependency information availa
    解决方案:该错误表示的是pom文件中相应依赖找不到进入pom文件中删除"相应错误"的依赖。   衍生问题:既然idea显示找不到相应的依赖,那为什么在maven刷......
  • 分治NTT
    首先是NTT的板子。intcnt[N];voidNTT(inta[],intlim,booltype){ for(inti=0;i<(1<<lim);i++)cnt[i]=(cnt[i>>1]>>1)|((i&1)<<(lim-1)); for(inti=0;i<(1<<lim);......
  • QOJ5013 Astral Birth(凸包,分治,堆,贪心)
    QOJ5013AstralBirth\(m=1\)直接求LIS。否则对于\(m\ge2\),就是求把序列分成\(m+1\)段,每段翻转或者不翻转,最终最多\(1\)的个数。很明显相邻两段翻不翻转的......
  • 【YBT2023寒假Day9 B】买棉花糖(DP)(分治)
    买棉花糖题目链接:YBT2023寒假Day9B题目大意有n个商店,每个商店有ci个物品,原价是ai,你在一个商店买的物品越多,下一个买的就越少,每次减少di块钱。然后有q次询问,......
  • 【YBT2023寒假Day7 B】打怪兽(cdq分治)(斜率优化)
    打怪兽题目链接:YBT2023寒假Day7B题目大意有n个怪,每个怪有攻击力和血量。你每次可以选一个怪打b的伤害,如果一个怪的血量小于等于0就死了。然后每次你打完之后所......
  • 快速傅里叶变换(FFT)的分治实现
    本文作者为JustinRochester。目录地址上一篇下一篇......