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.前三个数构成抛物 ...
随机推荐
- Datatable数据分组
datatable里面的数据是按照这个顺序排列的 姓名 性别 年龄 a1 男 12 a1 女 11 a ...
- Producer-consumer problem in Python
from: http://agiliq.com/blog/2013/10/producer-consumer-problem-in-python/ By : Akshar Raaj We will s ...
- JAVA NIO学习记录1-buffer和channel
什么是NIO? Java NIO(New IO)是从Java 1.4版本开始引入的一个新的IO API,可以替代标准的Java IO API.NIO与原来的IO有同样的作用和目的,但是使用的方式完全不 ...
- Axure8 实现移动端页面上下滑动效果
目前,很多Axure新人都在问如何实现界面上下滑动效果,网上相关的教程也不少,各有各的方法,但是很少有教程对滑动界限设置做出比较详细的说明,其实在工作过程中,个人发现练好Axure是很有意提升逼格的, ...
- Spring Boot Unregistering JMX-exposed beans on shutdown
创建springboot项目运行的时候报这个错误Unregistering JMX-exposed beans on shutdown,搜索发现第一条是: Spring boot 嵌入的tomcat不 ...
- 使用robotium对android应用进行自动化测试
所需要的环境: 1.eclipse 2.android development tools(ADT) 3.software develoment kit(SDK) 4.JDK 5.robotium 1 ...
- Implementing the On Item Checked Event for the TListView Control
The TListView Delphi control displays a list of items in a fashion similar to how Windows Explorer d ...
- EXCEL保存提示“隐私问题警告:此文档中包含宏……”解决办法
先点击“禁止宏运行”的那个按钮.打开文件后,按alt + F11 进入宏编辑器,在“工程”里查看是什么宏.如果是你需要的,就留着.否则右击这个宏名称,选择“移除”. 另外,如果是你需要的,还需要在 工 ...
- mysql 中文编码问题
- django创建工程,用命令
django创建工程的命令 >>python C:\Python33\Lib\site-packages\django\bin\django-admin.py startproject p ...