Codeforces Contest 1111 problem 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 A amount of power, otherwise it takes his B⋅na⋅l amount of power, where na is the number of avengers and l 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 n, k, A and B (1≤n≤30, 1≤k≤105, 1≤A,B≤104), where 2n is the length of the base, k is the number of avengers and A and B are the constants explained in the question.

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

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

Examples
inputCopy
2 2 1 2
1 3
outputCopy
6
inputCopy
3 2 1 2
1 7
outputCopy
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−2 is 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长的木板,上面有k个点,你有两种操作:
1.将一块木板分成两个大小相同的木板
2.你可以烧掉一块木板,如果这个木板上没有人,烧掉它的代价是a,否则就是b人数木板的长度
问你烧掉所有木板的最小代价是多少

题解:

由于它的木板是分成一样大小的,所以其实最终每块木板的大小是确定的,那么我们只要往下二分,查看当前二分到的木板要么是烧掉,要么是继续二分,直到这块木板上没有人为止,时间复杂度为nlogn

#include<bits/stdc++.h>
using namespace std;
#define ll long long
vector<int>vec;
ll n,m,a,b;
ll dfs(int l,int r)
{
    int sta=lower_bound(vec.begin(),vec.end(),l)-vec.begin();
    int fin=upper_bound(vec.begin(),vec.end(),r)-vec.begin()-1;
    ll num=fin-sta+1;
    ll ans=num==0?a:b*num*(ll)(r-l+1);
    int mid=l+r>>1;
    if(l==r||num==0)
        return ans;
    return min(ans,dfs(l,mid)+dfs(mid+1,r));
}
int main()
{
    scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
    int x;
    for(int i=1;i<=m;i++)
        scanf("%d",&x),vec.push_back(x);
    sort(vec.begin(),vec.end());
    printf("%lld\n",dfs(1,pow(2,n)));
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值