今晚CF GYM A题,神坑。。

原题:

Aspen Avenue

``Phew, that was the last one!'' exclaimed the garden helper Tim as he threw the last tree plant to the ground. His employer, countess Esmeralda Hunt who owned the estate, has ordered him to arrange an avenue of aspen trees along both sides of the front road leading up to the house. The first trees in the avenue are supposed to be planted at the very beginning of the road, and the last trees of the avenue at the very end of the road. Tim, who was constantly being reminded of her sense of accuracy, knew that the countess would insist on the trees being placed in perfectly aligned tree pairs, one on each side of the road, and with exactly the same spacing between the pairs along the road. However, when bringing the tree plants to the estate, Tim had just dropped them arbitrarily along the left side of the road and was now facing the task of moving the trees to their correct positions to meet the countess's requirements. Being stronger in mind than in arms, and since it was time for a coffee break anyway before he started digging, he sat down to figure out which trees to move to which positions so as to minimize the total (Euclidean) distance that he had to move the trees.

Input

The input file contains several test cases, each of them as described below.

The input starts with a positive even integer N between 4 and 2000 (inclusive), giving the total number of trees in the avenue. The next line contains two integers L and W , where 1L10000 is the length of the road, in meters, and 1W20 is the width of the road, in meters. The next N lines each describe where Tim had dropped of the trees. Each such line contains an integer 0pL indicating the position of a tree plant along the left side of the road, measured in meters from the start of the road.

Output

For each test case, write to the output the smallest total number of meters the tree plants need to be moved, on a line by itself. The answer should be given with an absolute or relative error of at most 10-6 .

Sample Input

4
10 1
1
0
10
10
6
10 1
0
9
3
5
5
6

Sample Output

2.4142135624
9.2853832858

题意:

  一条长为L,宽为W的道路,两旁共需要种N棵树。每两棵树作为一对,严格戳在距起点相同距离的道路两旁。每相邻两对树之间的距离要求严格相等。前两棵在0处,最后两棵在L处。

  初始时所有树都在道路的左侧乱序散布(输入位置),问最少需要将树总共移动多少欧氏距离才能把他们移动到规定的位置。

思路:

  开始会想到sort一下,然后依次让两棵树跑到距起点最近的没有树的位置去,稍微计算一下,能发现将距离规定位置最近的那棵树放在道路左边,将另一棵树放到道路右边是对这两棵树来说最优的。但是连WA 4发以后放弃了。。。

  最后确定DP来做,但由于是树形展开(from God crccw),所以直接for是不大行的,用dfs记忆化搜索可以解决。

  dp[i][j]=dfs(i,j)=min(dfs(i+1,j)+dis(tree[i+j],左侧i+1位置) ,dfs(i,j+1)+dis(tree[i+j],右侧j+1位置));

  tips:dp[i][j]初始化为INF;下标从0开始

代码:

 #include<stdio.h>
#include<math.h>
#include<algorithm>
using namespace std; const double INF = ;
int n;
double l,w;
double a[];
double itv;
double dp[][];
bool fact[][]; void ini()
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
dp[i][j]=INF;
fact[i][j]=;
}
} double dfs(int l,int r)
{
if(l>=n&&r>=n) return ;
if(fact[l][r]) return dp[l][r];
fact[l][r]=; if(l<n) dp[l][r]=min(dfs(l+,r)+fabs(a[l+r]-l*itv),dp[l][r]);
if(r<n) dp[l][r]=min(dfs(l,r+)+sqrt((a[l+r]-r*itv)*(a[l+r]-r*itv)+w*w),dp[l][r]); return dp[l][r];
} int main()
{
scanf("%d",&n);
scanf("%lf%lf",&l,&w);
for(int i=;i<n;i++)
{
scanf("%lf",&a[i]);
}
ini();
sort(a,a+n);
n=n/;
itv=l/((double)n-); printf("%.10f\n",dfs(,));
return ;
}

至于开始那种贪心做法为何不对,经过一番多次平方的不等式计算应该可以得到。

题外话:

今天在缺少雷神的极其恶劣的情况下,神队友crccw带着我做了zoj monthly march 2013和CF GYM,GYM是NCPC2008的题目,明明是A题,看起来也不是很难,,WA了5发,看了大牛博客才过。。也是学习了。。。

自己太弱,不过只要脸皮厚,就能继续学习。。多靠神队友。

晚安!

UVa11555 - Aspen Avenue的更多相关文章

  1. Aspen安装过程报错总结

    前几天一直帮朋友安装Aspen v11,因为之前的老版本总是报错,报错内容大概是证书过期了, 一开始朋友电脑上的老版本的Aspen 8卸载了,删除之前的数据库SqlServer 2012 ,然后重新安 ...

  2. Aspen 安装

    按原安装后破解不成功后: 从下载文件夹中,找到 AspenONEV8.\Patch\-STRGXI2.zip,里面有个 STRGXI2.dll,将该文 件复制到: C:\Program Files(x ...

  3. AtCoder Beginner Contest 184 E - Third Avenue (BFS)

    题意:给你一张图,\(S\)表示起点,\(G\)表示终点,\(.\)表示可以走,#表示不能走,小写字母可以传送到任意一个相同的字母的位置,问从\(S\)走到\(G\)的最小步数. 题解:假如不考虑字母 ...

  4. Aspen.net core 身份认证

  5. The 10 Best Neighborhoods in Seattle

    https://www.seattlemet.com/articles/2015/4/24/the-10-best-neighborhoods-in-seattle-may-2015 By Darre ...

  6. 以bank account 数据为例,认识elasticsearch query 和 filter

    Elasticsearch 查询语言(Query DSL)认识(一) 一.基本认识 查询子句的行为取决于 query context filter context 也就是执行的是查询(query)还是 ...

  7. 读书笔记--SQL必知必会--建立练习环境

    书目信息 中文名:<SQL必知必会(第4版)> 英文名:<Sams Teach Yourself SQL in 10 Minutes - Fourth Edition> MyS ...

  8. 读书笔记--SQL必知必会07--创建计算字段

    7.1 计算字段 字段(field),基本与列(column)含义相同. 利用计算字段可以直接从数据库中检索出转换.计算或格式化过的数据. 计算字段不实际存在于数据库表中,是运行时在SELECT语句内 ...

  9. 读书笔记--SQL必知必会12--联结表

    12.1 联结 联结(join),利用SQL的SELECT在数据查询的执行中联结表. 12.1.1 关系表 关系数据库中,关系表的设计是把信息分解成多个表,一类数据一个表,各表通过某些共同的值互相关联 ...

随机推荐

  1. 百度地图API地点搜索-获取经纬度

    分享一下地图上的地点搜索和鼠标点击获取地点经纬度,这些都是地图比较基本和实用的代码,其中还包括了根据用户IP进行地图的显示.改变地图上的鼠标样式.启用滚轮缩放等,算是半入门吧,其他的一些可以自己参考百 ...

  2. Linux 安全模块

    LSM是Linux Secrity Module的简称,即linux安全模块.其是一种轻量级通用访问控制框架,适合于多种访问控制模型在它上面以内核可加载模块的形实现.用户可以根据自己的需求选择合适的安 ...

  3. (转)Unity3D移动平台动态读取外部文件全解析

    Unity3D移动平台动态读取外部文件全解析 c#语言规范 阅读目录 前言: 假如我想在editor里动态读取文件 移动平台的资源路径问题 移动平台读取外部文件的方法 补充: 回到目录 前言: 一直有 ...

  4. HDU 1054 Strategic Game(树形DP)

    Strategic Game Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

  5. map.entry<k,v>小用法(转)

    你是否已经对每次从Map中取得关键字然后再取得相应的值感觉厌倦?使用Map.Entry类,你可以得到在同一时间得到所有的信息.标准的Map访问方法如下: Set keys = map.keySet( ...

  6. .NET 4.0里异常处理的新机制

    非原创,转载自:   http://www.cnblogs.com/killmyday/archive/2010/09/05/1818533.html .NET里不能捕捉(catch)到一些异常了,而 ...

  7. jquery指定div右键事件

    <div class="nav_list_item">需要淡季右键的div</div> 我们要 给这个div注册鼠标右键的时候需要先禁用该div的网页右键菜 ...

  8. 不停止MySQL服务增加从库的两种方式【转载】

    现在生产环境MySQL数据库是一主一从,由于业务量访问不断增大,故再增加一台从库.前提是不能影响线上业务使用,也就是说不能重启MySQL服务,为了避免出现其他情况,选择在网站访问量低峰期时间段操作. ...

  9. Android media媒体库分析之:MediaProvider

    在做Android媒体应用程序时(Audio.Image.Video)需要对Android的媒体提供者(MediaProvider)做详细的分析,下面记录一下我的收获: 一.获取MediaProvid ...

  10. java在CMD环境下执行需注意字符集设定

    最近有个小工具需要将DMS系统中随机文件名替换为原始文件名,当导出原始文件名到csv文件中,用小 工具读取然后rename时,发现在eclipse环境下运行正常,简繁中文名称也正常:但放到cmd中执行 ...