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. div 自动全屏高度

    最近做一个页面,需要一个div自动铺满全屏,但是高度总是难以搞定.查资料为:需要从html body到div 需要 设置 高度属性 为100%

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

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

  3. jquery parents用法

    之前一直用find 现在用parents var w = $("div"); w = $("div").parents('.class'); //在Parent ...

  4. WebDriverException:Element is not clickable at point - selenium执行过程中遇到的相关报错

    Element is not clickable at point (x, y) 这段可以忽略:本文来自 https://www.cnblogs.com/lozz/p/9947430.html 引起这 ...

  5. where T:new() 是什么意思

    经常看到方法后面加where T:new() ,下面来解释下 比如如下这个方法   protected static T CreateNewInstance<T>() where T :  ...

  6. thymeleaf 拼接字符串与变量

    参考https://www.thymeleaf.org/doc/tutorials/3.0/usingthymeleaf.html <span th:text="'The name o ...

  7. VS Access DataSet 插入

    在使用vs2008+access数据库,然后又使用了数据集,这时候插入操作遇到了问题,各种乱七八糟.各种头疼的问题就不说了,现在说找到的解决方法: 在xsd文件中插入TableAdapter后,会自动 ...

  8. 坑爹的HP

    昨天晚上帮人远程修理电脑,情况是这样的: HP CQ45笔记本, 比较老的机器, win32 xp sp3 系统, 突然发现没有声音了,而且右下角也没有出现小喇叭图标. 处理过程: 1.先查看了控制面 ...

  9. springMVC将处理的后的数据通过post方法传给页面时,可能会出现乱码问题,下面提出解决post乱码问题的方法

    在web.xml中加入: <!-- 解决post乱码问题 --> <filter> <filter-name>CharacterEncodingFilter< ...

  10. winform弹出文件和目录选择框

    目录选择: FolderBrowserDialog dialog = new FolderBrowserDialog(); dialog.Description = "请选择文件路径&quo ...