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. API的理解和使用——集合

    集合类型的命令及时间复杂度  区间 命令 功能 时间复杂度  集合内 sadd key element [element ... ]  添加元素 O(k),k是元素个数 srem key elemen ...

  2. Java for LeetCode 096 Unique Binary Search Trees

    Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...

  3. Quartz Job scheduling 基础实现代码

    Quartz 集成在 SpringBoot 中分为 config.task.utils.controller 和 MVC 的三层即 controller.service.dao 和 entity. c ...

  4. 加州小学grade1,学习计划

    Visual vocabulary Grammar Spelling Maths Chapter 1 Patterns and Number SenseChapter 2Understanding A ...

  5. (2)struts2配置祥解

    struts工作流程 反射 : 1.构造对象使用构造器 //类似为Servlet public class AddAction { public AddAction(){ System.out.pri ...

  6. POJ 3744 Scout YYF I:概率dp

    题目链接:http://poj.org/problem?id=3744 题意: 有n个地雷,位置为pos[i]. 在每个位置,你向前走一步的概率为p,向前走两步的概率为1-p. 你的初始位置为1. 问 ...

  7. 使用jedisPool管理jedis,使用jedis操作redis

    ps:jedis是redis在java中的客户端操作工具 package com.test; 2 3 import java.util.HashMap; 4 import java.util.Iter ...

  8. Java_泛型_01_T与?

    二.参考文档 1.JAVA泛型通配符T,E,K,V区别,T以及Class<T>,Class<?>的区别

  9. 在Asterisk CLI里面采用originate发起一个呼叫

    Asterisk cli下面可以执行很多命令,originate的用途是发起一个呼叫然后连接到指定的应用或上下文. 跟.call呼叫文件和AMI管理接口里的外呼功能一样,有两种语法格式: 呼叫成功转应 ...

  10. NPM 在MacOSX中的使用技巧

    经常看到有人说『为啥npm install 的时候报错,显示EACCESS错误…』,之前大家都是sudo大法解决问题,也没太在意. 至于这个问题是brew安装工具的时候造成的,还是系统修改磁盘权限造成 ...