题意:给定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. springBoot异常处理

    1.status=404 Whitelabel Error Page Whitelabel Error Page This application has no explicit mapping fo ...

  2. MVC中数据传递 ViewBag的使用

    ViewBag MVC3中 ViewBag.ViewData和TempData的使用和差别 在MVC3開始.视图数据能够通过ViewBag属性訪问.在MVC2中则是使用ViewData.MVC3中保留 ...

  3. jsp常见的指令总结

    一.三个编译指令 1.page指令: 首先,我们要明确一点就是page指令是一个全局指令,针对当前页面,其次我们再来深挖他的功能,它到底有哪些功能那,在我们程序中起到什么作用??? a.语法结构:&l ...

  4. hibernate中1对1的注解配置

    hibernate中1对1的注解配置分为:外键关联映射和主键关联映射 1.外键配置 //一方@Entity@Table(name="test_classinfo")public c ...

  5. 数据预处理之独热编码(One-Hot Encoding)(转载)

    问题由来 在很多机器学习任务中,特征并不总是连续值,而有可能是分类值. 例如,考虑一下的三个特征: ["male", "female"] ["from ...

  6. git cherry-pick用法

    场景: 如果你的应用已经发布了一个版本2.0, 代码分支叫release-2.0, 现在正在开发3.0, 代码的分支叫dev-3.0. 那么有一天产品说, 要把正在开发的某个特性提前上线, 也就是说要 ...

  7. 123. Best Time to Buy and Sell Stock III (Array; DP)

    Say you have an array for which the ith element is the price of a given stock on day i. Design an al ...

  8. 84. Largest Rectangle in Histogram (Array, Stack; DP)

    Given n non-negative integers representing the histogram's bar height where the width of each bar is ...

  9. Java获取资源文件

    比如我们有以下目录 |--project |--src |--javaapplication |--Test.java |--file1.txt |--file2.txt |--build |--ja ...

  10. SQL日期和时间函数

    使用这些函数可以计算日期和时间值.例如,假设您希望了解通常在一周中哪一天的销售量最高.使用 DAYOFWEEK 函数,您可以创建一个公式来标识每天的销售订单数量.再比如,假设您希望比较在过去的一年中的 ...