博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
HDU 4355 Party All the Time (三分求凸函数极值)
阅读量:4325 次
发布时间:2019-06-06

本文共 2740 字,大约阅读时间需要 9 分钟。

题目大意:有n个精灵位于一维坐标轴上, 每个精灵有一个权值w, 每个精灵走到另一个位置耗费能量S^3 * w。(s是两点间距离)。现在精灵要聚会, 求选取一点,使所有精灵到这点浪费能量之和最小。  
分析:设这点的位置是x,精灵位置是pi,则浪费能量值和f = sigma[(pi-x)^3*wi].其中x是变量. 通过对这个函数求二次导可以发现,这个函数是一个下凸函数.可以用
三分做.   具体的三分方法如图: 凸函数:   凹函数:     三分求凸函数极值模板:  
//如果一个解函数的二次导恒>0(or <0),则该函数为凸函数.//精度模板const double eps = 1e-6;bool dy(double x,double y)  {   return x > y + eps;} 		// x > ybool xy(double x,double y)  {   return x < y - eps;} 		// x < ybool dyd(double x,double y) {   return x > y - eps;} 		// x >= ybool xyd(double x,double y) {   return x < y + eps;}     	// x <= ybool dd(double x,double y)  {   return fabs( x - y ) < eps;}    // x == ydouble cal(double pos){    /* 根据题目的意思计算 */}double solve(double left, double right){    while(xy(left, right)){        double mid = DMID(left, right);        double midmid = DMID(mid, right);        double mid_area = cal(mid);        double midmid_area = cal(midmid);        if (xyd(mid_area, midmid_area))     //下凸函数,上凸函数用dyd(mid_area, midmid_area)            right = midmid;        else            left = mid;    }    return left;}
  HDU 4355 代码:  
#include #include #include #include #include #include #include #include #include #include #include #include #define MID(x,y) ((x+y)>>1)#define DMID(x,y) ((x+y)/2)#define mem(a,b) memset(a,b,sizeof(a))using namespace std;const int N = 50002;double x[N], w[N];int n;//二分精度模板const double eps = 1e-6;bool dy(double x,double y)  {   return x > y + eps;} 		// x > ybool xy(double x,double y)  {   return x < y - eps;} 		// x < ybool dyd(double x,double y) {   return x > y - eps;} 		// x >= ybool xyd(double x,double y) {   return x < y + eps;}     	// x <= ybool dd(double x,double y)  {   return fabs( x - y ) < eps;}    // x == ydouble cal(double pos){    /* 根据题目的意思计算 */    double res = 0.0;    for (int i = 0; i < n; i ++){        res += abs(pos - x[i]) * abs(pos - x[i]) * abs(pos - x[i]) * w[i];    }    return res;}double solve(double left, double right){    while(xy(left, right)){        double mid = DMID(left, right);        double midmid = DMID(mid, right);        double mid_area = cal(mid);        double midmid_area = cal(midmid);        if (xyd(mid_area, midmid_area))     //下凸函数,上凸函数dyd(mid_area, midmid_area)            right = midmid;        else            left = mid;    }    return left;}int main(){    int t;    scanf("%d", &t);    for (int caseo = 1; caseo <= t; caseo ++){        scanf("%d", &n);        double low = -1000000, high = 1000000;        for (int i = 0; i < n; i ++){            scanf("%lf %lf", &x[i], &w[i]);            low = min(low, x[i]);            high = max(high, x[i]);        }        printf("Case #%d: %.0f\n", caseo, (cal(solve(low, high))));    }	return 0;}
 

转载于:https://www.cnblogs.com/AbandonZHANG/archive/2013/04/04/4114232.html

你可能感兴趣的文章
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_9、SpringBoot基础HTTP其他提交方法请求实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_12、SpringBoot2.x文件上传实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_11、SpringBoot2.x目录文件结构讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_15、SpringBoot2.x配置文件讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第2节 SpringBoot接口Http协议开发实战_13、jar包方式运行web项目文件上传和访问...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_17、SpringBootTest单元测试实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_14、SpringBoot2.x使用Dev-tool热部署...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_19、SpringBoot个性化启动banner设置debug日志...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第三节SpringBoot热部署devtool和配置文件自动注入实战_16、注解配置文件自动映射到属性和实体类实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_20、SpringBoot2.x配置全局异常实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_18、SpringBoot测试进阶高级篇之MockMvc讲解...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第5节 SpringBoot部署war项目到tomcat9和启动原理讲解_23、SpringBoot2.x启动原理概述...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第4节 Springboot2.0单元测试进阶实战和自定义异常处理_21、SpringBoot2.x配置全局异常返回自定义页面...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_32..SpringBoot2.x持久化数据方式介绍...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_34、SpringBoot整合Mybatis实操和打印SQL语句...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_36、SpringBoot整合mybatis之事务处理实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_38、源码编译安装Redis4.x...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_33、SpringBoot2.x整合Mybatis3.x注解实战...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第8节 数据库操作之整合Mybaties和事务讲解_35、事务介绍和常见的隔离级别,传播行为...
查看>>
小D课堂 - 零基础入门SpringBoot2.X到实战_第9节 SpringBoot2.x整合Redis实战_40、Redis工具类封装讲解和实战...
查看>>