题目描述

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.


题解

不知道为什么能蓝。

这是一类被我们机房称为老王关灯的题鬼嘞只有我这么叫

老(wan)王(e)关(zhi)灯(yuan)

这道题比老王关灯还简单一丢丢,不用算功率啦,

单位时间消耗的dollar直接算未覆盖区间就好啦~

然后这种题 开个long long开不出吃亏开不出上当

还能有效预防越界(QAQ!!!!)

 /*
qwerta
P3080 [USACO13MAR]牛跑The Cow Run
Accepted
100
代码 C++,0.81KB
提交时间 2018-09-22 18:59:00
耗时/内存
231ms, 16760KB
*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
int dis[];
long long f[][][];
int main()
{
//freopen("a.in","r",stdin);
int n;
scanf("%d",&n);
for(int i=;i<=n;++i)
scanf("%d",&dis[i]);
n++;
sort(dis+,dis+n+);
memset(f,,sizeof(f));
int x=;
for(int i=;i<=n;++i)
if(dis[i]==)x=i;
f[x][x][]=f[x][x][]=;
for(int len=;len<=n;++len)
for(int l=,r=len;r<=n;++l,++r)
{
f[l][r][]=min(f[l+][r][]+(dis[l+]-dis[l])*(n-len+),
f[l+][r][]+(dis[r]-dis[l])*(n-len+));
f[l][r][]=min(f[l][r-][]+(dis[r]-dis[l])*(n-len+),
f[l][r-][]+(dis[r]-dis[r-])*(n-len+));
}
cout<<min(f[][n][],f[][n][]);
return ;
}

「USACO13MAR」「LuoguP3080」 牛跑The Cow Run (区间dp的更多相关文章

  1. 洛谷P3080 [USACO13MAR]牛跑The Cow Run

    P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...

  2. 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp

    题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我们可以认为草地是一个数轴上的一些点.Joseph看到这些草非常兴奋,它想把它们全部吃 ...

  3. 众安「尊享e生」果真牛的不可一世么?

    近日,具有互联网基因的.亏损大户(成立三年基本没盈利,今年二季度末亏损近4亿,你能指望它多厉害?).财产险公司—众安推出“尊享e生”中高端医疗保险(财险公司经营中高端医疗真的很厉害?真的是中高端医疗险 ...

  4. 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告

    P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...

  5. 「kuangbin带你飞」专题十七 AC自动机

    layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...

  6. 被「李笑来老师」拉黑之「JavaScript微博自动转发的脚本」

    故事的背景如下图,李笑来 老师于10月19日在 知乎Live 开设 一小时建立终生受用的阅读操作系统 的讲座,他老人家看到大家伙报名踊跃,便在微博上发起了一个 猜数量赢取iPhone7 的活动. 因为 ...

  7. iOS模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.

    Write in the first[写在最前] 对于从事 iOS 开发人员来说,当提到 ** runtime时,我想都可以说出来 「runtime 运行时」和基本使用的方法.相信很多开发者跟我当初一 ...

  8. iOS 模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.

    引导 Copyright © PBwaterln Unauthorized shall not be *copy reprinted* . 对于从事 iOS 开发人员来说,所有的人都会答出「runti ...

  9. 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp

    「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...

随机推荐

  1. minimum-moves-to-equal-array-elements-ii(好)

    https://leetcode.com/problems/minimum-moves-to-equal-array-elements-ii/ package com.company; import ...

  2. 关于C语言中二维数组传參————————【Badboy】

    直接上代码: #include void Fun(int *a[],int m,int n)// { printf("%d\t",*a);//[0][0] /* int e[2][ ...

  3. NHibernate剖析:Mapping篇之Mapping-By-Code(1):概览

    ModelMapper概述 NHibernate3.2版本号集成Mapping-By-Code(代码映射),其设计思想来源于ConfORM.代码总体构思基于"Loquacious" ...

  4. 系统安全-Man in the middleattack

    窃听VS加密(解决数据机密性) 加密由两部分组成:算法&秘钥(算法要够复杂,秘钥要够安全) 对称加密:(Symmetric encryption) 采用单秘钥密码系统的加密方法,同一个秘钥可以 ...

  5. Day1 [上]- 认识Python

    python简单介绍: python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...

  6. kubernetes故障现场一之Orphaned pod

    系列目录 问题描述:周五写字楼整体停电,周一再来的时候发现很多pod的状态都是Terminating,经排查是因为测试环境kubernetes集群中的有些节点是PC机,停电后需要手动开机才能起来.起来 ...

  7. SVG 签名动画 制作

    不知道哪天看到的一个朋友圈里面有发的什么什么免费教签名之类的,就看了下SVG,做这功能还不错. 主要用到的几个属性,需要自行百度一下,不详说  stroke-dashoffset ,  stroke- ...

  8. Dos下mySql

    2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 ...

  9. 用EasyClient开源项目采集Windows摄像头/麦克风的音视频进行RTSP直播

    EasyClient是EasyDarwin开源流媒体团队开发的一款功能丰富的开源PC客户端项目,目前支持Windows.Android版本,后续将支持ios版本,其中Windows版本的EasyCli ...

  10. Colly provides a clean interface to write any kind of crawler/scraper/spider

    Scraping Framework for Golang http://go-colly.org/ https://github.com/gocolly/colly package main imp ...