C&C++   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Codeforces 1111C Creative Snap分治+贪心大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

Creative Snap

C. Creative Snap
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

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:

  • if the current length is at leas22,divide the basE int22 equal halves and destroy them separately,or
  • burn the current base. If it contains no avenger in it,it takes Aamount of power,otherwise it takes his B⋅@H_616_86@nalB⋅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 (1n301≤n≤30, @H_955_197@1k1051≤k≤105, 1A,B1041≤A,B≤104),where 2n2n is the length of the base, kkis the number of avengers and AA and BB are the constants explained in the question.

The second line contains kIntegers a1,a2,a3,,aka1,a2,a3,…,ak (1ai2n1≤ai≤2n),where a@H_770_419@iai represents the position of avenger in the base.

Output

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

这题刚一看觉得很

,仔细一看数据范围……

其实也不难,主要是数据范围过大。要用一种类似离散化的做法。

对数组a排序。对于一段区间[l,r]的英雄数量等于a中第@L_673_3@比r大的数的下标减1减a中第@L_673_3@大于等于l的数的下标加1,这样就可以做了(k很小)。

不过要注意剪枝:若区间[l,r]的英雄数量等于0,就直接返回A。

代码

#include <bits/stdc++.h>
using namespace std;
long long n,k,a,b;
long long hero[1000001];
long long solve(long long l,long long r) {
    long long num = upper_bound(hero + 1,hero + 1 + k,r) - lower_bound(hero + 1,hero + 1 + k,l);
    if (num == 0) return a;
    long long ans = (r - l + 1) * b * num;
    if (l >= r) return ans;
    ans = min(ans,solve(l,(l + r) / 2) + solve((l + r) / 2 + 1,r));
    return ans;
}
int main() {
    cin >> n >> k >> a >> b;
    for (long long i = 1; i <= k; i++) cin >> hero[i];
    sort(hero + 1,hero + 1 + k);
    cout << solve(1,(1 << n));
}

大佬总结

以上是大佬教程为你收集整理的Codeforces 1111C Creative Snap分治+贪心全部内容,希望文章能够帮你解决Codeforces 1111C Creative Snap分治+贪心所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。