CodeCraft-19 and Codeforces Round #537 (Div. 2) 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.
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 one integer — the minimum power needed to destroy the avengers base.
2 2 1 2
1 3
6
3 2 1 2
1 7
8
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.
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<time.h>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize(2)
using namespace std;
#define maxn 1000005
#define inf 0x7fffffff
//#define INF 1e18
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
#define mclr(x,a) memset((x),a,sizeof(x))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 1000000000
#define sq(x) (x)*(x)
#define eps 1e-5
typedef pair<int, int> pii;
#define pi acos(-1.0)
//const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
typedef pair<int, int> pii; inline int rd() {
int x = 0;
char c = getchar();
bool f = false;
while (!isdigit(c)) {
if (c == '-') f = true;
c = getchar();
}
while (isdigit(c)) {
x = (x << 1) + (x << 3) + (c ^ 48);
c = getchar();
}
return f ? -x : x;
} ll gcd(ll a, ll b) {
return b == 0 ? a : gcd(b, a%b);
}
int sqr(int x) { return x * x; } /*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
if (!b) {
x = 1; y = 0; return a;
}
ans = exgcd(b, a%b, x, y);
ll t = x; x = y; y = t - a / b * y;
return ans;
}
*/
int n, k;
int A, B;
int a[maxn]; ll dfs(ll l, ll r) {
ll L = lower_bound(a + 1, a + 1 + k, l) - a;
ll R = upper_bound(a + 1, a + 1 + k, r) - a; R--;
ll tot = R - L + 1;
ll cost = 0;
if (tot == 0)cost = A;
else cost = B * (r - l + 1)*tot;
if (l == r || tot == 0)return cost;
ll mid = (l + r) >> 1;
return min(cost, dfs(l, mid) + dfs(mid+1, r));
} int main()
{
// ios::sync_with_stdio(0);
rdint(n); rdint(k); rdint(A); rdint(B);
for (int i = 1; i <= k; i++)rdint(a[i]);
sort(a + 1, a + 1 + k);
ll x = (ll)(1 << n);
cout << (ll)dfs(1, x) << endl;
return 0;
}
CodeCraft-19 and Codeforces Round #537 (Div. 2) C. Creative Snap 分治的更多相关文章
- CodeCraft-19 and Codeforces Round #537 Div. 2
D:即有不超过52种物品,求容量为n/2的有序01背包方案数.容易想到设f[i][j]为前i种物品已用容量为j的方案数,有f[i][j]=f[i-1][j-a[i]]*C(n/2-j+a[i],a[i ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) E 虚树 + 树形dp(新坑)
https://codeforces.com/contest/1111/problem/E 题意 一颗有n个点的树,有q个询问,每次从树挑出k个点,问将这k个点分成m组,需要保证在同一组中不存在一个点 ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) D 多重排列 + 反向01背包 + 离线处理
https://codeforces.com/contest/1111/problem/D 多重排列 + 反向01背包 题意: 给你一个字符串(n<=1e5,n为偶数),有q个询问,每次询问两个 ...
- CodeCraft-19 and Codeforces Round #537 (Div. 2) 题解
传送门 D. Destroy the Colony 首先明确题意:除了规定的两种(或一种)字母要在同侧以外,其他字母也必须在同侧. 发现当每种字母在左/右边确定之后,方案数就确定了,就是分组的方案数乘 ...
- 【CodeCraft-19 and Codeforces Round #537 (Div. 2) C】Creative Snap
[链接] 我是链接,点我呀:) [题意] 横坐标1..2^n对应着2^n个复仇者的基地,上面有k个复仇者(位置依次给出). 你是灭霸你要用以下方法消灭这k个复仇者: 一开始你获取整个区间[1..2^n ...
- Codeforces Round #256 (Div. 2/C)/Codeforces448C_Painting Fence(分治)
解题报告 给篱笆上色,要求步骤最少,篱笆怎么上色应该懂吧,.,刷子能够在横着和竖着刷,不能跳着刷,,, 假设是竖着刷,应当是篱笆的条数,横着刷的话.就是刷完最短木板的长度,再接着考虑没有刷的木板,,. ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
- Codeforces Round 542 (Div. 2)
layout: post title: Codeforces Round 542 (Div. 2) author: "luowentaoaa" catalog: true tags ...
- Codeforces Round #456 (Div. 2)
Codeforces Round #456 (Div. 2) A. Tricky Alchemy 题目描述:要制作三种球:黄.绿.蓝,一个黄球需要两个黄色水晶,一个绿球需要一个黄色水晶和一个蓝色水晶, ...
随机推荐
- Spring Cloud Eureka 4 (高可用服务注册中心)
在微服务这样的分布式环境中,我们需要充分考虑发生故障的情况,所以在生产环境中必须考虑对各个组件进行高可用部署,对于服务注册中心也是一样. Eureka Server 的高可用实际上就是讲自己作为服务向 ...
- grep家族
grep家族由命令grep.egrep和fgrep组成. grep:在文件中全局查找指定的正则表达式,并且打印所有包含该表达式的行.egrep和fgrep是grep的变体.egrep:grep的扩展, ...
- 201671010140. 2016-2017-2 《Java程序设计》java学习第六章
java学习第六章 本周对与java中的接口,lambda表达式与内部类进行了学习,以下是我在学习中的一些体会: 1.接口: <1>.接口中的所有常量必须是public sta ...
- 带你剖析WebGis的世界奥秘----瓦片式加载地图(转)
带你剖析WebGis的世界奥秘----瓦片式加载地图 转:https://zxhtom.oschina.io/zxh/20160805.html 编程 java 2016/08/05 0留言, ...
- 场景中,并没有灯源的存在,但是cube却会有灯光照射的反应,这就是Light Probe Group的作用。
http://blog.csdn.net/qq617119142/article/details/41674755
- Ionic01 简单介绍、环境搭建、创建项目、项目结构、创建组件、创建页面、子页面跳转
1 Ionic 基本介绍 Ionic 是一款基于 Angular.Cordova 的强大的 HTML5 移动应用开发框架 , 可以快速创建一个跨平台的移动应用.可以快速开发移动 App.移动端 WEB ...
- 面试题:cook和session
1.首先,Cookie与Session存在的目的是什么? 答:二者都是为了保持客户端访问用户与后台服务器的交互状态,之所以为了保持这种状态,一是为了方便一些业务的实现,另一方面就是为了简化后台服务端的 ...
- SQLAlchemy(ORM框架)
SQLAlchemy SQLAlchemy概述 2 3 4 5 6 7 8 9 10 11 12 13 MySQL-Python mysql+mysqldb://<user>:&l ...
- 初次接触URDF
使用URDF创建机器人3D仿真模型 在真实的机器人上编程可以更好地让我们理解机器人的控制方式,因为真实的机器人会有反馈.如果没有真实的机器人,那么ROS仿真是一个很好的选择. ROS通过URDF(Un ...
- Part5核心初始化_lesson4---关闭mmu
1.ARM存储体系 2.cache 3.虚拟地址 那么谁来完成把虚拟地址转换成物理地址呢? 4.这个工作就由MMU来转换!! 5.关闭MMU和cache 他们都是通过cp15协处理器来控制的!应该在A ...