1. 程式人生 > >洛谷 P2945 [USACO09MAR]沙堡Sand Castle

洛谷 P2945 [USACO09MAR]沙堡Sand Castle

contains %d john class -m ons this fin lac

P2945 [USACO09MAR]沙堡Sand Castle

題目描述

Farmer John has built a sand castle! Like all good castles, the walls have crennelations, that nifty pattern of embrasures (gaps) and merlons (filled spaces); see the diagram below. The N (1 <= N <= 25,000) merlons of his castle wall are conveniently numbered 1..N; merlon i has height M_i (1 <= M_i <= 100,000); his merlons often have varying heights, unlike so many.

He wishes to modify the castle design in the following fashion: he has a list of numbers B_1 through B_N (1 <= B_i <= 100,000), and wants to change the merlon heights to those heights B_1, ..., B_N in some order (not necessarily the order given or any other order derived from the data).

To do this, he has hired some bovine craftsmen to raise and lower the merlons‘ heights. Craftsmen, of course, cost a lot of money. In particular, they charge FJ a total X (1 <= X <= 100) money per unit height added and Y (1 <= Y <= 100) money per unit height

reduced.

FJ would like to know the cheapest possible cost of modifying his sand castle if he picks the best permutation of heights. The answer is guaranteed to fit within a 32-bit signed integer.

Note: about 40% of the test data will have N <= 9, and about 60% will have N <= 18.

約翰用沙子建了一座城堡.正如所有城堡的城墻,這城墻也有許多槍眼,兩個相鄰槍眼中間那部分叫作“城齒”. 城墻上一共有N(1≤N≤25000)個城齒,每一個都有一個高度Mi.(1≤Mi≤100000).現在約翰想把城齒的高度調成某種順序下的Bi,B2,…,BN(1≤Bi≤100000). -個城齒每提高一個單位的高度,約翰需要X(1≤X≤100)元;每降低一個單位的高度,約翰需要Y(1≤y≤100)元. 問約翰最少可用多少錢達到目的.數據保證答案不超過2^31-1.

輸入輸出格式

輸入格式:

  • Line 1: Three space-separated integers: N, X, and Y

  • Lines 2..N+1: Line i+1 contains the two space-separated integers: M_i and B_i

輸出格式:

  • Line 1: A single integer, the minimum cost needed to rebuild the castle

輸入輸出樣例

輸入樣例#1: 復制
3 6 5 
3 1 
1 2 
1 2 
輸出樣例#1: 復制
11 

說明

FJ‘s castle starts with heights of 3, 1, and 1. He would like to change them so that their heights are 1, 2, and 2, in some order. It costs 6 to add a unit of height and 5 to remove a unit of height.

FJ reduces the first merlon‘s height by 1, for a cost of 5 (yielding merlons of heights 2, 1, and 1). He then adds one unit of height to the second merlon for a cost of 6 (yielding merlons of heights 2, 2, and 1).

思路:貪心

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define MAXN 25010
using namespace std;
int n,x,y,ans;
int m[MAXN],b[MAXN];
int main(){
    scanf("%d%d%d",&n,&x,&y);
    for(int i=1;i<=n;i++)
        scanf("%d%d",&m[i],&b[i]);
    sort(m+1,m+1+n);
    sort(b+1,b+1+n);
    for(int i=1;i<=n;i++){
        if(m[i]>b[i])    ans+=(m[i]-b[i])*y;
        else if(m[i]<b[i])    ans+=(b[i]-m[i])*x;
    }
    cout<<ans;
}

洛谷 P2945 [USACO09MAR]沙堡Sand Castle