HDU 4355 Party All the Time (三分求极值)
题意:给定x轴上有n个点,每一个点都有一个权值,让在x轴上选一个点,求出各点到这个点的距离的三次方乘以权值最小。
析:首先一开始我根本不会三分,也并没有看出来这是一个三分的题目的,学长说这是一个三分的题,我就百度了一下什么是三分算法,一看感觉和二分差不多,当然就是和二分差不多,也是慢慢缩短范围。
这个题也这样,在最左端和最右端不断的三分,直到逼进那个点,刚开始我设置的误差eps是10负8,但是TLE了,我以为是太小,三分数太多,然后我又改成10负6还是TLE,我又失望了,干脆我不用误差了,我让它三分200次就结束,但一直是TLE,直到我改到20次,才AC。但实际并不是我设置的太小,而是我用了pow这个函数和fabs这个函数,这两个函数运行起来太慢了,导致我TLEn次,所以我不建议用这两个函数,完全可以自己写嘛,这样才会更快。
知道三分,这个题就很简单了,就是扫一下而已。
代码如下:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <vector> using namespace std;
typedef long long LL;
const int maxn = 50005;
const double eps = 1E-6;
double x[maxn], w[maxn];
int n; double f(double mid){
double ans = 0.0;
for(int i = 0; i < n; ++i)
ans += pow(fabs(x[i] - mid), 3) * w[i];
return ans; }
int main(){
int T, cases = 0; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%lf %lf", &x[i], &w[i]); double r = x[n-1], l = x[0];
for(int i = 0; i < 30; ++i){
double mid_l = l + (r-l) / 3.0;
double mid_r = r - (r-l) / 3.0;
if(f(mid_l) < f(mid_r)) r = mid_r;
else l = mid_l;
} int ans1 = (int)floor(f(l)+0.5), ans2 = (int)floor(f(r)+0.5);
int ans = min(ans1, ans2);
printf("Case #%d: %d\n", ++cases, ans);
}
return 0;
}
这是我不用pow函数的代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <set>
#include <map>
#include <cmath>
#include <string>
#include <vector> using namespace std;
typedef long long LL;
const int maxn = 50005;
const double eps = 1E-8;
double x[maxn], w[maxn];
int n; double f(double mid){
double ans = 0.0;
for(int i = 0; i < n; ++i){
double tmp = x[i] - mid;
if (tmp < 0)tmp = -tmp;
ans += tmp*tmp*tmp* w[i]; }
return ans; }
int main(){
int T, cases = 0; cin >> T;
while(T--){
scanf("%d", &n);
for(int i = 0; i < n; ++i)
scanf("%lf %lf", &x[i], &w[i]); double r = x[n-1], l = x[0];
while(r - l > eps){
double mid_l = l + (r-l) / 3.0;
double mid_r = r - (r-l) / 3.0;
if(f(mid_l) < f(mid_r)) r = mid_r;
else l = mid_l;
} int ans1 = (int)floor(f(l)+0.5), ans2 = (int)floor(f(r)+0.5);
int ans = min(ans1, ans2);
printf("Case #%d: %d\n", ++cases, ans);
}
return 0;
}
HDU 4355 Party All the Time (三分求极值)的更多相关文章
- HLJU 1221: 高考签到题 (三分求极值)
1221: 高考签到题 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 9 Solved: 4 [Submit][id=1221">St ...
- hihocoder 1142 三分求极值【三分算法 模板应用】
#1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一 ...
- Hihocoder #1142 : 三分·三分求极值
1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个 ...
- hihocoder 1142 三分·三分求极值(三分)
题目1 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点 ...
- ZOJ 3203 Light Bulb( 三分求极值 )
链接:传送门 题意: 求影子长度 L 的最大值 思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况.根据图中的辅助线外加相似三角形定理可以得到 L ...
- hdu 4717(三分求极值)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 思路:三分时间求极小值. #include <iostream> #include ...
- hicoder1142 三分求极值
在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 我们代入公式,有: $d = min(\sqrt{(X - x)^2+(aX^2+bX+c-y)^2 ...
- hihocoder 第四十周 三分求极值
题目链接:http://hihocoder.com/contest/hiho40/problem/1 ,一道简单的三分. 题目是在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求 ...
- 【HIHOCODER 1142】 三分·三分求极值
描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 输入 第1行:5个整数a,b,c,x,y.前三个数构成抛物 ...
随机推荐
- 前端-CSS-2-选择器
基本选择器 首先来说一下,什么是选择器.在一个HTML页面中会有很多很多的元素,不同的元素可能会有不同的样式, 某些元素又需要设置相同的样式,选择器就是用来从HTML页面中查找特定元素的,找到元素之后 ...
- python练习题:三级菜单
需求:可依次选择进入各子菜单可从任意一层往回退到上一层可从任意一层退出程序所需新知识点:列表.字典 测试环境:win7系统,python3.7.0,工具:pycharm-community-2018. ...
- ansible之条件语句when
注册变量: 变量的另一个用途是将一条命令的运行结果保存到变量中,供后面的playbook使用.例如: - hosts: webservers tasks: - shell: /usr/bin/foo ...
- oracle惯用缩写的含义
$ORACLE_HOME/bin下的utilities解释Binary First Available Description----------------- ...
- SQL 2008维护计划不执行的问题
平台环境; 先是装了WINDOWS 2008,没有升级到AD,再安装了sql2008后再升级了AD. 现在SQL建了几个数据库备份计划,但都提示下面的信息: 日期 2010-4-15 9:36:00 ...
- fm 讲解加代码
转自: 博客 http://blog.csdn.net/google19890102/article/details/45532745/ github https://github.com/zhaoz ...
- intellij idea 的常见配置
1.视图配置 配置好后如下图: 2.修改字体大小 3.编码修改 4.行号显示 5.控制台字体大小调整 File->Settings->Editor->Colors & F ...
- C函数指针的用法
1.最简单的用法: #include <cstdio> int (*p)(int);//定义一个函数指针变量p(下面的f其实是一个常量函数指针) int f(int x) { printf ...
- 使用css实现特殊标志或图形
1. 前言 由于图片占的空间比较大,且图片越多,越不好管理,所以有些时候,我们可以使用一些简单的标签样式来实现简单的图形标志来替代图片. 2. 实例展示: 三角形示例 示例代码: <style ...
- day9:vcp考试
Q161. An administrator wants to select a Host Power Management Policy for an ESXi 6.x host that will ...