题意:给定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 (三分求极值)的更多相关文章

  1. HLJU 1221: 高考签到题 (三分求极值)

    1221: 高考签到题 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 9  Solved: 4 [Submit][id=1221">St ...

  2. hihocoder 1142 三分求极值【三分算法 模板应用】

    #1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一 ...

  3. Hihocoder #1142 : 三分·三分求极值

    1142 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个 ...

  4. hihocoder 1142 三分·三分求极值(三分)

    题目1 : 三分·三分求极值 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点 ...

  5. ZOJ 3203 Light Bulb( 三分求极值 )

    链接:传送门 题意: 求影子长度 L 的最大值 思路:如果 x = 0 ,即影子到达右下角时,如果人继续向后走,那么影子一定是缩短的,所以不考虑这种情况.根据图中的辅助线外加相似三角形定理可以得到 L ...

  6. hdu 4717(三分求极值)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4717 思路:三分时间求极小值. #include <iostream> #include ...

  7. hicoder1142 三分求极值

    在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 我们代入公式,有: $d = min(\sqrt{(X - x)^2+(aX^2+bX+c-y)^2 ...

  8. hihocoder 第四十周 三分求极值

    题目链接:http://hihocoder.com/contest/hiho40/problem/1 ,一道简单的三分. 题目是在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求 ...

  9. 【HIHOCODER 1142】 三分·三分求极值

    描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 输入 第1行:5个整数a,b,c,x,y.前三个数构成抛物 ...

随机推荐

  1. ABAP-反调JCO服务

  2. dll is in timestamps but is not known in guidmapper… 错误

    解决方法是:在Assets下右键,Reimport All

  3. 迷你MVVM框架 avalonjs 学习教程18、一步步做一个todoMVC

    大凡出名的MVC,MVVM框架都有todo例子,我们也搞一下看看avalon是否这么便宜. 我们先从react的todo例子中扒一下HTML与CSS用用. <!doctype html> ...

  4. delphi实现两个目录路径的链接

    filepath := PathJoin(['C:', 'path1', 'path2\', 'a.doc']); // filepath = 'C:\path1\path2\a.doc' 代码: f ...

  5. eclispse修改项目项目编码

    最近遇到问题,在myeclipse新建或导入项目后,有些文件中文显示乱码,每次都要在项目property中修改其编码,所以想到一次性解决所有编码问题,让项目新建或导入之后自动是utf-8编码,这样就不 ...

  6. hibernate最佳实践

    1.数据量巨大,性能要求高,hibernate由于在ORM映射中对系统资源消耗也比较高,所以不适合 2.hibernate适合:逻辑复杂,数据量不大. 3.sessionFactory的创建非常消耗资 ...

  7. do{}while() ;异常语句

    //while (true) //只要括号里面是true(正确的如:(1==1)),就会无限循环 //{ //} //do{}while() //不管while满足与否,首先先做一遍 //然后去看wh ...

  8. Python 列表表达式 ,迭代器(2) Yield

    .yield 暂存为list def max_generator(numbers): current_max = for i in numbers: current_max = max(i, curr ...

  9. new operator

    [new operator] When the code new Foo(...) is executed, the following things happen: A new object is ...

  10. 微信小程序及开发工具介绍

    http://mp.weixin.qq.com/wiki  这里下载开发者工具