[BZOJ3399] [Usaco2009 Mar]Sand Castle城堡(排序)
3399: [Usaco2009 Mar]Sand Castle城堡
Time Limit: 3 Sec Memory Limit: 128 MBSubmit: 79 Solved: 66
[Submit][Status][Discuss]
Description
约翰用沙子建了一座城堡.正如所有城堡的城墙,这城墙也有许多枪眼,两个相邻枪眼中间那部分叫作“城齿”. 城墙上一共有N(1≤N≤25000)个城齿,每一个都有一个高度Mi.(1≤尬≤100000).现在约翰想把城齿的高度调成某种顺序下的Bi,B2,…,BN(I≤Bi≤100000). -个城齿每提高一个单位的高度,约翰需要X(I≤X≤100)元;每降低一个单位的高度,约翰需要Y(1≤y≤100)元. 问约翰最少可用多少钱达到目的.数据保证答案不超过2^32.
Input
第1行输入3个整数N,X,Y.
第2到N+1行每行输入两个整数Mi和Bi.
Output
最少花费.
Sample Input
3 6 5
3 1
1 2
1 2
3 1
1 2
1 2
Sample Output
11
HINT
第1个城齿降低1,第2个城齿提高1
Source
有一个非常直观的结论,越小的肯定对应越小的,越大的肯定对应越大的。然后排序乱搞。
/**************************************************************
Problem: 3399
User: ecnu161616
Language: C++
Result: Accepted
Time:24 ms
Memory:2856 kb
****************************************************************/
#include <bits/stdc++.h>
using namespace std;
int n, x, y;
int a[200000], b[200000];
long long ans;
int main()
{
#ifdef ULTMASTER
freopen("a.in","r",stdin);
#endif
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);
for (int i = 0; i < n; ++i) {
if (a[i] < b[i])
ans += 1LL * (b[i] - a[i]) * x;
else ans += 1LL * (a[i] - b[i]) * y;
}
printf("%lld\n", ans);
return 0;
}

浙公网安备 33010602011771号