思路:首先因为要让距离尽量大,所以奶牛1一定在1号牛棚,奶牛n一定在s号牛棚,然后考虑dp。

因为总距离为s-1,然后要使长度为d的段数尽量多,那么剩下的一定就是d+1的段数,也就是s-(n-1)*d。

然后f[i][j]表示保证前i个牛棚合法且前面长为d+1的段数为j的答案,然后第i个牛棚的位置其实就是(i-1)*d+j。

又因为第i-1个牛棚只可能是相隔d或d-1,所以有f[i][j]=min(f[i-1][j],f[i-1][j-1])+abs(a[i]-(i-1)*d-j)

答案就是f[n][m]。

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define maxn 1505 int n,s;
int a[maxn];
int f[maxn][maxn]; inline int read(){
int x=0;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar());
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x;
} inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
} int main(){
n=read(),s=read();int d=(s-1)/(n-1),m=s-(n-1)*d;
for (int i=1;i<=n;i++) a[i]=read();
sort(a+1,a+n+1);
memset(f,63,sizeof(f)),f[1][1]=a[1]-1;
for (int i=2;i<=n;i++)
for (int j=1;j<=m&&j<=i;j++)
f[i][j]=min(f[i-1][j],f[i-1][j-1])+abs(a[i]-d*(i-1)-j);
print(f[n][m]);
return 0;
}

bzoj3405:[Usaco2009 Open]Grazing2 移动牛棚的更多相关文章

  1. bzoj千题计划152:bzoj3405: [Usaco2009 Open]Grazing2 移动牛棚

    http://www.lydsy.com/JudgeOnline/problem.php?id=3405 n个牛棚,n-1段 因为要求距离尽量大,而且尽可能多的为d 所以: 第1个牛棚一定在位置1 最 ...

  2. 3409: [Usaco2009 Oct]Barn Echoes 牛棚回声

    3409: [Usaco2009 Oct]Barn Echoes 牛棚回声 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 57  Solved: 47[ ...

  3. BZOJ-USACO被虐记

    bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...

  4. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  5. bzoj 1576: [Usaco2009 Jan]安全路经Travel 树链剖分

    1576: [Usaco2009 Jan]安全路经Travel Time Limit: 10 Sec  Memory Limit: 64 MB Submit: 665  Solved: 227[Sub ...

  6. Bzoj 1579: [Usaco2009 Feb]Revamping Trails 道路升级 dijkstra,堆,分层图

    1579: [Usaco2009 Feb]Revamping Trails 道路升级 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 1573  Solv ...

  7. BZOJ3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 51  Solved: 4 ...

  8. BZOJ 1770: [Usaco2009 Nov]lights 燈( 高斯消元 )

    高斯消元解xor方程组...暴搜自由元+最优性剪枝 -------------------------------------------------------------------------- ...

  9. BZOJ 3402: [Usaco2009 Open]Hide and Seek 捉迷藏

    题目 3402: [Usaco2009 Open]Hide and Seek 捉迷藏 Time Limit: 3 Sec  Memory Limit: 128 MB Description     贝 ...

随机推荐

  1. SpringMVC经典系列-14自己定义SpringMVC的拦截器---【LinusZhu】

    注意:此文章是个人原创.希望有转载须要的朋友们标明文章出处.假设各位朋友们认为写的还好,就给个赞哈.你的鼓舞是我创作的最大动力,LinusZhu在此表示十分感谢,当然文章中如有纰漏,请联系linusz ...

  2. delphi 取得汉字的第一个字母

    功能说明://取得汉字的第一个字母 function GetPYIndexChar( hzchar:string):char;begin  caseWORD(hzchar[1])shl8+WORD(h ...

  3. ListView返回选中的多项目

    ListView返回选中的多项目     procedure TForm1.Button3Click(Sender: TObject);var  s: string;  I: Integer;begi ...

  4. Swift 3.0 的 open,public,internal,fileprivate,private 关键字

      import Foundation   /// final的含义保持不变 public final class FinalClass { }   // 这个类在ModuleA的范围外是不能被继承的 ...

  5. gulp如何自定义插件

    gulp是基于”流“的构建工具,上层流的输出就是下层流的输入,为了更好的支持链式操作,可以使用through2或者map-stream这两个库来对node stream做一层包装 这里,我们就使用th ...

  6. debian7 oracle11g 解决 link binaries 错误方案

    ln -s /etc /etc/rc.d ln -s /usr/bin/awk /bin/awk ln -s /usr/bin/basename /bin/basename ln -s /usr/bi ...

  7. java_log4j多文件配置

    今天配置了log4j中写多个文件的内容,配置了半天才搞出来,为了避免类似问题,写个博客吧. 首先说一下需求,每天要在7个文件夹中生成文件,文件格式为xxx.log.2000.01.01,自己开发个写文 ...

  8. Computer skills one can learn within one day

    Computer related technical skills are usually thought as complicated and difficult to understand. It ...

  9. 琐碎-关于StringTokenizer工具

    属于:java.util包 构造函数: 1. StringTokenizer(String str):构造一个用来解析str的StringTokenizer对象.java默认的分隔符是“空格”.“制表 ...

  10. 组合数学poj 1496 1850 同样的代码过两题

    Description   1942 Transmitting and memorizing information is a task that requires different coding ...