列表

详情


NC24838. [USACO 2009 Mar S]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 Mi (1 <= Mi <= 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 B1 through BN (1 <= Bi <= 100,000), and wants to change the merlon heights to those heights B1, ..., BN 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.

输入描述

* Line 1: Three space-separated integers: N, X, and Y
* Lines 2..N+1: Line i+1 contains the two space-separated integers: Mi and Bi

输出描述

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

示例1

输入:

3 6 5 
3 1 
1 2 
1 2 

输出:

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).

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 20ms, 内存消耗: 504K, 提交时间: 2019-09-07 10:40:44

#include<bits/stdc++.h> 
using namespace std;
int m[30000],b[30000];
int main(){
	int n,x,y;
	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+n+1);
	sort(b+1,b+n+1);
	int sum=0;
	for(int i=1;i<=n;i++){
		if(m[i]>b[i]){
			sum+=(m[i]-b[i])*y;
		}
		else if(m[i]<b[i]){
			sum+=(b[i]-m[i])*x;
		}
	}
	printf("%d\n",sum);
	return 0;
}

C++ 解法, 执行用时: 18ms, 内存消耗: 512K, 提交时间: 2022-03-25 19:08:41

#include<bits/stdc++.h>
using namespace std;
int main()
{
	int n,x,y,a[25001],b[25001],ans=0;cin>>n>>x>>y;
	for(int i=1;i<=n;i++)cin>>a[i]>>b[i];
	sort(a+1,a+n+1);sort(b+1,b+n+1);
	for(int i=1;i<=n;i++)
	{
		if(a[i]<b[i])ans+=(b[i]-a[i])*x;
		else ans+=(a[i]-b[i])*y;
	}
	cout<<ans;
	return 0;
}

pypy3(pypy3.6.1) 解法, 执行用时: 123ms, 内存消耗: 25132K, 提交时间: 2021-05-02 23:43:54

n,x,y=map(int,input().split())
m=[]
b=[]
ans=0
for i in range(n):
    a=input()
    m.append(int(a.split()[0]))
    b.append(int(a.split()[1]))
m.sort()
b.sort()
for i in range(n):
    if(m[i]<b[i]):
        ans+=(b[i]-m[i])*x
    else:
        ans+=(m[i]-b[i])*y
print(ans)

上一题