hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离
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 模拟退火/三分求椭圆上离圆心最近的点的距离的更多相关文章
- hdu 5017 模拟退火算法
hdu 5017 http://blog.csdn.net/mypsq/article/details/39340601 #include <cstdio> #include <cs ...
- hdu 5017 模拟退火
题意:给出椭球面的立体解析式,要求椭球面上距离原点最近的点的距离 sol:这题要想推公式就
- HDU - 5017 Ellipsoid(模拟退火法)
Problem Description Given a 3-dimension ellipsoid(椭球面) your task is to find the minimal distance bet ...
- HDU 6362(求椭圆中矩形周长的期望 数学)
题意是给定一个椭圆标准方程的a,b(椭圆的长半轴长和短半轴长),在[0,b]内取一个数,则过点(0,b)且平行于x轴的直线与椭圆交于两点,再将此两点关于x轴做对称点,顺次连接此四点构成矩形,求出这些矩 ...
- HDU - 5017 Ellipsoid(模拟退火)
题意 给一个三维椭球面,求球面上距离原点最近的点.输出这个距离. 题解 模拟退火. 把\(z = f(x, y)\)函数写出来,这样通过随机抖动\(x\)和\(y\)坐标就能求出\(z\). 代码 / ...
- HDU 4355——Party All the Time——————【三分求最小和】
Party All the Time Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- HDU 4311 Meeting point-1 求一个点到其它点的曼哈顿距离之和
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4311 解题报告:在一个平面上有 n 个点,求一个点到其它的 n 个点的距离之和最小是多少. 首先不得不 ...
- HLJU 1221: 高考签到题 (三分求极值)
1221: 高考签到题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 9 Solved: 4 [Submit][id=1221">St ...
- BNU 4260 ——Trick or Treat——————【三分求抛物线顶点】
ial Judge Prev Submit Status Statistics Discuss Next Type: None None Graph Theory 2-SAT ...
随机推荐
- HTML CSS + DIV实现整体布局 part2
9.盒模型的层次关系 我们通过一个经典的盒模型3D立体结构图来理解,如图: 从上往下看,层次关系如下: 第1层:盒子的边框(border), 第2层:元素的内容(content).内边 ...
- 兼容IE7、IE8、IE9的input type="number"插件
IE11版本好像才兼容input type="number",但是现在Win7版本操作系统下,很多人的IE版本都是IE7/8/9,所以为了体验就自己写了一个小插件,支持设置最大值. ...
- cin中函数的作用
cin是istream类的对象,它是从标准输入设备(键盘)获取数据,程序中的变量通过流提取符">>"从流中提取数据.流提取符">>"从流 ...
- tf.unstack()、tf.stack()
tf.unstack 原型: unstack( value, num=None, axis=0, name='unstack' ) 官方解释:https://tensorflow.google.cn/ ...
- Linux配置Hadoop 常用的命令
uname -a 看主机位数 ip a 看IP地址 vi /etc/sysconfig/network 改主机的名字 vi /etc/hosts 改映射关系 vi /etc/sysconfig/net ...
- jquery 动态添加的代码不能触发绑定事件
今天发现jQuery对动态添加的元素不触发事件,比如blur.click事件等 参考文章证明了我的结论,并给出了原因及解决方案 原因:程序找不到动态添加的节点. 解决方案:在绑定父元素后的子元素 $( ...
- 简单解决 Javascrip 浮点数计算的 Bug(.toFixed(int 小数位数))
众所周知,Javascript 在进行浮点数运算时,结果会非预期地出现一大长串小数. 解决: 如果变量 result 是计算结果,则在返回时这样写,return result.toFixed(2): ...
- 五个步骤搞定敏捷UX设计
互联网产品发展的速度越来越快,人们对于产品的要求也在不断的升级,这直接地导致了用户体验设计的重要性不断提升.与此同时,过去的流程冗长的设计开发模式已经不能够满足快速迭代的需要.<敏捷宣言> ...
- mybatis 和 mybatis-spring
一. 1. 实体bean package com.mybatisBean; public class User { private Integer id; private String name; ...
- 关闭文件描述符-close
头文件:#include<unistd.h> 原型:int close(int fd); 返回值:成功返回0,失败返回-1.