【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 ...
随机推荐
- [AWS] Install the AWS cli
On Windows, just download the installer and install it. Configure: aws configure In your aws console ...
- htaccess 正则规则整理(转)
为了方便 htaccess 编写正则,这里整理了一下 htaccess 的正则规则. # —— 位于行首时表示注释. [F] —— Forbidden(禁止): 命令服务器返回 403 Forbidd ...
- 关于Linux的缓存内存 Cache Memory详解<转>
转自 http://www.ha97.com/4337.html PS:前天有童鞋问我,为啥我的Linux系统没运行多少程序,显示的可用内存这么少?其实Linux与Win的内存管理不同,会尽量缓存内存 ...
- 中国剩余定理模板poj1006
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #i ...
- ubuntu 查看端口被占用并处理
当启动程序出现端口号被占用的情况,需要查看端口使用情况,使用netstat命令,下面是常用的几个查看端口情况的命令:查看所有的服务端口(ESTABLISHED netstat -a查看所有的服务端口, ...
- Android客户端与服务端交互之登陆示例
Android客户端与服务端交互之登陆示例 今天了解了一下android客户端与服务端是怎样交互的,发现其实跟web有点类似吧,然后网上找了大神的登陆示例,是基于IntentService的 1.后台 ...
- codevs1099字串变换(Bfs)
/* 最少步数问题 妥妥的Bfs 很显然队列里存的是串(可能存个数也可以 就像8数码那样) 然后每次队首元素弄出来 能换的都换一遍 最后每次换完的新串入队前先判断到头了没 最后说一句 String大法 ...
- js页面加载事件
<body onload="myfunction()" > </body> <script type="text/javascript&qu ...
- 认识CSS样式
CSS全称为“层叠样式表 (Cascading Style Sheets)”,它主要是用于定义HTML内容在浏览器内的显示样式,如文字大小.颜色.字体加粗等. 如下列代码: p{ font-size: ...
- javascript基础学习(十一)
javascript之BOM 学习要点: BOM介绍 Window对象 一.BOM介绍 浏览器对象模型简称为BOM(Brower Object Model),BOM由很多对象构成,对象与对象之间有着相 ...