Brief Intro:

有n+1个点,其中n个点在X轴上,求从第k个点出发最短的汉密尔顿路径

Solution:

分类讨论+逐个枚举

设dist(i)是第i个点到n+1的距离

cal1(l,r)是n+1到dat[l]~dat[r]的最短距离

cal2(l,r)是dat[k]到dat[l]~dat[r]再到n+1的最短距离

1、如果k=n+1,则答案明显是线段长+min(dist(1),dist(n))

2、否则要逐个枚举

可以发现共有2种可能:n+1连到线段的边有1条/2条

如为1条,则结果为cal1(1,n);

如为2条,则可将路径分为dat[k]到dat[l]~dat[r]再到n+1  +  n+1到剩余的点

明显剩余的点连续时有最优解

于是res[i]=min( cal1(1,i-1)+cal2(i,n) , cal2(1,i-1)+cal1(i,n) )。

Code:

#include <bits/stdc++.h>

using namespace std;

const int MAXN=1e5+;
double dat[MAXN],x,y,kx;
int n,k; double dist(int pos){return hypot(x-dat[pos],y);} double cal1(int l,int r)
{
return dat[r]-dat[l]+min(dist(l),dist(r));
} double cal2(int l,int r)
{
return dat[r]-dat[l]+min(dist(l)+fabs(kx-dat[r]),dist(r)+fabs(kx-dat[l]));
} int main()
{
scanf("%d%d",&n,&k);
for(int i=;i<=n;i++) scanf("%lf",&dat[i]);
scanf("%lf%lf",&x,&y); kx=dat[k];sort(dat+,dat+n+); double res;
if(k==n+) res=cal1(,n);
else
{
res=cal2(,n);
for(int i=;i<=n;i++)
res=min(res,min(cal1(,i-)+cal2(i,n),cal2(,i-)+cal1(i,n)));
}
printf("%.10f",res);
return ;
}

[Codeforces 30D] Kings Problem的更多相关文章

  1. codeforces 340C Tourist Problem

    link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...

  2. codeforces B. Routine Problem 解题报告

    题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...

  3. Codeforces 527D Clique Problem

    http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...

  4. Codeforces 706C - Hard problem - [DP]

    题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...

  5. Codeforces 1096D - Easy Problem - [DP]

    题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...

  6. Codeforces 793C - Mice problem(几何)

    题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...

  7. CodeForces 687A NP-Hard Problem

    Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变 ...

  8. Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset

    Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...

  9. Codeforces Gym 100342C Problem C. Painting Cottages 转化题意

    Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...

随机推荐

  1. bzoj2002: [Hnoi2010]Bounce 弹飞绵羊 分块

    这个题体现了分块不只是最大值最小值众数次数,而是一种清真的思想. 我们把整个序列分块,在每个块里处理每个位置跳出这个块的次数和跳出的位置,那么每次修改n0.5,每次查询也是,那么O(m* n0.5)的 ...

  2. NAS星云链 入门之从零开发第一个DAPP

    应该有很多小伙伴和我一样,一直想去入手学习区块链,但是总无从下手,有些概念感觉理解了,有感觉没理解.其实这都是“没实践”的锅. 所谓看十遍不如想一遍,想一遍不如做一遍.这不最近星云链nebulas正有 ...

  3. spring中<bean>中parent标签的使用

    简介:spring 中parent标签是指:某个<bean>的父类.这个类可以覆盖parent的属性, 代码如下: Parent类的代码如下: package com.timo.domai ...

  4. region xx not deployed on any region server

    ERROR: Region { meta => month_hotstatic,860010-2288000000_201405_5_exit_00000047486,1400144486405 ...

  5. Windows Time Client

    Timezone: UTC Coordinated Universal Time ====Perform by Local / administrator must,configure Time se ...

  6. (转)Django常用命令

    转自GoodSpeed,http://www.cnblogs.com/cacique/archive/2012/09/30/2709145.html . . . . .

  7. jsp中的路径问题

    在学jsp的时候我每次都遇到路径的问题,每次都不能够说100%的把这搞定,这让我很烦恼,今天下午花了点时间来把路径问题整理了下. 一:首先我们在加载项目(我的项目名称是FinalExam)是的路径是h ...

  8. nginx重要配置项简要说明

    1.重要配置项 以下是一个完整的nginx配置信息. #================================以下是全局配置项 #指定运行nginx的用户和用户组,默认情况下该选项关闭(关闭 ...

  9. HTTP中的URL长度限制

    首先,其实http 1.1 协议中对url的长度是不受限制的,协议原文: The HTTP protocol does not place any a priori limit on the leng ...

  10. SCC模板

    vector<int> G[maxn]; int pre[maxn], low[maxn], c[maxn]; int n, m; stack<int> s; int dfst ...