【HDOJ】2440 Watch out the Animal
刚开始学随机算法,凸包+模拟退火。
/* 2440 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std; #define MAXN 105 typedef struct {
int x, y;
} Point_t; Point_t stack[MAXN];
Point_t points[MAXN];
int dir[][] = {
{-, }, {, }, {, -}, {, },
{-, -}, {-, }, {, -}, {, }
};
const double eps = 1e-;
const double next = 0.9;
int n, top;
int ans; double Length(Point_t a, Point_t b) {
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) + .);
} int Length2(Point_t a, Point_t b) {
return (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y);
} int cross(Point_t a, Point_t b, Point_t c) {
return (b.x-a.x)*(c.y-a.y) - (c.x-a.x)*(b.y-a.y);
} bool comp(Point_t a, Point_t b) {
int m = cross(points[], a, b);
return m> || (m== && Length2(points[], a)<Length2(points[], b));
} void Graham() {
int i, j, k;
Point_t p; p = points[];
k = ;
for (i=; i<n; ++i) {
if (points[i].x<p.x || (points[i].x==p.x && points[i].y<p.y)) {
k = i;
p = points[i];
}
}
points[k] = points[];
points[] = p; sort(points+, points+n, comp);
stack[] = points[];
stack[] = points[];
top = ; for (i=; i<n; ++i) {
while (top> && cross(stack[top-], stack[top], points[i])<=)
--top;
stack[++top] = points[i];
}
} double cal(double xx, double yy) {
double ret = ;
double x, y; for (int i=; i<=top; ++i) {
x = xx - stack[i].x;
y = yy - stack[i].y;
ret += sqrt(x*x + y*y);
} return ret;
} void SA() {
double step = .;
double dis, tmp;
double x, y;
double xx, yy;
int i; x = stack[].x;
y = stack[].y;
dis = cal(x, y);
while (step > eps) {
for (i=; i<; ++i) {
xx = x + step * dir[i][];
yy = y + step * dir[i][];
tmp = cal(xx, yy);
if (tmp < dis) {
dis = tmp;
x = xx;
y = yy;
}
}
step *= next;
} ans = (dis+0.5);
} int main() {
int t;
int i, j, k; #ifndef ONLINE_JUDGE
freopen("data.in", "r", stdin);
//freopen("data.out", "w", stdout);
#endif scanf("%d", &t);
while (t--) {
scanf("%d", &n);
for (i=; i<n; ++i)
scanf("%d %d", &points[i].x, &points[i].y);
Graham();
SA();
printf("%d\n", ans);
if (t)
printf("\n");
} return ;
}
【HDOJ】2440 Watch out the Animal的更多相关文章
- 【BZOJ】2440: [中山市选2011]完全平方数
[题意]T次询问第k小的非完全平方数倍数的数.T<=50,k<=10^9.(即无平方因子数——素因数指数皆为0或1的数) [算法]数论(莫比乌斯函数) [题解]考虑二分,转化为询问[1,x ...
- 【HDOJ】4729 An Easy Problem for Elfness
其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...
- 【HDOJ】【3506】Monkey Party
DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...
- 【HDOJ】【3516】Tree Construction
DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...
- 【HDOJ】【3480】Division
DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...
- 【HDOJ】【2829】Lawrence
DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...
- 【HDOJ】【3415】Max Sum of Max-K-sub-sequence
DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...
- 【HDOJ】【3530】Subsequence
DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...
- 【HDOJ】【3068】最长回文
Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...
随机推荐
- VMware于CentOS网络设置
VMware于CentOS网络设置 底: 笔记本电脑有两块网卡: 1. 网卡连接公司内网,仅仅配置了内网ip和子网掩码. 2. 无线网卡.连接4g无线路由器.dhcp自己主动配置. 问题: 在VMwa ...
- vim note(4)
:new 文件名.词尾 新文件. :e 文件名 打开文件. :w 文件名.txt 保存文件. :wq 保存并退出. :x 辍学.假设文件更改被保存 版权声明:本文博主原创文章,博客,未经同意不得转载.
- Java中数组的初始化方式
Java中数组的初始化方式 初始化方式有两种: 1.静态初始化:初始化时由程序猿显式指定每一个数组元素的初始值,由系统指定数组长度 2.动态初始化:初始化时由程序猿仅仅指定数组长度,由系统为数组 ...
- C#学习第四天
今天主要学习了结构方面的知识,首先是定义,代码: struct<typeName> { <memberDeclarations> } struct route { public ...
- animation的6个属性
@keyframes 规定动画. animation 所有动画属性的简写属性,除了 animation-play-state 属性. animation-name 规定 @keyframes ...
- PC端手机访问跳转手机站点
第一种: var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.sr ...
- jsonp Ajax跨域请求
什么是JSONP? JSONP(JSON with Padding)是一个非官方的协议,它允许在服务器端集成Script tags返回至客户端,通过javascript callback的形式实现跨域 ...
- ASP.NET实现文件下载
转:http://blog.csdn.net/codeshark/article/details/2473664 方式一:TransmitFile实现下载.将指定的文件直接写入 HTTP 响应输出流, ...
- [转] 详细整理:UITableView优化技巧
原文:http://www.cocoachina.com/ios/20150602/11968.html 最近在微博上看到一个很好的开源项目VVeboTableViewDemo,是关于如何优化 ...
- /root/.bashrc与/etc/profile的异同
要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是loginshell 和non-loginshell. 交互式模式就是shell等待你的输入,并 ...