题意:给定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. windows平台下 c++获取 系统版本 网卡 内存 CPU 硬盘 显卡信息<转>

    GetsysInfo.h: #ifndef _H_GETSYSINFO #define _H_GETSYSINFO #pragma once #include <afxtempl.h> c ...

  2. PowerEdge服务器生命周期控制器:Lifecycle Controller

    戴尔从第11代服务器开始推出生命周期控制器(简称LC,即Lifecycle Controller).生命周期控制器(LC)通过在主板上部署的控制芯片和闪存,与BMC以及iDRAC卡配合,在服务器的整个 ...

  3. 条件语句;for循环 嵌套复习

    //打印数字,0,1,8,10,12,每一个数单独占一行 //在全部数字打印完毕之后在打印数字的个数和所有数的和 int count = 0; int sum = 0; for (int i = 0; ...

  4. localstorage是什么,它有哪些作用

    localStorage作为HTML5本地存储web storage特性的API之一,主要作用是将数据保存在客户端中,而客户端一般是指上海网站设计用户的计算机.在移动设备上,由于大部分浏览器都支持 w ...

  5. 【转】Android Shape绘制虚线在手机端查看是实线的问题

    Android share绘制虚线在手机上显示实线问题 给控件添加Drawableleft等图片后,单独给图片设置动画效果,参考文章: http://blog.csdn.net/langzxz/art ...

  6. python,使用PIL库对图片进行操作

    在做识别验证码时,需要对验证码图片进行一些处理,所以就学习了一下PIL的知识,下面是我总结的一些常用方法. 注明:图片的操作都需要Image库,所以要使用import Image导入库 1.打开图片 ...

  7. 结对项目3-bug的三种状态

    这周和小伙伴结对构造程序,来深刻理解软件测试中,bug发现的三种状态. 1:不能触发Fault 2:触发Fault,但是不能触发Error 3:触发Error,但是不能产生Failure 我们完成的代 ...

  8. Easyui-datagrid显示时间的格式化代码

    {field: 'Time', title: '时间', formatter: function (value, row, index) { var date = new Date(value); v ...

  9. Spring配置连接池

    ---------------------siwuxie095                                 Spring 配置连接池         1.Spring 配置内置连接 ...

  10. strcpy函数;memcpy函数;memmove函数

    strcpy函数实现: char* strcpy(char* des,const char* source) { char* r=des; assert((des != NULL) && ...