Codeforces Gym101246J:Buoys(三分搜索)
http://codeforces.com/gym/101246/problem/J
题意:给出n个点坐标,要使这些点间距相同的话,就要移动这些点,问最少的需要的移动距离是多少,并输出移动后的坐标。
思路:昨晚比赛确定的一点就是通过X分搜索去枚举间距,使得最后的移动距离最短。
不过使用的是二分,而且写的时候也只枚举了起点和终点,后面想了要枚举所有的点,但时间来不及。
因为间距的单调不会使得答案单调,间距过大过小都会使得最后的移动距离不是最优,所以是使用三分而不是二分,顺便重温一下三分。
while(fabs(r - l) > eps) {
double mid = (l + r) / 2.0;
double midd = (mid + r) / 2.0;
if(cal(mid, ) > cal(midd, )) l = mid;
else r = midd;
}
double ans = cal(l, ) > cal(r, ) ? r : l;
对于每次三分,每次在n个点里面假设一个点不动,然后枚举完算一遍,取最优。
而且据说挺卡精度的,直接开了1e-12.
#include <bits/stdc++.h>
using namespace std;
#define N 405
const double eps = 1e-;
double num[N];
int n, id; double cal(double x, int flag) {
double res = , ans = ;
for(int st = ; st <= n; st++) {
res = ;
for(int i = ; i < st; i++)
res += fabs(num[st] - (st - i) * x - num[i]);
for(int i = st + ; i <= n; i++)
res += fabs(num[st] + (i - st) * x - num[i]);
if(ans > res) ans = res, id = st;
}
return ans;
} void solve() {
scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%lf", &num[i]);
double l = , r = ;
while(fabs(r - l) > eps) {
double mid = (l + r) / 2.0;
double midd = (mid + r) / 2.0;
if(cal(mid, ) > cal(midd, )) l = mid;
else r = midd;
}
double ans = cal(l, ) > cal(r, ) ? r : l;
double res = cal(ans, );
printf("%.4f\n", res);
for(int i = ; i < id; i++)
printf("%.10f ", num[id] - (id - i) * ans);
printf("%.10f ", num[id]);
for(int i = id + ; i <= n; i++)
printf("%.10f ", num[id] + (i - id) * ans);
} int main() {
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
solve();
return ;
}
Codeforces Gym101246J:Buoys(三分搜索)的更多相关文章
- 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 ...
- hdu 4355 Party All the Time(三分搜索)
Problem Description In the Dark forest, there is a Fairy kingdom where all the spirits will go toget ...
- 三分搜索-ZOJ LightBulb
开始算法基础学习的第一天 今天学习的内容是三分搜索 相对来说很基础的内容(还是觉得脑子不够用) 三分搜索主要用于凸函数查找极大值. (盗个图) 如图所示 若要查找该函数的最大值 可以考虑和二分法一样的 ...
- Codeforces 782B:The Meeting Place Cannot Be Changed(三分搜索)
http://codeforces.com/contest/782/problem/B 题意:有n个人,每个人有一个位置和速度,现在要让这n个人都走到同一个位置,问最少需要的时间是多少. 思路:看上去 ...
- 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 ...
- 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 ...
- Codeforces C. NP-Hard Problem 搜索
C. NP-Hard Problem time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...
- Codeforces 939E Maximize! (三分 || 尺取)
<题目链接> 题目大意:给定一段序列,每次进行两次操作,输入1 x代表插入x元素(x元素一定大于等于之前的所有元素),或者输入2,表示输出这个序列的任意子集$s$,使得$max(s)-me ...
- CodeForces - 1059D——二分/三分
题目 题目链接 简单的说,就是作一个圆包含所有的点且与x轴相切,求圆的最小半径 方法一 分析:求最小,对半径而言肯定满足单调性,很容易想到二分.我们二分半径,然后由于固定了与X轴相切,我们对于每一个点 ...
随机推荐
- Highcharts纯js图表库,以后可以跟客户说,你跟阿里云ECS用的图表库是同款
Highcharts是一款纯javascript编写的图表库,能够很简便的在Web网站或Web应用中添加交互性的图表,Highcharts目前支持直线图.曲线图.面积图.柱状图.饼图.散点图等多达18 ...
- maven_默认新建项目jdk1.6_默认配置
<?xml version="1.0" encoding="UTF-8"?> <settings xmlns="http://mav ...
- [LeetCode OJ] Copy List with Random Pointer 扩大
职务地址:https://oj.leetcode.com/problems/copy-list-with-random-pointer/ 题意:对一个有回路的链表的深复制 解题:这道题我AC了之后才发 ...
- CheckBox IsHitTestVisible
<Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto"></Colu ...
- 将RDL报表转换成RDLC报表的函数
原文:将RDL报表转换成RDLC报表的函数 近日研究RDLC报表,发现其不能与RDL报表兼容,尤其是将RDL报表转换成RDLC报表.网上的资料贴出的的转换方式复杂且不切实际,遂决定深入研究.经研究发现 ...
- Win8 Metro(C#)数字图像处理--2.50图像运动模糊
原文:Win8 Metro(C#)数字图像处理--2.50图像运动模糊 [函数名称] 图像运动模糊算法 MotionblurProcess(WriteableBitmap src,int ...
- C语言实现的CRC16/CCITT-FALSE校验码函数
要求:输入字符串“00 AA FF CC AA 01 00” 得到校验码“79B1” 方法1: // ConsoleApplication1.cpp: 定义控制台应用程序的入口点. // #inclu ...
- SqlServer批量压缩数据库日志-多数据库批量作业,批量备份还原
原文:SqlServer批量压缩数据库日志-多数据库批量作业,批量备份还原 --作业定时压缩脚本 多库批量操作 DECLARE @DatabaseName NVARCHAR(50) DECLARE @ ...
- Android零基础入门第88节:Fragment显示和隐藏、绑定和解绑
在上一期我们学习了FragmentManager和FragmentTransaction的作用,并用案例学习了Fragment的添加.移除和替换,本期一起来学习Fragment显示和隐藏.绑定和解绑. ...
- EF CodeFirst数据迁移与防数据库删除
1 开启migrations功能 enable-migrations -force 2 添加迁移版本 add-migration 名称后缀 我们每次修改实体后,都应该使用这个add-migration ...