首页 > 其他 > 详细

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

时间:2019-02-10 15:49:43      阅读:152      评论:0      收藏:0      [点我收藏+]

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 (1n30, 1k105, 1A,B104), 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 (1ai2n), where ai

represents the position of avenger in the base.

Output

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

Examples
Input
Copy
2 2 1 2
1 3
Output
Copy
6
Input
Copy
3 2 1 2
1 7
Output
Copy
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.

 

#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 分治

原文:https://www.cnblogs.com/zxyqzy/p/10359275.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!