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. RabbitMQ 高可用集群搭建

    面向EDA(事件驱动架构)的方式来设计你的消息 AMQP routing key的设计 RabbitMQ cluster搭建 Mirror queue policy设置 两个不错的RabbitMQ p ...

  2. thymeleaf 拼接字符串与变量

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

  3. 刚刚明白了for循环写三角形

    for(int a = 15; a >=1; a--) { for(int b = a - 1; b >=0; b--) { System.out.print("A") ...

  4. Java的线程同步

    synchronized获取的锁是对象,而不是函数或语句块. 项目结构 资源类 import java.util.concurrent.TimeUnit; public class myResourc ...

  5. oracle 使用正则表达式获取字符串中包含的数字

    select REGEXP_REPLACE('字符串中包含的数字123,提取后就是123', '[^0-9]', '') from dual;

  6. iOS - OC - JSON 解析 - NSJSONSerialization

    #import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...

  7. Canvas游戏计算机图形教程

    TechbrooD   主站 WOW 登录   注册 0首页 1简介 1.1WWW 技术变迁和生态 1.2WWW 学习建议 1.3WWW 互联网基础知识 1.4WWW Web 1.5 WWW Web ...

  8. db2 统计信息 runstats

    1.runstats的语法:runstats on table [模式名].[表名] with distribution and detailed indexes all注意:你可以在所有列上,或者仅 ...

  9. hive 报错 java.lang.RuntimeException: Unable to instantiate org.apache.hadoop.hive.metastore.HiveMetaStoreClient

    Exception in thread "main" java.lang.RuntimeException: java.lang.RuntimeException: Unable ...

  10. eclipse 远程调试mapreduce

    使用环境:centos6.5+eclipse(4.4.2)+hadoop2.7.0 1.下载eclipse hadoop 插件  hadoop-eclipse-plugin-2.7.0.jar 粘贴到 ...