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

洛谷P3080 [USACO13MAR]牛跑The Cow Run的更多相关文章

  1. 「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 ...

  2. 洛谷——P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  3. 洛谷 P2853 [USACO06DEC]牛的野餐Cow Picnic

    P2853 [USACO06DEC]牛的野餐Cow Picnic 题目描述 The cows are having a picnic! Each of Farmer John's K (1 ≤ K ≤ ...

  4. 洛谷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 ...

  5. 洛谷 3029 [USACO11NOV]牛的阵容Cow Lineup

    https://www.luogu.org/problem/show?pid=3029 题目描述 Farmer John has hired a professional photographer t ...

  6. 洛谷 P2966 [USACO09DEC]牛收费路径Cow Toll Paths

    题目描述 Like everyone else, FJ is always thinking up ways to increase his revenue. To this end, he has ...

  7. 洛谷 P2909 [USACO08OPEN]牛的车Cow Cars

    传送门 题目大意: m个车道. 如果第i头牛前面有k头牛,那么这头牛的最大速度会 变为原本的速度-k*D,如果速度小于l这头牛就不能行驶. 题解:贪心 让初始速度小的牛在前面 代码: #include ...

  8. 洛谷2971 [USACO10HOL]牛的政治Cow Politics

    原题链接 假设只有一个政党,那么这题就退化成求树的直径的问题了,所以我们可以从此联想至\(k\)个政党的情况. 先处理出每个政党的最大深度,然后枚举每个政党的其它点,通过\(LCA\)计算长度取\(\ ...

  9. 洛谷 P2906 [USACO08OPEN]牛的街区Cow Neighborhoods | Set+并查集

    题目: https://www.luogu.org/problemnew/show/P2906 题解: 垃圾水题 #include<cstdio> #include<algorith ...

随机推荐

  1. MLGBZ

    April cloudy, boss rainy, told me he want to kick But coming so,Formosa Heart sad , Down,down,down W ...

  2. selenium 页面超时后捕获异常也无法继续get(url)使用的问题解决方案

    参考这篇博客 http://www.xiaomilu.top/archives/106

  3. gradlew tasks

    D:\AndroidWorkSpace\Qi\LocalM>gradlew tasks > Configure project : AAAA > Configure project ...

  4. 7-10 括号匹配(25 分) 【STL】

    7-10 括号匹配(25 分) 给定一串字符,不超过100个字符,可能包括括号.数字.字母.标点符号.空格,编程检查这一串字符中的( ) ,[ ],{ }是否匹配. 输入格式: 输入在一行中给出一行字 ...

  5. 转载:SPFA算法学习

    转载地址:http://www.cnblogs.com/scau20110726/archive/2012/11/18/2776124.html 粗略讲讲SPFA算法的原理,SPFA算法是1994年西 ...

  6. PC样式reset参考

    /* html5doctor.com Reset Stylesheet */ * { padding:; margin:; list-style: none; } html, body, div, s ...

  7. OpenCv-Python 图像处理基本操作

    1. 图片加载.显示和保存 import cv2 img = cv2.imread("01.jpg") imgGrey = cv2.imread("01.jpg" ...

  8. HihoCoder1673 : 01间隔矩阵([Offer收割]编程练习赛41)(单调队列)

    描述 给定一个N × M的01矩阵,小Hi希望从中找到一个01间隔的子矩阵,并且子矩阵的面积越大越好. 例如对于 0101010 1000101 0101010 1010101 0101010 在右侧 ...

  9. 洛谷【P2664】树上游戏

    浅谈树分治:https://www.cnblogs.com/AKMer/p/10014803.html 题目传送门:https://www.luogu.org/problemnew/show/P266 ...

  10. requests模拟上传照片

    博客园相册管理中有上传照片的功能 现在通过requests库模拟上传图片功能 先手动上传图片,用Fiddler转包,查看到上传图片接口请求格式, ------WebKitFormBoundarySKZ ...