[Codeforces 30D] Kings Problem
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的更多相关文章
- codeforces 340C Tourist Problem
link:http://codeforces.com/problemset/problem/340/C 开始一点也没思路,赛后看别人写的代码那么短,可是不知道怎么推出来的啊! 后来明白了. 首先考虑第 ...
- codeforces B. Routine Problem 解题报告
题目链接:http://codeforces.com/problemset/problem/337/B 看到这个题目,觉得特别有意思,因为有熟悉的图片(看过的一部电影).接着让我很意外的是,在纸上比划 ...
- Codeforces 527D Clique Problem
http://codeforces.com/problemset/problem/527/D 题意:给出一些点的xi和wi,当|xi−xj|≥wi+wj的时候,两点间存在一条边,找出一个最大的集合,集 ...
- Codeforces 706C - Hard problem - [DP]
题目链接:https://codeforces.com/problemset/problem/706/C 题意: 给出 $n$ 个字符串,对于第 $i$ 个字符串,你可以选择花费 $c_i$ 来将它整 ...
- Codeforces 1096D - Easy Problem - [DP]
题目链接:http://codeforces.com/problemset/problem/1096/D 题意: 给出一个小写字母组成的字符串,如果该字符串的某个子序列为 $hard$,就代表这个字符 ...
- Codeforces 793C - Mice problem(几何)
题目链接:http://codeforces.com/problemset/problem/793/C 题目大意:给你一个捕鼠器坐标,和各个老鼠的的坐标以及相应坐标的移动速度,问你是否存在一个时间点可 ...
- CodeForces 687A NP-Hard Problem
Portal:http://codeforces.com/problemset/problem/687/A 二分图染色 好模板题 有SPJ 值得注意的是,因为C++的奇妙的运算机制 若在vector变 ...
- Codeforces Gym 100342J Problem J. Triatrip 求三元环的数量 bitset
Problem J. Triatrip Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100342/at ...
- Codeforces Gym 100342C Problem C. Painting Cottages 转化题意
Problem C. Painting CottagesTime Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/10 ...
随机推荐
- 【NOIP模拟赛】超级树 DP
这个题我在考试的时候把所有的转移都想全了就是新加一个点时有I.不作为II.自己呆着III.连一个IV.连接两个子树中的两个V连接一个子树中的两个,然而V我并不会转移........ 这个题的正解体现了 ...
- ionic运行测试
http://blog.csdn.net/yucihan/article/details/54631747
- docker compose,link,Odoo
1.报错: /usr/bin/docker-current: Error response from daemon: driver failed programming external connec ...
- hibernate连接mysql,自动建表失败
hibernate的列名使用了mysql的关键字.
- Sync Data to AWS S3 on Windows Box
1. Install AWS CLI first, windows download link https://s3.amazonaws.com/aws-cli/AWSCLI64.msi 2. The ...
- O(n^2)以及O(nlogn)时间复杂度的排序算法
O(n^2)的算法 都是做的升序. 简单选择排序 思路:每次选择还未排序的区间的最小值和未排序区间的第一个值交换. function selectSort(arr){ for(let i = 0; i ...
- 转载:Apache commons开源工具简介
Apache Commons是一个非常有用的工具包,解决各种实际的通用问题,下面是一个简述表,详细信息访问http://jakarta.apache.org/commons/index.html Be ...
- centos7装机时更改网卡名为eth0操作
- EL表达式中获取list长度(JSTL函数用法)
在jsp页面中不能通过${list.size}取列表长度,而是 <%@ taglib uri="http://java.sun.com/jsp/jstl/core" pref ...
- python3 内置函数(转)
http://www.runoob.com/python/python-built-in-functions.html divmod(7,2) # 返回(3,1)商和余的元组 frozenset() ...