CSU 1548 Design road(三分查找)
题目链接:https://cn.vjudge.net/problem/142542/origin
Description
You need to design road from (0, 0) to (x, y) in plane with the lowest cost. Unfortunately, there are N Rivers between (0, 0) and (x, y).It costs c1 Yuan RMB per meter to build road, and it costs c2 Yuan RMB per meter to build a bridge. All rivers are parallel to the Y axis with infinite length.
Input
There are several test cases.
Each test case contains 5 positive integers N,x,y,c1,c2 in the first line.(N ≤ 1000,1 ≤ x,y≤ 100,000,1 ≤ c1,c2 ≤ 1000).
The following N lines, each line contains 2 positive integer xi, wi ( 1 ≤ i ≤ N ,1 ≤ xi ≤x, xi-1+wi-1 < xi , xN+wN ≤ x),indicate the i-th river(left bank) locate xi with wi width.
The input will finish with the end of file.
Output
For each the case, your program will output the least cost P on separate line, the P will be to two decimal places .
Sample Input
1 300 400 100 100
100 50
1 150 90 250 520
30 120
Sample Output
50000.00
80100.00
Hint
题意:
给你一个二维的坐标系,你要从[0,0]走到[x,y]。但是其间会有一些平行于y轴的河,你需要架桥。给你每米的修路费,和每米的修桥费,问最少的费用是多少?
题解:
现场的时候看了这题,感觉是2元方程,很难求的感觉。赛后,师兄说是三分求最值。然后学了一下三分,就A了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <sstream>
#include <cmath>
#include <algorithm>
using namespace std;
#define pb push_back
#define mp make_pair
#define exp 1e-7
#define ms(a, b) memset((a), (b), sizeof(a))
//#define LOCAL
typedef long long LL;
const int inf = 0x3f3f3f3f;
const int maxn = +;
const int mod = ;
double n, x, y, c1, c2, riverlen, landlen;
double cut(double hh)
{
return c2*hypot(riverlen, hh) + c1*hypot(landlen, y-hh);
}
int main() {
#ifdef LOCAL
freopen("input.txt" , "r", stdin);
#endif // LOCAL
while(~scanf("%lf%lf%lf%lf%lf", &n, &x, &y, &c1, &c2)){
riverlen = 0.0;
for(int i=;i<n;i++){
double xi, wi;
scanf("%lf%lf", &xi, &wi);
riverlen += wi;
}
landlen = x - riverlen;
double l , r, mid, mmid;
l = ;
r = y;
while( l + exp < r){
mid = ( l + r ) / ;
mmid = (mid + r) / ;
if( cut(mid) <= cut(mmid) )
r = mmid;
else
l = mid;
}
printf("%.2f\n", cut(l));
}
return ;
}
CSU 1548 Design road(三分查找)的更多相关文章
- 三分 --- CSU 1548: Design road
Design road Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...
- 1548: Design road (思维题 做法:三分找极值)
1548: Design road Submit Page Summary Time Limit: 2 Sec Memory Limit: 256 Mb Submitted ...
- csu 1548(三分)
1548: Design road Time Limit: 2 Sec Memory Limit: 256 MBSubmit: 383 Solved: 200[Submit][Status][We ...
- HDU 5144 NPY and shot(物理运动学+三分查找)
NPY and shot Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- Gym 101246J Buoys(三分查找)
http://codeforces.com/gym/101246/problem/J 题意: 给定x轴上的n个点的坐标,按顺序从左到右给出,现在要使得每个点的间距相同,可以移动每个点的坐标,但是不能改 ...
- HDU4355 三分查找
/* * 三分查找 */ #include<cstdio> #include<cmath> #define eps 1e-6 //typedef __int64 LL; i ...
- [LeetCode] Add and Search Word - Data structure design 添加和查找单词-数据结构设计
Design a data structure that supports the following two operations: void addWord(word) bool search(w ...
- hdu_2899_Strange fuction(三分查找)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=2899 题意:让你解方程 题解:对于只有一个凸或者没有凸的图像,可以直接上三分解决. #include& ...
- HDU 2438 Turn the corner(三分查找)
托一个学弟的福,学了一下他的最简便三分写法,然后找了一道三分的题验证了下,AC了一题,写法确实方便,还是我太弱了,漫漫AC路!各路大神,以后你们有啥好的简便写法可以在博客下方留个言或私信我,谢谢了! ...
随机推荐
- JDK8以后接口是可以定义实现方法,必须需要default修饰符修饰
package com.company.java.oop.cls; interface IB { default void doMethod1() { System.out.println(" ...
- jdbc步骤:
一.注册数据库驱动 Class.forName("com.mysql.jdbc.Driver"); 二.建立连接(Connection) Connection conn = Dri ...
- Tensorflow实战 手写数字识别(Tensorboard可视化)
一.前言 为了更好的理解Neural Network,本文使用Tensorflow实现一个最简单的神经网络,然后使用MNIST数据集进行测试.同时使用Tensorboard对训练过程进行可视化,算是打 ...
- go工具链
1 编辑器 goland 2 GOPATH GOPATH是go的一个环境变量,它以绝对路径提供go的工作目录. go工程的源码存放在${GOPATH}/src目录下,go编译过程中生成的中间文件存放在 ...
- BZOJ 1875(DP+矩阵快速幂)
题面 传送门 分析 容易想到根据点来dp,设dp[i][j]表示到i点路径长度为j的方案数 状态转移方程为dp[i][k]=∑(i,j)∈Edp[j][k−1]" role="pr ...
- Spring之使用注解实例化Bean并注入属性
1.准备工作 (1)导入jar包 除了上篇文章使用到的基本jar包外,还得加入aop的jar包,所有jar包如下 所需jar包 (2)配置xml <?xml version="1.0& ...
- 搜索(DFS)---能到达的太平洋和大西洋的区域
能到达的太平洋和大西洋的区域 417. Pacific Atlantic Water Flow (Medium) Given the following 5x5 matrix: Pacific ~ ~ ...
- 什么是dockerfile?
什么是dockerfile? Dockerfile是一个包含用于组合映像的命令的文本文档.可以使用在命令行中调用任何命令. Docker通过读取Dockerfile中的指令自动生成映像. docker ...
- Codeforces Round #425 (Div. 2) - B
题目链接:http://codeforces.com/contest/832/problem/B 题意:给定一个好字母集合(只有小写字母,除了这些外其余都是坏字母集合),给定一个匹配模式串, 模式串只 ...
- sysbench github & manual
sysbench github https://github.com/akopytov/sysbench sysbench-manual.pdf https://github.com/mrivandu ...