「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 <= 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.
输入输出样例
说明
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.
题解
不知道为什么能蓝。
这是一类被我们机房称为老王关灯的题鬼嘞只有我这么叫
这道题比老王关灯还简单一丢丢,不用算功率啦,
单位时间消耗的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的更多相关文章
- 洛谷P3080 [USACO13MAR]牛跑The Cow Run
P3080 [USACO13MAR]牛跑The Cow Run 题目描述 Farmer John has forgotten to repair a hole in the fence on his ...
- 【bzoj1742】[Usaco2005 nov]Grazing on the Run 边跑边吃草 区间dp
题目描述 John养了一只叫Joseph的奶牛.一次她去放牛,来到一个非常长的一片地,上面有N块地方长了茂盛的草.我们可以认为草地是一个数轴上的一些点.Joseph看到这些草非常兴奋,它想把它们全部吃 ...
- 众安「尊享e生」果真牛的不可一世么?
近日,具有互联网基因的.亏损大户(成立三年基本没盈利,今年二季度末亏损近4亿,你能指望它多厉害?).财产险公司—众安推出“尊享e生”中高端医疗保险(财险公司经营中高端医疗真的很厉害?真的是中高端医疗险 ...
- 「洛谷P2906」[USACO08OPEN]牛的街区Cow Neighborhoods 解题报告
P2906 [USACO08OPEN]牛的街区Cow Neighborhoods 题目描述 Those Who Know About Cows are aware of the way cows gr ...
- 「kuangbin带你飞」专题十七 AC自动机
layout: post title: 「kuangbin带你飞」专题十七 AC自动机 author: "luowentaoaa" catalog: true tags: - ku ...
- 被「李笑来老师」拉黑之「JavaScript微博自动转发的脚本」
故事的背景如下图,李笑来 老师于10月19日在 知乎Live 开设 一小时建立终生受用的阅读操作系统 的讲座,他老人家看到大家伙报名踊跃,便在微博上发起了一个 猜数量赢取iPhone7 的活动. 因为 ...
- iOS模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.
Write in the first[写在最前] 对于从事 iOS 开发人员来说,当提到 ** runtime时,我想都可以说出来 「runtime 运行时」和基本使用的方法.相信很多开发者跟我当初一 ...
- iOS 模式详解—「runtime面试、工作」看我就 🐒 了 ^_^.
引导 Copyright © PBwaterln Unauthorized shall not be *copy reprinted* . 对于从事 iOS 开发人员来说,所有的人都会答出「runti ...
- 「bzoj1003」「ZJOI2006」物流运输 最短路+区间dp
「bzoj1003」「ZJOI2006」物流运输---------------------------------------------------------------------------- ...
随机推荐
- Examples osgparticleshader例子学习
Examples osgparticleshader 粒子与shader的使用 参考文档 http://blog.csdn.net/csxiaoshui/article/details/234345 ...
- Git以及github的使用方法(四),版本回退
现在,你已经学会了修改文件,然后把修改提交到Git版本库,现在,再练习一次,修改readme.txt文件如下: Git is a distributed version control system. ...
- ThinkPHP学习(五)图片验证码
今天用到图片验证码的功能,在网上找到ThinkPHP的下面代码: Public function verify(){ import('think.Image'); Image::buildImageV ...
- 整合Hibernate3.x
As of Spring 3.0, Spring requires Hibernate 3.2 or later. Hibernate 3和Hibernate 4有一些区别,所以对于spring而已, ...
- VueJS数据绑定文本显示:{{message}}
HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <titl ...
- Spring.net1.3.1+Nhibernate3.0+Mysql/Access/SqlServer/Oracel/SQlite
详情请看我的博文:http://www.ruisoftcn.com/blog/article.asp?id=999
- DM8168 unrecoverable error: OMX_ErrorBadParameter (0x80001005) [resolved]
DM8168 custom board 成功启动系统之后想先測一下8168编解码功能,把开发包里的examples跑一遍.启动完毕后.连上HDMI显示,在starting Matrix GUI app ...
- oracle sql 当初始化数据时避免重复主键
一:当有主键序列自动增长时候(序列为:seq_cct_id) insert into cs_cost_type (CCT_ID, CCT_NAME, CCT_RATE, CCT_RATE_TYPE, ...
- linux的su和sudo(转载)
来源:http://www.jb51.net/LINUXjishu/12713.html 一. 使用 su 命令临时切换用户身份 1.su 的适用条件和威力 su命令就是切换用户的工具,怎么理解呢?比 ...
- cocos2d-x lua 中使用protobuf并对http进行处理
cocos2d-x lua 中使用protobuf并对http进行处理 本文介绍 cocos2d-x lua 中使用http 和 基于cocos2d-x 对lua http的封装(部分ok) 本博客链 ...