如何用分治贪心算法解决Codeforces 1111C创意快照问题?

2026-04-16 20:434阅读0评论SEO资源
  • 内容介绍
  • 文章标签
  • 相关推荐

本文共计837个文字,预计阅读时间需要4分钟。

如何用分治贪心算法解决Codeforces 1111C创意快照问题?

Thanos计划摧毁复仇者基地,但必须连同复仇者一并摧毁。

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 of2">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:

如何用分治贪心算法解决Codeforces 1111C创意快照问题?

  • if the current length is at least2">22, divide the base into2">22equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takesA">AAamount of power, otherwise it takes hisB⋅na⋅l">B⋅na⋅lB⋅na⋅lamount of power, wherena">nanais the number of avengers andl">llis the length of the current base.
Output the minimum power needed by Thanos to destroy the avengers‘ base. Input

The first line contains four integersn">nn,k">kk,A">AAandB">BB(1≤n≤30">1≤n≤301≤n≤30,1≤k≤105">1≤k≤1051≤k≤105,1≤A,B≤104">1≤A,B≤1041≤A,B≤104), where2n">2n2nis the length of the base,k">kkis the number of avengers andA">AAandB">BBare the constants explained in the question.

The second line containsk">kkintegersa1,a2,a3,…,ak">a1,a2,a3,…,aka1,a2,a3,…,ak(1≤ai≤2n">1≤ai≤2n1≤ai≤2n), whereai">aiairepresents 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)); }

本文共计837个文字,预计阅读时间需要4分钟。

如何用分治贪心算法解决Codeforces 1111C创意快照问题?

Thanos计划摧毁复仇者基地,但必须连同复仇者一并摧毁。

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 of2">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:

如何用分治贪心算法解决Codeforces 1111C创意快照问题?

  • if the current length is at least2">22, divide the base into2">22equal halves and destroy them separately, or
  • burn the current base. If it contains no avenger in it, it takesA">AAamount of power, otherwise it takes hisB&#x22C5;na&#x22C5;l">B⋅na⋅lB⋅na⋅lamount of power, wherena">nanais the number of avengers andl">llis the length of the current base.
Output the minimum power needed by Thanos to destroy the avengers‘ base. Input

The first line contains four integersn">nn,k">kk,A">AAandB">BB(1&#x2264;n&#x2264;30">1≤n≤301≤n≤30,1&#x2264;k&#x2264;105">1≤k≤1051≤k≤105,1&#x2264;A,B&#x2264;104">1≤A,B≤1041≤A,B≤104), where2n">2n2nis the length of the base,k">kkis the number of avengers andA">AAandB">BBare the constants explained in the question.

The second line containsk">kkintegersa1,a2,a3,&#x2026;,ak">a1,a2,a3,…,aka1,a2,a3,…,ak(1&#x2264;ai&#x2264;2n">1≤ai≤2n1≤ai≤2n), whereai">aiairepresents 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)); }