NC24838. [USACO 2009 Mar S]Sand Castle
描述
输入描述
* 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.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)