http://codeforces.com/contest/1111/problem/C
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 22 . 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:
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≤301≤n≤30 , 1≤k≤1051≤k≤105 , 1≤A,B≤1041≤A,B≤104 ), where 2n2n is the length of the base, kk is the number of avengers and AA and BB are the constants explained in the question.
The second line contains kk integers a1,a2,a3,…,aka1,a2,a3,…,ak (1≤ai≤2n1≤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−41−4 with power 2⋅2⋅4=162⋅2⋅4=16 .
Otherwise he can divide the base into two parts 1−21−2 and 3−43−4 .
For base 1−21−2 , he can either burn it with power 2⋅1⋅2=42⋅1⋅2=4 or divide it into 22 parts 1−11−1 and 2−22−2 .
For base 1−11−1 , he can burn it with power 2⋅1⋅1=22⋅1⋅1=2 . For 2−22−2 , he can destroy it with power 11 , as there are no avengers. So, the total power for destroying 1−21−2 is 2+1=32+1=3 , which is less than 44 .
Similarly, he needs 33 power to destroy 3−43−4 . The total minimum power needed is 66 .
设计递归函数dfs(l,r):区间[l,r]消灭掉的最小值
要快速找出[l,r]内英雄的个数,但是数字很大,没办法开桶+前缀和O(1)。暴力会超时。
那么用log的方法。首先将英雄位置排序,然后针对位置数组upper_bound(r)-lower_bound(l)就是[l,r]内英雄的个数。
注意不一定有连续的一段无人看守位置就一定一次使用A灭掉,如果A非常大,那么就会在更大的区间一次全部处理而不涉及A。
#include
using namespace std;
#define ll long long
ll n,k,A,B;
int a[100000+1000];
ll dfs(int l,int r)
{
int m=l+(r-l+1)/2;
ll ans;
int num=upper_bound(a+1,a+1+k,r)-lower_bound(a+1,a+1+k,l);
if(num==0)return ans=A;
else ans=B*num*(r-l+1);
if(r-l>=1)ans=min(ans,dfs(l,m-1)+dfs(m,r));
return ans;
}
int main()
{
// freopen("input.in","r",stdin);
cin>>n>>k>>A>>B;
for(int i=1;i<=k;i++)cin>>a[i];
sort(a+1,a+1+k);
cout<