con name 个数 blank 调整 can 贪心 pro
【题目链接】 http://www.lydsy.com/JudgeOnline/problem.php?id=3399
【题目大意】
将一个集合调整成另一个集合中的数,把一个数 + 1 需要消耗 x,-1 需要消耗 y,问最小消耗。
【题解】
显然两个集合排序之后一一对应调整需要消耗的才是最少的,所以排序计算答案即可。
【代码】
- #include < cstdio > #include < algorithm > using namespace std;
- const int N = 25010;
- int a[N],
- b[N],
- n,
- x,
- y;
- int main() {
- while (~scanf("%d%d%d", &n, &x, &y)) {
- for (int i = 0; i < n; i++) scanf("%d%d", &a[i], &b[i]);
- sort(a, a + n);
- sort(b, b + n);
- int ans = 0;
- for (int i = 0; i < n; i++) {
- if (a[i] < b[i]) ans += (b[i] - a[i]) * x;
- else ans += (a[i] - b[i]) * y;
- }
- printf("%d\n", ans);
- }
- return 0;
- }
BZOJ 3399 [Usaco2009 Mar]Sand Castle 城堡(贪心)
来源: http://www.bubuko.com/infodetail-2058253.html