P3080 [USACO13MAR]牛跑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.

农夫约翰的牧场围栏上出现了一个洞,有N(1 <= N <= 1,000)只牛从这个洞逃出了牧场。这些出逃的奶牛很狂躁,他们在外面到处搞破坏,每分钟每头牛都会给约翰带来1美元的损失。约翰必须用缰绳套住所有的牛,以停止他们搞破坏。

幸运的是,奶牛们都在牧场外一条笔直的公路上,牧场的大门恰好位于公里的0点处。约翰知道每头牛距离牧场大门的距离P_i(-500,000 <= P_i <= 500,000, P_i != 0)

约翰从农场大门出发,每分钟移动一个单位距离,每到一头牛所在的地点,约翰就会给它套上缰绳,套缰绳不花时间。按怎样的顺序去给牛套缰绳才能使约翰损失的费用最少?

输入输出格式

输入格式:

  • 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
输出样例#1:

50

说明

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

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.

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
using namespace std;
#define maxn 1010
int n,a[maxn],d[maxn][maxn];
long long ans=;
bool vis[maxn];
void dfs(int now,int cnt,long long sum){
if(sum>ans)return;
if(cnt==n){
ans=min(ans,sum);
return;
}
for(int i=;i<=n;i++){
if(!vis[i]){
vis[i]=;
dfs(i,cnt+,sum+d[now][i]*(n-cnt));
vis[i]=;
}
}
}
int main(){
scanf("%d",&n);
for(int i=;i<=n;i++)scanf("%d",&a[i]),d[][i]=abs(a[i]);
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
d[i][j]=abs(a[i]-a[j]);
dfs(,,);
cout<<ans;
}

42分 暴力

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n,s[],f[][][],ans;
int main(){
freopen("Cola.txt","r",stdin);
int i,j,k;cin>>n;
for(i=;i<=n;i++)cin>>s[i];
s[i]=;n++;
sort(s+,s+n+);
for(i=;i<=n;i++)if(s[i]>)break;
k=i-;//0的位置
memset(f,/,sizeof(f));
f[k][k][]=;
f[k][k][]=;
for(i=k+;i<=n;i++)f[k][i][]=f[k][i-][]+(s[i]-s[i-])*(n-i+k);
for(i=k-;i>=;i--)f[i][k][]=f[i+][k][]+(s[i+]-s[i])*(n-k+i);
for(i=k-;i>=;i--)
for(j=k+;j<=n;j++){
f[i][j][]=min(f[i+][j][]+(s[i+]-s[i])*(n-j+i),f[i+][j][]+(s[j]-s[i])*(n-j+i));
f[i][j][]=min(f[i][j-][]+(s[j]-s[i])*(n-j+i),f[i][j-][]+(s[j]-s[j-])*(n-j+i));
}
cout<<min(f[][n][],f[][n][])<<endl;
return ;
}

100分 区间dp

最新文章

  1. C++ 事件驱动型银行排队模拟
  2. Leetcode Construct Binary Tree from Inorder and Postorder Traversal
  3. JMeter学习-034-JMeter调试工具之一---HTTP Mirror Server
  4. Windows 8.1 新增控件之 Flyout
  5. [Java基础] SequenceInputStream输入合并流
  6. [hive小技巧]使用limit查询变成抽样,而不是全盘扫描
  7. (八)shell中的循环结构
  8. IOS开发设计思路
  9. arcgis中注记的高级处理
  10. 关于“学习Linux用什么系统”的解答
  11. IE 创建条件样式
  12. 服务器编程入门(11)TCP并发回射服务器实现 - 单线程select实现
  13. Linux学习 -- 日志管理
  14. 安装及配置jdk和tomcat
  15. linun 乌班图 vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装
  16. 正确JAVA从本机获取IP地址的方法
  17. Java学习笔记35(sql补充)
  18. C++ MFC棋牌类小游戏day5
  19. 微信支付 php兼容问题
  20. 实现checkbox的多选

热门文章

  1. 4.Web工程师的开发工具箱
  2. &lt;ReversingEngineering&gt;关于windows32位系统下的dll注入技术经验汇
  3. Ubuntu 17.4下如何安装和配置flash player
  4. BZOJ 3043 IncDec Sequence:反向差分
  5. BZOJ 1641 [Usaco2007 Nov]Cow Hurdles 奶牛跨栏:新版floyd【路径上最大边最小】
  6. CentOS Wifi Connection
  7. 集训Day10
  8. POJ-3680:Intervals (费用流)
  9. 关于qwerta
  10. 【Lintcode】106.Convert Sorted List to Balanced BST