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

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

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

  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7. const double eps = 1e-8;
  8. const double r = 0.99; //降温速度
  9. const int dx[] = { 0, 0, 1, -1, 1, -1, 1, -1 };
  10. const int dy[] = { 1, -1, 0, 0, -1, 1, 1, -1 };
  11. double a, b, c, d, e, f;
  12.  
  13. double dis(double x, double y, double z) {
  14. return sqrt(x * x + y * y + z * z);
  15. }
  16.  
  17. //已知x,y,求z
  18. double getz(double x, double y) {
  19. double A = c, B = e * x + d * y,
  20. C = a * x * x + b * y * y + f * x * y - 1;
  21. double delta = B * B - 4 * A * C;
  22. if (delta < 0) return 1e60;
  23. double z1 = (-B + sqrt(delta)) / 2 / A,
  24. z2 = (-B - sqrt(delta)) / 2 / A;
  25. if (z1 * z1 < z2 * z2) return z1;
  26. else return z2;
  27. }
  28.  
  29. double solve() {
  30. //模拟退火
  31. double step = 1; //步长
  32. double x = 0, y = 0, z;
  33. while (step > eps) {
  34. z = getz(x, y);
  35. for (int i = 0; i < 8; i++) {
  36. double nx = x + dx[i] * step,
  37. ny = y + dy[i] * step,
  38. nz = getz(nx, ny);
  39. if (nz > 1e30) continue;
  40. if (dis(nx, ny, nz) < dis(x, y, z)) {
  41. x = nx; y = ny; z = nz;
  42. }
  43. }
  44. step *= r;
  45. }
  46. return dis(x, y, z);
  47. }
  48.  
  49. int main() {
  50. while (scanf("%lf%lf%lf%lf%lf%lf", &a, &b, &c, &d, &e, &f) != EOF) {
  51. printf("%.8f\n", solve());
  52. }
  53. return 0;
  54. }

三分要比退火快

  1. #include <cstdio>
  2. #include <cstdlib>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <string>
  6. #include <queue>
  7. #include <vector>
  8. #include <iostream>
  9. #include <algorithm>
  10. using namespace std;
  11. #define RD(x) scanf("%d",&x)
  12. #define RD2(x,y) scanf("%d%d",&x,&y)
  13. #define clr0(x) memset(x,0,sizeof(x))
  14. typedef long long LL;
  15. const double INF = 10000;
  16. const double eps = 1e-8;
  17. double solve(double a,double b,double c)
  18. {
  19. double delta = b*b-4.0*a*c;
  20. if(delta < eps)
  21. return INF;
  22. return (sqrt(delta)-b)/(a*2.0);
  23. }
  24. double a,b,c,d,e,f;
  25. double z(double x,double y)
  26. {
  27. double zz = solve(c,d*y+e*x,a*x*x+b*y*y+f*x*y-1);
  28. return x*x+y*y+zz*zz;
  29. }
  30. double y(double x)
  31. {
  32. double l = -INF,r = INF;
  33. int t = 200;
  34. while(l+eps<r){
  35. double mid = (l+r)/2,rr = (mid+r)/2;
  36. if(z(x,rr) < z(x,mid))
  37. l = mid;
  38. else
  39. r = rr;
  40. }
  41. return z(x,l);
  42. }
  43. double x()
  44. {
  45. double l = -INF,r = INF;
  46. int t = 200;
  47. while(l+eps<r){
  48. double mid = (l+r)/2,rr = (mid+r)/2;
  49. if(y(rr) < y(mid))
  50. l = mid;
  51. else
  52. r = rr;
  53. }
  54. return sqrt(y(l));
  55. }
  56. int main(){
  57. while(~scanf("%lf%lf%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f))
  58. printf("%.8lf\n",x());
  59. return 0;
  60. }

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. Access denied for user 'root'@'localhost' (using password:YES)解决方法

    Access denied for user 'root'@'localhost' (using password:YES)解决方法 在MySQL的使用过程中,我们可能会碰到“Access denie ...

  2. android热门消息推送横向测评![转]

    关于这个话题,已经不是什么新鲜事了.对于大多数中小型公司一般都是选择第三方的服务来实现.但是现在已经有很多提供推送服务的公司和产品,如何选择一个适合自己项目的服务呢?它们之间都有什么差别?在此为大家做 ...

  3. django通过url传递参数(编辑操作页面)

    在做到编辑部分时,想到的办法是在编辑上跳转到页面时给他一个包含唯一标识id的url,然后通过这个url中的id去查询出该条数据,将数据内容显示在编辑页面.   1.编辑按钮 <button on ...

  4. .net 委托的用法

    定义了两个委托 //Func有返回值:Action无返回值.两个委托 Func<int,int> f= a =>a+1;//参数,返回值: int reslut=f(5);//6

  5. cf-Round541-Div2-F(并查集+静态链表)

    题目链接:http://codeforces.com/contest/1131/problem/F 思路: 很容易看出这是一道并查集的题目,因为要输出每个cage中住的鸟的编号,故采用静态链表.用l[ ...

  6. 注册google账号 解决国内手机注册失败的问题

    1. PC端下载夜神安卓模拟器.安装,启动. 2. 在模拟器里的市场应用里下载qq邮箱. 3. 启动邮箱,选择gmail,注册.后续一切顺利. 这是迄今为止,唯一注册顺利的方法.其他方法,手机验证一关 ...

  7. .net的内置对象

    一 . 获取客户端,服务器端信息: Response.Write("客户端信息:"); Response.Write("<br>浏览器类型,版本:" ...

  8. UVa 1592 Database(巧用map)

    Peter studies the theory of relational databases. Table in the relational database consists of value ...

  9. 利用python计算windows全盘文件md5值的脚本

    import hashlib import os import time import configparser import uuid def test_file_md5(file_path): t ...

  10. SQL时间格式化 转载备用~

    Sel1 取值后格式化{0:d}小型:如2005-5-6{0:D}大型:如2005年5月6日{0:f}完整型 2 当前时间获取 DateTime.Now.ToShortDateString 3 取值中 ...