http://acm.hdu.edu.cn/showproblem.php?pid=5017

求椭圆上离圆心最近的点的距离。

模拟退火和三分套三分都能解决

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath> using namespace std;
const double eps = 1e-8;
const double r = 0.99; //降温速度
const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
double a, b, c, d, e, f; double dis(double x, double y, double z) {
return sqrt(x * x + y * y + z * z);
} //已知x,y,求z
double getz(double x, double y) {
double A = c, B = e * x + d * y,
C = a * x * x + b * y * y + f * x * y - 1;
double delta = B * B - 4 * A * C;
if (delta < 0) return 1e60;
double z1 = (-B + sqrt(delta)) / 2 / A,
z2 = (-B - sqrt(delta)) / 2 / A;
if (z1 * z1 < z2 * z2) return z1;
else return z2;
} double solve() {
//模拟退火
double step = 1; //步长
double x = 0, y = 0, z;
while (step > eps) {
z = getz(x, y);
for (int i = 0; i < 8; i++) {
double nx = x + dx[i] * step,
ny = y + dy[i] * step,
nz = getz(nx, ny);
if (nz > 1e30) continue;
if (dis(nx, ny, nz) < dis(x, y, z)) {
x = nx; y = ny; z = nz;
}
}
step *= r;
}
return dis(x, y, z);
} int main() {
while (scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f) != EOF) {
printf("%.8f\n", solve());
}
return 0;
}

三分要比退火快

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const double INF = 10000;
const double eps = 1e-8;
double solve(double a,double b,double c)
{
double delta = b*b-4.0*a*c;
if(delta < eps)
return INF;
return (sqrt(delta)-b)/(a*2.0);
}
double a,b,c,d,e,f;
double z(double x,double y)
{
double zz = solve(c,d*y+e*x,a*x*x+b*y*y+f*x*y-1);
return x*x+y*y+zz*zz;
}
double y(double x)
{
double l = -INF,r = INF;
int t = 200;
while(l+eps<r){
double mid = (l+r)/2,rr = (mid+r)/2;
if(z(x,rr) < z(x,mid))
l = mid;
else
r = rr;
}
return z(x,l);
}
double x()
{
double l = -INF,r = INF;
int t = 200;
while(l+eps<r){
double mid = (l+r)/2,rr = (mid+r)/2;
if(y(rr) < y(mid))
l = mid;
else
r = rr;
}
return sqrt(y(l));
}
int main(){
while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f))
printf("%.8lf\n",x());
return 0;
}

hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离的更多相关文章

  1. hdu 5017 模拟退火算法

    hdu 5017 http://blog.csdn.net/mypsq/article/details/39340601 #include <cstdio> #include <cs ...

  2. hdu 5017 模拟退火

    题意:给出椭球面的立体解析式,要求椭球面上距离原点最近的点的距离 sol:这题要想推公式就

  3. HDU - 5017 Ellipsoid(模拟退火法)

    Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance bet ...

  4. HDU 6362(求椭圆中矩形周长的期望 数学)

    题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...

  5. HDU - 5017 Ellipsoid(模拟退火)

    题意 给一个三维椭球面,求球面上距离原点最近的点.输出这个距离. 题解 模拟退火. 把\(z = f(x, y)\)函数写出来,这样通过随机抖动\(x\)和\(y\)坐标就能求出\(z\). 代码 / ...

  6. HDU 4355——Party All the Time——————【三分求最小和】

    Party All the Time Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  7. HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...

  8. HLJU 1221: 高考签到题 (三分求极值)

    1221: 高考签到题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 9  Solved: 4 [Submit][id=1221">St ...

  9. BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】

    ial Judge Prev Submit Status Statistics Discuss Next Type: None   None   Graph Theory       2-SAT   ...

随机推荐

  1. Numpy 基础知识

    1.使用ipython --pylab 自动加载 Numpy.Scipy.Matplotlib模块. 创建数组a = arange(10) b = arange(10,dtype='f')c = ar ...

  2. HTML CSS + DIV实现整体布局 part1

    HTML CSS + DIV实现整体布局 1.技术目标: 开发符合W3C标准的Web页面 理解盒子模型 实现DIV+CSS整体布局 2.什么是W3C标准? W3C:World Wide Web Con ...

  3. sql重复数据只取一条记录

    1.SQL SELECT DISTINCT 语句 在表中,可能会包含重复值.这并不成问题,不过,仅仅列出不同(distinct)的值. 关键词 DISTINCT 用于返回唯一不同的值. 语法: SEL ...

  4. PIE结对编程

    学习进度条 点滴成就 学习时间 新编写代码行数 博客量 学到知识点 第一周 8 0 0 了解软件工程 第二周 7 0 1 了解软件工程 第三周 11 0 1 用例图 第四周 6 25 0 结对编程 第 ...

  5. Oracle CHAR,VARCHAR,VARCHAR2,nvarchar类型的区别与使用(转载)

    一 varchar,varchar2,nvarchar,nvarchar2 四个类型都属于变长字符类型, varchar和varchar2的区别在与后者把所有字符都占两字节,前者只对汉字和全角等字符占 ...

  6. java并发:volatile关键字

    java并发需要保证原子性,可见性,有序性. http://www.cnblogs.com/expiator/p/9226775.html 一.volatile关键字作用如下: 1.volatile关 ...

  7. cf-Round542-Div2-B(贪心)

    题目链接:http://codeforces.com/contest/1130/problem/B 思路: 贪心题.定义结构体数组a,a[i].x[0],a[i].x[1]分别表示i出现的第一个下标和 ...

  8. 安装完CentOS可以不做的事

    添加用户到sudo. 打开/etc/sudoers 找到root ALL=(ALL) ALL这一行,在后面再加上一行就可以了(不用引号): username ALL=(ALL) ALL 注意,都用ta ...

  9. mysql基本的增删改查和条件语句

    增 insert into 表名(列名,列名......) values("test1",23),("test2",23),("test3" ...

  10. mysql数据库的最基本的命令

    #查看mysql有哪些数据库:show databases; 创建一个数据库名称为DataBaseName,字符编码为utf8支持中文create database DataBaseName char ...