洛谷P3080 [USACO13MAR]牛跑The Cow Run
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.
输入输出样例
4
-2
-12
3
7
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
洛谷P3080 [USACO13MAR]牛跑The Cow Run的更多相关文章
- 「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (区间dp
题目描述 Farmer John has forgotten to repair a hole in the fence on his farm, and his N cows (1 <= N ...
- 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic
P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...
- 洛谷P2853 [USACO06DEC]牛的野餐Cow Picnic
题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ 100) cows is grazing in one of N ...
- 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup
https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...
- 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths
题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...
- 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars
传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...
- 洛谷2971 [USACO10HOL]牛的政治Cow Politics
原题链接 假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况. 先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\ ...
- 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集
题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...
随机推荐
- 【docker】学习笔记一:制作自己的centos6.9镜像
前言: 最近开始研究docker,在这里做一个记录. 本来开始想用centos7系列做镜像,毕竟是最新版本的centos,但是centos7有一个严重的bug,就是正常启动的镜像不能使用systemc ...
- 负载均衡,会话保持,session同步(转)
一,什么负载均衡一个新网站是不要做负载均衡的,因为访问量不大,流量也不大,所以没有必要搞这些东西.但是随着网站访问量和流量的快速增长,单台服务器受自身硬件条件的限制,很难承受这么大的访问量.在这种情况 ...
- 使用aspnet_regiis.exe重新注册.NET Framework
cd C:\Windows\Microsoft.NET\Framework64\v4.0.30319 aspnet_regiis.exe -i 重新安装IIS以后,需要用aspnet_re ...
- React之jsx语法特性
jsx 语法,直接可以在js中使用html标签. 还可以通过花括号的形式,在html标签中,写js表达式. <div> { 1 + 2 } hello,world! </div> ...
- Hadoop- Wordcount程序原理及代码实现
如果对Hadoop- MapReduce分布式计算框架原理还不熟悉的可以先了解一下它,因为本文的wordcount程序实现就是MapReduce分而治之最经典的一个范例. 单词计数(wordcount ...
- matlab之结构体数组struct
以下内容来自于:https://blog.csdn.net/u010999396/article/details/54413615/ 要在MALTAB中实现比较复杂的编程,就不能不用struct类型. ...
- Listen 82
Doc Calls Deconditioning a Condition Doctors know a lot about prescribing medications. "Take tw ...
- linux命令学习笔记(23):Linux 目录结构
对于每一个Linux学习者来说,了解Linux文件系统的目录结构,是学好Linux的至关重要的一步.,深入了解linux文件 目录结构的标准和每个目录的详细功能,对于我们用好linux系统只管重要,下 ...
- kettle结合MySQL生成保留最近6个月月度报告_20161009
之前计算用户ID各月的金额(各月在列字段),用的是下面代码 ,b.金额,,b.金额,,b.金额,NULL)) AS 9月金额 FROM ( SELECT city AS 城市,DATE_FORMAT( ...
- 浏览器,tab页显示隐藏的事件监听--页面可见性
//监听浏览器tab切换,以便在tab切换之后,页面隐藏的时候,把弹幕停止 document.addEventListener('webkitvisibilitychange', function() ...