BZOJ 3399 [Usaco2009 Mar]Sand Castle城堡(贪心)

 

【题目链接】 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;
}
posted @ 2017-05-07 20:16  forever97  阅读(215)  评论(0编辑  收藏  举报