(File IO): input:graze2.in output:graze2.out

Description

译题:
  FJ有N(2<=N<=1,500)头牛编号为1到N,FJ新盖了S(N<=S<=1,000,000)个牛棚,编号为1到S,S个牛棚排成一排,相邻牛棚距离为1。
每个牛棚只能住一头牛,每头牛都选择了一个牛棚P_i来休息,当两头牛离得太近时就会变得很暴躁,FJ想移动一些牛到其他牛棚使得他们之间的间距尽可能大,同时FJ又希望这N-1个间距尽可能相似。
具体一点说,FJ希望所有间距与(S-1)DIV(N-1)最多相差1,而且希望这N-1个间距尽可能多的等于(S-1)DIV(N-1)。例如4个牛住8个牛棚,可以有以下方案:1,3,5,8或1,3,6,8,但1,2,4,7或1,2,4,8是不符合要求的。
帮助FJ用最少的移动距离满足要求。

原题:
Farmer John has N (2 <= N <= 1,500) prize milk cows conveniently numbered 1..N. His newly-painted barn has S (N <= S <= 1,000,000) stalls (conveniently numbered 1..S) in a single long line; each stall is a unit distance from its neighboring stall(s).

The cows have made their way to the stalls for a rest; cow i is in stall P_i. Antisocial as they are, the cows get grumpy if they are situated in stalls very close to each other, so Farmer John wants to move the cows to be as spread out as possible.

FJ wants to make sure that the N - 1 distances between adjacent cows are as large as possible, and he would also like them to be similar to each other (i.e., close to equi-distant spacing).

In particular, FJ would like all distances between adjacent cows to be at most 1 different from (S - 1) / (N - 1), where integer division is used. Moreover, he would like as many of these distances as possible to be exactly equal to (S - 1) / (N - 1) [integer division]. Thus, with four cows and eight stalls, one can place the cows at positions 1, 3, 5, 8 or 1, 3, 6, 8 but not at 1, 2, 4, 7 or 1, 2, 4, 8.

Help FJ spread the cows as efficiently as possible by calculating and reporting the minimum total distance that the cows have to move in order to achieve proper spacing. Ignore the distance it takes for a cow to enter or exit a stall.

Input

  • Line 1: Two space-separated integers: N and S

  • Lines 2..N+1: Line i+1 contains the single integer: P_i

      第1行:两个空格隔开的整数N和S
      第2到N+1行:第i+1行包含一个整数P_i

Output

  • Line 1: A single integer: the minimum total distance the cows have to travel. This number is guaranteed to be under 1,000,000,000 (thus fitting easily into a signed 32-bit integer).
      输出一个整数表示最少移动距离。

Sample Input

5 10
2
8
1
3
9

Sample Output

4

Data Constraint

Hint

【样例说明】
1 2 3 4 5 6 7 8 9 10
Init Stall | A | B | C | . | . | . | . | D | E | . |
Final Stall | A | . | B | . | C | . | . | D | . | E |
Distance moved | 0 | . | 1 | . | 2 | . | . | 0 | . | 1 |

思路:
题目中没有说明两个条件:第一位和最后一位必须有牛棚,所以就DP啦

pascal:

type arr=array[0..1500] of longint;
var n,s,i,j,l,x,y:longint;
a:arr;
f:array[0..1500,0..1500] of longint;
function min(x,y:longint):longint;
beginif x<y then exit(x) else exit(y);
end;
procedure dg();
beginfor i:=2 to n dobegins:=min(i-1,y);f[i,0]:=f[i-1,0]+abs(a[i]-i*x+x-1);for j:=1 to s do f[i,j]:=min(f[i-1,j-1],f[i-1,j])+abs(a[i]-i*x+x-j-1);end;
end;
procedure chuli();
var i,j:longint;
beginfor i:=0 to 1500 dobeginfor j:=0 to 1500 dobeginf[i,j]:=maxlongint div 3;end;end;f[1,0]:=a[1]-1;x:=(s-1)div(n-1);y:=(s-1)mod(n-1);
end;
procedure qsort_min_max(l,r:longint; var px:arr);
var i,j,mid:longint;
begini:=l;j:=r;mid:=px[(i+j) div 2];while i<=j dobeginwhile px[i]<mid do inc(i);while px[j]>mid do dec(j);if i<=j thenbeginpx[0]:=px[i];px[i]:=px[j];px[j]:=px[0];inc(i);dec(j);end;end;if i<r then qsort_min_max(i,r,px);if l<j then qsort_min_max(l,j,px);
end;
procedure init();
beginreadln(n,s);for i:=1 to n do readln(a[i]);qsort_min_max(1,n,a);chuli();dg();writeln(f[n,y]);
end;
beginassign(input,'graze2.in');reset(input);assign(output,'graze2.out');rewrite(output);init();close(input);close(output);
end.

牛棚(graze2.pas/c/cpp)相关推荐

  1. {题解}[jzoj1298]牛棚(graze2.pas/c/cpp)

    jzoj1298 Description FJ有N(2<=N<=1,500)头牛编号为1到N,FJ新盖了S(N<=S<=1,000,000)个牛棚,编号为1到S,S个牛棚排成一 ...

  2. 【模拟】相似度 (similar.pas/c/cpp)

    相似度 similar.pas/c/cpp 问题描述: 有两个长度相等的DNA序列(只包含A,T,G,C的字符串),我们想知道它的相似度,它的相似度是这样定义的. 首先相似度为0. 若在对应位置出现A ...

  3. 【字符串处理】文明的复兴 words.pas/c/cpp/in/out

    文明的复兴 (words.pas/c/cpp/in/out) Problem 战神Prince&Gush回归了,但许多原先的上层精灵越来越不安分.他们无法忍受失去权力的空虚感,开始重新寻找新的 ...

  4. 【暴力枚举】速算游戏 fun.pas/c/cpp

    速算游戏 fun.pas/c/cpp 源程序名 fun.pas|c|cpp 输入文件名 fun.in 输出文件名 fun.out 时间限制 1s/testcase 空间限制 32MB 问题描述 jyx ...

  5. 最接近神的人(sophist/pas/c/cpp)

    最接近神的人(sophist/pas/c/cpp) [题目描述] 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案.而石门上 ...

  6. 试题二:最接近神的人(sophist/pas/c/cpp) (逆序对)

    试题二:最接近神的人(sophist/pas/c/cpp) [题目描述] 破解了符文之语,小FF开启了通往地下的道路.当他走到最底层时,发现正前方有一扇巨石门,门上雕刻着一幅古代人进行某种活动的图案. ...

  7. 【打表】Round Numbers rndnum.pas/c/cpp

    Round Numbersrndnum.pas/c/cpp 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过扔硬币的方式. 所 ...

  8. 开心农场(happyfarm.pas/c/cpp)

    [问题描述] 这个夏天,与众不同.QQ空间也引入了最近流行的社区交互类游戏<开心农场>.自然而然地,Chroi也成为了众多农场主的一员.可是Chroi整个暑假忙于OI,没什么时间照顾农场, ...

  9. “疲劳”传说 (legend.pas/c/cpp)

    [题目描述] 炉石传说:魔兽英雄传是一款由暴雪开发的非常流行的游戏.这个问题建立在这个游戏的基础上,但是即使你不知道这个游戏,你也能非常轻松的解决这个问题. 每一局游戏是两个对手1v1的比赛.炉石传说 ...

最新文章

  1. 循环出按钮点击按钮显示按钮上面文字 vue el-button_前端学习计划之VUE学习(一)...
  2. 数据库外键的使用原则
  3. USACO 2.3 Money Systems(DP)
  4. 遍历 List 之效率
  5. 设计模式:讲在设计模式之前
  6. 带有光纤的可扩展,健壮和标准的Java Web服务
  7. Tensorflow(0)--Tensorboard
  8. java(包括springboot)读取resources下文件方式
  9. VC++绘图时,利用双缓冲解决屏幕闪烁 转载
  10. 分享一款影视源码_爱客影院网站3.5.6版本
  11. 牛客网项目里的数据表
  12. html 好看的英文字体,力荐!8款超有设计感的英文字体。
  13. 心理声学模型在感知音频编码中的应用
  14. tp无线路由器设置打印服务器,tplink路由器wdr7500怎么设置usb打印服务器
  15. 怎么删除网络文件服务器的帐号,linux连个文件都删除不了,什么鬼!
  16. 怎么实现使用手机号、邮箱、用户名登录
  17. #include<intrins.h>中包含的函数说明
  18. Day16-01 P71 面向对象12什么是多态 P72 对象13:instanceof和类型转换 P73 static关键字详解 P74 抽象类 P75 接口的定义与实现 P76N种内部类
  19. 微信小程序设置锚点定位,wx.pageScrollTo
  20. 读王坚博士著作《在线》笔记(一)

热门文章

  1. 长江水质的评价和预测——综合指标评价
  2. HTTP返回内容前面出现一个多出来的数字 Transfer-Encoding: chunked
  3. 2017云栖大会参会体验
  4. 记录第一次写静态网页-百度首页
  5. VScode 代码对齐快捷键
  6. 打印机使用技巧:打印机的另类用法
  7. C# | 上位机开发新手指南(一)概述
  8. Why “transform(s.begin(),s.end(),s.begin(),tolower)” can't be complied successfully?
  9. Python笔记 - Python切片
  10. 高并发分布式下生成全局ID的几种方法