列表

详情


NC24394. [USACO 2013 Mar G]The Cow Run

描述

Farmer John has forgotten to repair a hole in the fence on his farm, and his N cows (1 <= N <= 1,000) have escaped and gone on a rampage! Each minute a cow is outside the fence, she causes one dollar worth of damage. FJ must visit each cow to install a halter that will calm the cow and stop the damage. 
Fortunately, the cows are positioned at distinct locations along a straight line on a road outside the farm. FJ knows the location P_i of each cow i (-500,000 <= P_i <= 500,000, P_i != 0) relative to the gate (position 0) where FJ starts. 
FJ moves at one unit of distance per minute and can install a halter instantly. Please determine the order that FJ should visit the cows so he can minimize the total cost of the damage; you should compute the minimum total damage cost in this case.

输入描述

* Line 1: The number of cows, N.

* Lines 2..N+1: Line i+1 contains the integer P_i.

输出描述

* Line 1: The minimum total cost of the damage.

示例1

输入:

4
-2
-12
3
7

输出:

50

说明:

INPUT DETAILS:
Four cows placed in positions: -2, -12, 3, and 7.

OUTPUT DETAILS:
The optimal visit order is -2, 3, 7, -12. FJ arrives at position -2 in 2
minutes for a total of 2 dollars in damage for that cow.
He then travels to position 3 (distance: 5) where the cumulative damage is
2 + 5 = 7 dollars for that cow.
He spends 4 more minutes to get to 7 at a cost of 7 + 4 = 11 dollars for
that cow.
Finally, he spends 19 minutes to go to -12 with a cost of 11 + 19 = 30 dollars.
The total damage is 2 + 7 + 11 + 30 = 50 dollars.

原站题解

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

C++14(g++5.4) 解法, 执行用时: 27ms, 内存消耗: 16364K, 提交时间: 2020-06-05 12:39:16

#include <bits/stdc++.h>
using namespace std;
const int MX = 1e3+7;
int dis[MX];
long long f[MX][MX][2];
int main()
{
    int n;
    cin>>n;
    for(int i = 1; i <= n; ++i) {
        scanf("%d", &dis[i]);
    }
    n++;
    sort(dis+1, dis+n+1);
    memset(f, 127, sizeof f);
    int x = 0;
    for(int i = 1; i <= n; ++i)
        if(dis[i] == 0) x = i;
    f[x][x][0] = f[x][x][1] = 0;
    for(int len = 2; len <= n; ++len)
    for(int l = 1, r = len; r <= n; ++l, ++r)
    {
        f[l][r][0] = min(f[l+1][r][0]+(dis[l+1]-dis[l])*(n-len+1), f[l+1][r][1]+(dis[r]-dis[l])*(n-len+1));
        f[l][r][1] = min(f[l][r-1][0]+(dis[r]-dis[l])*(n-len+1), f[l][r-1][1]+(dis[r]-dis[r-1])*(n-len+1));
    }
    cout<<min(f[1][n][0],f[1][n][1]);
    return 0;
}

C++11(clang++ 3.9) 解法, 执行用时: 11ms, 内存消耗: 8580K, 提交时间: 2020-05-31 17:10:36

#include<bits/stdc++.h>
using namespace std;
const int N=1001;
int dp[N][N][2],p[N];
int main(){
	memset(dp,0x3f3f,sizeof(dp));
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;++i){
		scanf("%d",&p[i]);
	}
	sort(p+1,p+n+1);
	for(int i=1;i<=n;++i){
		dp[i][i][0]=dp[i][i][1]=n*abs(p[i]);
	}
	for(int i=n;i;--i){
		for(int j=i+1;j<=n;++j){
			int L=n-(j-i);
			dp[i][j][0]=min(dp[i+1][j][0]+L*(p[i+1]-p[i]),dp[i+1][j][1]+L*(p[j]-p[i]));
			dp[i][j][1]=min(dp[i][j-1][1]+L*(p[j]-p[j-1]),dp[i][j-1][0]+L*(p[j]-p[i]));
		}
	}
	printf("%d",min(dp[1][n][0],dp[1][n][1]));
	system("pause");
	return 0;
}

上一题