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 least 22, divide the base into 22 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≤301≤n≤30, 1≤k≤1051≤k≤105, 1≤A,B≤1041≤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 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.

这题刚一看觉得很水

,仔细一看数据范围……

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

对数组a排序。对于一段区间[l,r]的英雄数量等于a中第一个比r大的数的下标减1减a中第一个大于等于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));
}

原文地址:https://www.cnblogs.com/zcr-blog/p/11427268.html

时间: 2024-08-30 02:29:03

Codeforces 1111C Creative Snap分治+贪心的相关文章

Codeforces 442B Andrey and Problem(贪心)

题目链接:Codeforces 442B Andrey and Problem 题目大意:Andrey有一个问题,想要朋友们为自己出一道题,现在他有n个朋友,每个朋友想出题目的概率为pi,但是他可以同时向多个人寻求帮助,不过他只能要一道题,也就是如果他向两个人寻求帮助,如果两个人都成功出题,也是不可以的. 解题思路:贪心,从概率最大的人开始考虑,如果询问他使得概率变大,则要询问. #include <cstdio> #include <cstring> #include <a

Codeforces Round #300-Tourist&#39;s Notes(贪心)

Tourist's Notes Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Description A tourist hiked along the mountain range. The hike lasted for n days, during each day the tourist noted height above the sea level

Codeforces 432E Square Tiling(构造+贪心)

我们通常这么写 using (SqlDataReader drm = sqlComm.ExecuteReader()) { drm.Read();//以下把数据库中读出的Image流在图片框中显示出来. MemoryStream ms = new MemoryStream((byte[])drm["Logo"]); Image img = Image.FromStream(ms); this.pictureBox1.Image = img; } 我的写数据 private void b

Codeforces Round #256 (Div. 2) C. Painting Fence(分治贪心)

题目链接:http://codeforces.com/problemset/problem/448/C ---------------------------------------------------------------------------------------------------------------------------------------------------------- 欢迎光临天资小屋:http://user.qzone.qq.com/593830943

Codeforces 888G Xor-MST - 分治 - 贪心 - Trie

题目传送门 这是一条通往vjudge的高速公路 这是一条通往Codeforces的高速公路 题目大意 给定一个$n$阶完全图,每个点有一个权值$a_{i}$,边$(i, j)$的权值是$(a_{i}\  xor\  a_{j})$.一个生成树的权值是各边的权值和.问最小生成树的权值. 设值域为$[0, 2^{k})$. 当$k = 1$的时候,显然将点权为0的点之间互相连边,点权为1的点之间互相连边,中间随便连一条. 当$k = x\ (x > 1)$的时候,将这些点按照二进制的第$k$位分成两

C. Creative Snap(分治)

只用考虑对于任意一段,直接炸掉好还是分成两段好,看见0的直接返回A 然后用了在整个区间上的二分搜索,这个一开始想复杂了,还想把a数组分成小区间,但是后来发现没必要 #include<bits/stdc++.h> using namespace std; typedef long long ll; ll a[100010]; ll n,k,A,B; ll cal(ll l,ll r) { //最后一个小于等于r的数字 //第一个大于等于l的数字的前一个数字 return upper_bound(

【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap

[链接] 我是链接,点我呀:) [题意] 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出). 你是灭霸你要用以下方法消灭这k个复仇者: 一开始你获取整个区间[1..2^n] 假设你当前获取的区间为[l,r] mid = (l+r)/2 那么你每次有两种选择 1.将整个区间全都毁掉,如果这个区间里没有复仇者,那么花费为A,否则花费为B复仇者个数区间长度 2.将区间分为[l,mid]和[mid+1,r]分开毁掉(即分别获取[l,mid]和[mid+1,r]这两个区间,然

Codeforces 798D Mike and distribution - 贪心

Mike has always been thinking about the harshness of social inequality. He's so obsessed with it that sometimes it even affects him while solving problems. At the moment, Mike has two sequences of positive integers A = [a1, a2, ..., an] and B = [b1, 

codeforces 349B Color the Fence 贪心,思维

1.codeforces 349B    Color the Fence 2.链接:http://codeforces.com/problemset/problem/349/B 3.总结: 刷栅栏.1-9每个字母分别要ai升油漆,问最多可画多大的数字. 贪心,也有点考思维. #include<bits/stdc++.h> using namespace std; #define LL long long #define INF 0x3f3f3f3f int main() { int v,a[1