【CF1111C】 Creative Snap

题目

题意翻译
题目描述
灭霸要摧毁复仇者们的基地!

我们可以将复仇者的基地看成一个序列,每个位置都有可能有多个复仇者;但是每个复仇者只能占据一个位置。
他们基地的长度刚好是22的整数幂,灭霸想要用最少的能量摧毁它们。他在摧毁过程中,可以选择:

如果这段基地长度\ge 2≥2,他可以将其分为相等长度的两半。
烧掉这段基地。如果这段基地中没有复仇者,他需要消耗AA的能量;如果有,则需要消耗BxlB∗x∗l的能量。其中ll是这段基地长度,xx是这段中的复仇者数量。
输出一个整数,表示他摧毁全部基地需要的最少能量。

输入输出格式
输入格式
第一行四个整数n,k,A,Bn,k,A,B;2^n2
n
为基地长度,kk是总共的复仇者数量,A,BA,B的意义如题目描述。
接下来一行kk个整数,a_ia
i
​ 表示第ii个复仇者所在的位置

输出格式
一个整数,表示摧毁基地所需要的最少能量。

说明
样例解释
对于样例1,直接烧区间[1,4][1,4]需要能量为422=164∗2∗2=16。
但是,如果将其分为44段,分别烧掉,所需能量只有2+1+2+1=62+1+2+1=6。
可以证明没有更优的方案,所以输出6。

数据范围
对于全部数据:
1\le n \le 301≤n≤30
1\le k \le 10^51≤k≤10
5

1\le A,B \le 10^41≤A,B≤10
4

1\le a_i \le 2^n1≤a
i
​ ≤2
n

题目描述
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 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 2 , divide the base into 2 2 equal halves and destroy them separately, or
burn the current base. If it contains no avenger in it, it takes A A amount of power, otherwise it takes his B \cdot n_a \cdot l B⋅n
a
​ ⋅l amount of power, where n_a n
a
​ is the number of avengers and l l is the length of the current base.

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

输入输出格式
输入格式:
The first line contains four integers n n , k k , A A and B B ( 1 \leq n \leq 30 1≤n≤30 , 1 \leq k \leq 10^5 1≤k≤10
5
, 1 \leq A,B \leq 10^4 1≤A,B≤10
4
), where 2^n 2
n
is the length of the base, k k is the number of avengers and A A and B B are the constants explained in the question.

The second line contains k k integers a_{1}, a_{2}, a_{3}, \ldots, a_{k} a
1
​ ,a
2
​ ,a
3
​ ,…,a
k
​ ( 1 \leq a_{i} \leq 2^n 1≤a
i
​ ≤2
n
), where a_{i} a
i
​ represents the position of avenger in the base.

输出格式:
Output one integer — the minimum power needed to destroy the avengers base.

输入输出样例
输入样例#1:
2 2 1 2
1 3
输出样例#1:
6
输入样例#2:
3 2 1 2
1 7
输出样例#2:
8
说明
Consider the first example.

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

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

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

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

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

思路

代码

我们想到用线段树来维护。

线段树的每个节点维护区间内的人数和价值,然后按照题意pushup即可。注意如果一个区间内的人数为0,这个区间的价值为A。

由于2^n
很大,所以我们要动态开节点。建树的时候将所有人依次插入线段树中。最后答案即为根节点的价值。

然后就没有然后了qwq

代码

#include <cctype>
#include <cstdio>
#include <climits>
#include <algorithm>

template <typename T> inline void read(T& x) {
    int f = 0, c = getchar(); x = 0;
    while (!isdigit(c)) f |= c == '-', c = getchar();
    while (isdigit(c)) x = x * 10 + c - 48, c = getchar();
    if (f) x = -x;
}
template <typename T, typename... Args>
inline void read(T& x, Args&... args) {
    read(x); read(args...); 
}
template <typename T> void write(T x) {
    if (x < 0) x = -x, putchar('-');
    if (x > 9) write(x / 10);
    putchar(x % 10 + 48);
}
template <typename T> void writeln(T x) { write(x); puts(""); }
template <typename T> inline bool chkmin(T& x, const T& y) { return y < x ? (x = y, true) : false; }
template <typename T> inline bool chkmax(T& x, const T& y) { return x < y ? (x = y, true) : false; }

typedef long long LL;
const int maxn = 1e5 + 207;
int a[maxn];
int n, K, A, B;

inline LL count(int l, int r) {
    int pr = std::upper_bound(a + 1, a + K + 1, r) - a;
    --pr;
    int pl = std::upper_bound(a + 1, a + K + 1, l - 1) - a;
    --pl;
    return pr - pl;
}

LL solve(int l, int r) {
    int cnt = count(l, r);
    if (!cnt) return A;
    if (l == r) return B * cnt;
    int mid = (l + r) >> 1;
    return std::min(1ll * cnt * B * (r - l + 1), solve(l, mid) + solve(mid + 1, r));
}

int main() {
    read(n, K, A, B);
    for (int i = 1; i <= K; ++i) read(a[i]);
    std::sort(a + 1, a + K + 1);
    writeln(solve(1, 1 << n));
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值