http://codeforces.com/gym/101246/problem/J

题意:给出n个点坐标,要使这些点间距相同的话,就要移动这些点,问最少的需要的移动距离是多少,并输出移动后的坐标。

思路:昨晚比赛确定的一点就是通过X分搜索去枚举间距,使得最后的移动距离最短。

不过使用的是二分,而且写的时候也只枚举了起点和终点,后面想了要枚举所有的点,但时间来不及。

因为间距的单调不会使得答案单调,间距过大过小都会使得最后的移动距离不是最优,所以是使用三分而不是二分,顺便重温一下三分。

  1. while(fabs(r - l) > eps) {
  2. double mid = (l + r) / 2.0;
  3. double midd = (mid + r) / 2.0;
  4. if(cal(mid, ) > cal(midd, )) l = mid;
  5. else r = midd;
  6. }
  7. double ans = cal(l, ) > cal(r, ) ? r : l;

对于每次三分,每次在n个点里面假设一个点不动,然后枚举完算一遍,取最优。

而且据说挺卡精度的,直接开了1e-12.

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define N 405
  4. const double eps = 1e-;
  5. double num[N];
  6. int n, id;
  7.  
  8. double cal(double x, int flag) {
  9. double res = , ans = ;
  10. for(int st = ; st <= n; st++) {
  11. res = ;
  12. for(int i = ; i < st; i++)
  13. res += fabs(num[st] - (st - i) * x - num[i]);
  14. for(int i = st + ; i <= n; i++)
  15. res += fabs(num[st] + (i - st) * x - num[i]);
  16. if(ans > res) ans = res, id = st;
  17. }
  18. return ans;
  19. }
  20.  
  21. void solve() {
  22. scanf("%d", &n);
  23. for(int i = ; i <= n; i++) scanf("%lf", &num[i]);
  24. double l = , r = ;
  25. while(fabs(r - l) > eps) {
  26. double mid = (l + r) / 2.0;
  27. double midd = (mid + r) / 2.0;
  28. if(cal(mid, ) > cal(midd, )) l = mid;
  29. else r = midd;
  30. }
  31. double ans = cal(l, ) > cal(r, ) ? r : l;
  32. double res = cal(ans, );
  33. printf("%.4f\n", res);
  34. for(int i = ; i < id; i++)
  35. printf("%.10f ", num[id] - (id - i) * ans);
  36. printf("%.10f ", num[id]);
  37. for(int i = id + ; i <= n; i++)
  38. printf("%.10f ", num[id] + (i - id) * ans);
  39. }
  40.  
  41. int main() {
  42. freopen("input.txt", "r", stdin);
  43. freopen("output.txt", "w", stdout);
  44. solve();
  45. return ;
  46. }

Codeforces Gym101246J:Buoys(三分搜索)的更多相关文章

  1. Weakness and Poorness CodeForces - 578C 三分搜索 (精度!)

    You are given a sequence of n integers a1, a2, ..., an. Determine a real number x such that the weak ...

  2. hdu 4355 Party All the Time(三分搜索)

    Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toget ...

  3. 三分搜索-ZOJ LightBulb

    开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...

  4. Codeforces 782B:The Meeting Place Cannot Be Changed(三分搜索)

    http://codeforces.com/contest/782/problem/B 题意:有n个人,每个人有一个位置和速度,现在要让这n个人都走到同一个位置,问最少需要的时间是多少. 思路:看上去 ...

  5. Codeforces 799D Field expansion - 搜索 - 贪心

    In one of the games Arkady is fond of the game process happens on a rectangular field. In the game p ...

  6. Codeforces Round #427 (Div. 2) Problem D Palindromic characteristics (Codeforces 835D) - 记忆化搜索

    Palindromic characteristics of string s with length |s| is a sequence of |s| integers, where k-th nu ...

  7. Codeforces C. NP-Hard Problem 搜索

    C. NP-Hard Problem time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  8. Codeforces 939E Maximize! (三分 || 尺取)

    <题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...

  9. CodeForces - 1059D——二分/三分

    题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...

随机推荐

  1. WPF 控件的内容属性

    WPF的内容属性不应定都是content, 例如TextBlock的内容属性是Text Panel的内容属性是Children ListBox的内容属性是Items

  2. WPF 自定义范围分组

    <Window x:Class="ViewExam.MainWindow"        xmlns="http://schemas.microsoft.com/w ...

  3. 利用WIX制作安装包(1)

    原文 利用WIX制作安装包(1) #installation 下载最新版本的WIX toolset 并安装DOWNLOAD Wix toolset V3.5之后的版本已经可以集成到visual stu ...

  4. rdlc水晶报表在wpf里的使用

    1引用程序集 Microsoft.ReportViewer.WinForms 2 xaml 命名空间 xmlns:rv="clr-namespace:Microsoft.Reporting. ...

  5. 图像滤镜艺术---Swirl滤镜

    原文:图像滤镜艺术---Swirl滤镜 Swirl Filter Swirl 滤镜是实现图像围绕中心点(cenX,cenY)扭曲旋转的效果,效果图如下: 原图 效果图 代码如下:         // ...

  6. 微信小程序把玩(十二)text组件

    原文:微信小程序把玩(十二)text组件 通常文本设置要不在wxml中设置,再要不就是通过weml绑定在js中设置文字. wxml <view > <text>我是文本组件&l ...

  7. Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio

    原文:Android零基础入门第10节:开发IDE大升级,终于迎来了Android Studio 通过前面几期的学习,我们知道了Android的前世今生,也了解了Android的系统架构和应用组件,也 ...

  8. C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装

    原文:C#高性能大容量SOCKET并发(二):SocketAsyncEventArgs封装 1.SocketAsyncEventArgs介绍 SocketAsyncEventArgs是微软提供的高性能 ...

  9. Android零基础入门第62节:搜索框组件SearchView

    原文:Android零基础入门第62节:搜索框组件SearchView 一.SearchView概述 SearchView是搜索框组件,它可以让用户在文本框内输入文字,并允许通过监听器监控用户输入,当 ...

  10. 为javasript中对象与json串之间转换的总结。

    jQuery.json 是 jQuery 的一个插件,可轻松实现对象和 JSON 字符串之间的转换.可序列化 JavaScript 对象.数值.字符串和数组到 JSON 字符串,同时可转换 JSON ...