刚开始学随机算法,凸包+模拟退火。

 /* 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的更多相关文章

  1. 【BZOJ】2440: [中山市选2011]完全平方数

    [题意]T次询问第k小的非完全平方数倍数的数.T<=50,k<=10^9.(即无平方因子数——素因数指数皆为0或1的数) [算法]数论(莫比乌斯函数) [题解]考虑二分,转化为询问[1,x ...

  2. 【HDOJ】4729 An Easy Problem for Elfness

    其实是求树上的路径间的数据第K大的题目.果断主席树 + LCA.初始流量是这条路径上的最小值.若a<=b,显然直接为s->t建立pipe可以使流量最优:否则,对[0, 10**4]二分得到 ...

  3. 【HDOJ】【3506】Monkey Party

    DP/四边形不等式 裸题环形石子合并…… 拆环为链即可 //HDOJ 3506 #include<cmath> #include<vector> #include<cst ...

  4. 【HDOJ】【3516】Tree Construction

    DP/四边形不等式 这题跟石子合并有点像…… dp[i][j]为将第 i 个点开始的 j 个点合并的最小代价. 易知有 dp[i][j]=min{dp[i][j] , dp[i][k-i+1]+dp[ ...

  5. 【HDOJ】【3480】Division

    DP/四边形不等式 要求将一个可重集S分成M个子集,求子集的极差的平方和最小是多少…… 首先我们先将这N个数排序,容易想到每个自己都对应着这个有序数组中的一段……而不会是互相穿插着= =因为交换一下明 ...

  6. 【HDOJ】【2829】Lawrence

    DP/四边形不等式 做过POJ 1739 邮局那道题后就很容易写出动规方程: dp[i][j]=min{dp[i-1][k]+w[k+1][j]}(表示前 j 个点分成 i 块的最小代价) $w(l, ...

  7. 【HDOJ】【3415】Max Sum of Max-K-sub-sequence

    DP/单调队列优化 呃……环形链求最大k子段和. 首先拆环为链求前缀和…… 然后单调队列吧<_<,裸题没啥好说的…… WA:为毛手写队列就会挂,必须用STL的deque?(写挂自己弱……s ...

  8. 【HDOJ】【3530】Subsequence

    DP/单调队列优化 题解:http://www.cnblogs.com/yymore/archive/2011/06/22/2087553.html 引用: 首先我们要明确几件事情 1.假设我们现在知 ...

  9. 【HDOJ】【3068】最长回文

    Manacher算法 Manacher模板题…… //HDOJ 3068 #include<cstdio> #include<cstring> #include<cstd ...

随机推荐

  1. C\C++代码优化的27个建议

    1. 记住阿姆达尔定律: funccost是函数func运行时间百分比,funcspeedup是你优化函数的运行的系数. 所以,如果你优化了函数TriangleIntersect执行40%的运行时间, ...

  2. IO流--转载

    第 1 部分 从输出流中读取 http://www.ibm.com/developerworks/cn/java/j-io1/ 自早期基于浏览器的 applet 和简单应用程序以来,Java 平台已有 ...

  3. Android运行时注解

    Android的注解有编译时注解和运行时注解,本文就介绍下运行时注解. 其实非常简单,直接上代码:本文主要是替代传统的findViewById()的功能,就是在我们Activity中不需要再使用fin ...

  4. 【Python之旅】第六篇(七):开发简易主机批量管理工具

    [Python之旅]第六篇(七):开发简易主机批量管理工具 python 软件开发 Paramiko模块 批量主机管理 摘要: 通过前面对Paramiko模块的学习与使用,以及Python中多线程与多 ...

  5. 开始学习编程了…… 2015年九月七日 …… 31岁的Me.

    给自己下的命令:做今天开始认认真真地开始学习编程,一年后的今天一定要找到一份编程的工作. 为什么要学编程?:因为不想回以前的圈子,“创业”快三年什么都给“创”没了,咳……,不过呢,倒是领略到编程能带来 ...

  6. Asp.net Core 部署到Azure.cn的一个小问题

    前一段尝试在azure.cn上部署Aps.net Core未成功,报503错误!在网上查到是Azure.cn的问题,未能完美支持Asp.net Core! Asp.net Core发表正式版了,又尝试 ...

  7. c-连接两个链表

    概述 还是相对简单,不过要记得释放不用的头结点即可. 代码为: //将lList2头结点连接在lList1尾结点的后面. void combine(linklist lList1, linklist ...

  8. iOS开发知识点:理解assign,copy,retain变strong

    一..h和.m文件的变化说明 1.对于.h头文件,主要是将属性定义由retain变为strong @property (retain, nonatomic) 变为 @property (strong, ...

  9. ANSI escape code

    最近在做iOS上的SSH终端项目,主要是在手机上远程连接Unix系统,并进行一些简单的指令操作,类似于SecureCRT:今天想总结一下这个项目中遇到的新东西----ANSI escape code. ...

  10. 数据类型的转换String

    x.toString(): 无法转换null和undefined 不过String()却是万能的,其中的原理如下 function String(x){ if(x===undefined){ retu ...