链接

二分高度,算面积的地方有点麻烦,没有用求交点的模板,直接自己按三角形相似手算了一下,写的有点麻烦。

上下界直接取水可放的最高点以及最低点。

自己的长得很挫的代码

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 2010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
double x ,y;
point(double x=,double y=):x(x),y(y){}
}p[N],q[N],ch[N];
double minz;
int n,m;
typedef point pointt;
pointt operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
double dis(point a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
}
double getarea(int n,point p[])
{
int i;
double s = ;
for(i = ; i < n ; i++)
s+=cross(p[i]-p[],p[i+]-p[]);
return s/;
}
double cal(double r,point p[],int n)
{
int i,j;
int k = ;
for(i = ; i <= n; i++)
if(p[i].y<p[k].y) k = i; if(dcmp(r-p[k].y)<=) return 0.0; point p1,p2;
int g = ;
for(i = ; i < k; i++)
if(dcmp(p[i].y-r)>=&&dcmp(p[i+].y-r)<)
{
p1.y = r;
double d = r-p[i+].y;
p1.x = d*(p[i].x-p[i+].x)/(p[i].y-p[i+].y)+p[i+].x;
break;
} for(j = k ; j >= i+; j--)
ch[++g] = p[j];
ch[++g] = p1;
for(i = k+ ; i <= n; i++)
if(dcmp(p[i-].y-r)<&&dcmp(p[i].y-r)>=)
{
p2.y = r;
double d = r-p[i-].y;
p2.x = d*(p[i].x-p[i-].x)/(p[i].y-p[i-].y)+p[i-].x;
break;
}
ch[++g] = p2;
for(j = i- ; j > k; j--)
ch[++g] = p[j]; return fabs(getarea(g,ch));
}
int main()
{
int t,i,a;
cin>>t;
while(t--)
{
scanf("%d",&a);
scanf("%d",&n);
minz = INF;
double maxz = INF;
for(i = ; i <= n ;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
maxz = min(maxz,p[i].y);
}
scanf("%d",&m);
for(i = ; i <= m ;i++)
{
scanf("%lf%lf",&q[i].x,&q[i].y);
maxz = min(maxz,p[i].y);
}
minz = min(min(p[].y,p[n].y),min(q[].y,q[m].y));
double lef = maxz,rig = minz,mid;
while(fabs(rig-lef)>eps)
{
mid = (rig+lef)/2.0;
double s1 = cal(mid,p,n),s2 = cal(mid,q,m);
if(dcmp(s1+s2-a)>)
rig = mid;
else lef = mid;
}
printf("%.3f\n",lef);
}
return ;
}

感觉不错的别人的代码

 #include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 2010
#define LL long long
#define INF 0xfffffff
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct point
{
double x ,y;
point(double x=,double y=):x(x),y(y){}
}p[N],q[N],ch[N];
double minz;
typedef point pointt;
pointt operator -(point a,point b)
{
return point(a.x-b.x,a.y-b.y);
}
int dcmp(double x)
{
if(fabs(x)<eps) return ;
return x<?-:;
}
double dis(point a)
{
return sqrt(a.x*a.x+a.y*a.y);
}
double cross(point a,point b)
{
return a.x*b.y-a.y*b.x;
} double getarea(int n,point p[])
{
int i;
double s = ;
for(i = ; i < n- ; i++)
s+=cross(p[i]-p[],p[i+]-p[]);
return s/;
}
//double cal(double r,point p[],int n)
//{
// int i,j;
// int k = 1;
// for(i = 1; i <= n; i++)
// if(p[i].y<p[k].y) k = i;
//
// r = min(r,minz);
// if(dcmp(r-p[k].y)<=0) return 0.0;
//
// point p1,p2;
// int g = 0;
// for(i = 1; i < k; i++)
// if(dcmp(p[i].y-r)>=0&&dcmp(p[i+1].y-r)<0)
// {
// p1.y = r;
// double d = r-p[i+1].y;
// p1.x = d*(p[i].x-p[i+1].x)/(p[i].y-p[i+1].y)+p[i+1].x;
// break;
// }
// // cout<<p1.x<<" "<<p1.y<<endl;
// for(j = k ; j >= i+1; j--)
// ch[++g] = p[j];
// ch[++g] = p1;
// for(i = k+1 ; i <= n; i++)
// if(dcmp(p[i-1].y-r)<0&&dcmp(p[i].y-r)>=0)
// {
// p2.y = r;
// double d = r-p[i-1].y;
// p2.x = d*(p[i].x-p[i-1].x)/(p[i].y-p[i-1].y)+p[i-1].x;
// break;
// }
// ch[++g] = p2;
// for(j = i-1 ; j > k; j--)
// ch[++g] = p[j];
//
// return fabs(getarea(g,ch));
//}
point intersect(point p1,point p2,point p3,point p4){
point p;
double a1,b1,a2,b2,c1,c2,d;
a1=p1.y-p2.y; b1=p2.x-p1.x; c1=p1.x*p2.y-p2.x*p1.y;
a2=p3.y-p4.y; b2=p4.x-p3.x; c2=p3.x*p4.x-p4.x*p3.y;
d=a1*b2-a2*b1;
p.x=(-c1*b2+c2*b1)/d;
p.y=(-a1*c2+a2*c1)/d;
return p;
}
bool pan(point a,point b,double y){
return dcmp(a.y-y)*dcmp(b.y-y)<=;
}
double cal(double y,point P[],int n){ int i,j,k;
int tot=;
point a=point(,y),b=point(,y);
for(i=;i<n;i++){
if(pan(P[i],P[i+],y)){
ch[tot++]=intersect(P[i],P[i+],a,b);
break;
}
}
for(j=i+;j<n;j++){
ch[tot++]=P[j];
if(pan(P[j],P[j+],y)){
ch[tot++]=intersect(P[j],P[j+],a,b);
break;
}
}
double area=getarea(tot,ch);
return area;
}
int main()
{
int t,i,a,n,m;
cin>>t;
while(t--)
{
scanf("%d",&a);
scanf("%d",&n);
minz = INF;
for(i = ; i <= n ;i++)
{
scanf("%lf%lf",&p[i].x,&p[i].y);
minz = min(minz,p[i].y);
}
scanf("%d",&m);
for(i = ; i <= m ;i++)
{
scanf("%lf%lf",&q[i].x,&q[i].y);
minz = min(minz,q[i].y);
}
double maxz = min(min(p[].y,p[n].y),min(q[].y,q[m].y));
double lef = minz,rig = maxz,mid;
while(fabs(rig-lef)>eps)
{
mid = (rig+lef)/2.0;
double s1 = cal(mid,p,n),s2 = cal(mid,q,m);
if(dcmp(s1+s2-a)>)
rig = mid;
else lef = mid;
}
printf("%.3f\n",lef);
}
return ;
}

poj3334Connected Gheeves(二分)的更多相关文章

  1. poj3334(Connected Gheeves)

    Connected Gheeves Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 1008   Accepted: 368 ...

  2. poj 3334 Connected Gheeves (Geometry + BInary Search)

    3334 -- Connected Gheeves 题意是,给出两个尖形的相连的容器,要求向其中灌水.它们具有日常的物理属性,例如两个容器中水平面高度相同以及水高于容器顶部的时候就会溢出.开始的时候打 ...

  3. BZOJ1012: [JSOI2008]最大数maxnumber [线段树 | 单调栈+二分]

    1012: [JSOI2008]最大数maxnumber Time Limit: 3 Sec  Memory Limit: 162 MBSubmit: 8748  Solved: 3835[Submi ...

  4. BZOJ 2756: [SCOI2012]奇怪的游戏 [最大流 二分]

    2756: [SCOI2012]奇怪的游戏 Time Limit: 40 Sec  Memory Limit: 128 MBSubmit: 3352  Solved: 919[Submit][Stat ...

  5. 整体二分QAQ

    POJ 2104 K-th Number 时空隧道 题意: 给出一个序列,每次查询区间第k小 分析: 整体二分入门题? 代码: #include<algorithm> #include&l ...

  6. [bzoj2653][middle] (二分 + 主席树)

    Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...

  7. [LeetCode] Closest Binary Search Tree Value II 最近的二分搜索树的值之二

    Given a non-empty binary search tree and a target value, find k values in the BST that are closest t ...

  8. [LeetCode] Closest Binary Search Tree Value 最近的二分搜索树的值

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  9. jvascript 顺序查找和二分查找法

    第一种:顺序查找法 中心思想:和数组中的值逐个比对! /* * 参数说明: * array:传入数组 * findVal:传入需要查找的数 */ function Orderseach(array,f ...

随机推荐

  1. JavaEE基础(十九)/异常和File

    1.异常(异常的概述和分类) A:异常的概述 异常就是Java程序在运行过程中出现的错误. B:异常的分类 通过API查看Throwable Error 服务器宕机,数据库崩溃等 Exception ...

  2. Web前端工作2个月小结

    开始语: 2013年6月30日,Microsoft Learning support 项目结束,转而进入Forerunner Development 项目,这对于这块领域空白的我,空前的困难,可是我坚 ...

  3. oracle VS postgresql系列-行列转换

    [需求]例如先有数据为 id | name ------+--------- | lottu | xuan | rax | ak | vincent 现在需要转换为 id | names ------ ...

  4. Linux C socket 编程之TCP

    推荐:http://www.cnblogs.com/tianshuai11/archive/2011/11/30/2477224.html

  5. python: indentationerror: unexpected indent

    以后遇到了IndentationError: unexpected indent你就要知道python编译器是在告诉你“Hi,老兄,你的文件里格式不对了,可能是tab和空格没对齐的问题,你需要检查下t ...

  6. miniUI子窗口调父窗口方法

    window.Owner.XXX 其中XXX是父窗口里的方法名

  7. mysql一些常用命令总结

    mysql时间戳转日期格式SELECT FROM_UNIXTIME(add_time,'%Y-%m-%d %H:%i:%s') FROM `wh5_username` where id=23; 按in ...

  8. vsftp 使用匿名帐号登陆

    1.正常安装. 2.改配置文件:vi /etc/vsftpd/vsftpd.conf #允许匿名用户登录FTP anonymous_enable=YES #设置匿名用户的登录目录(如需要,需自己添加并 ...

  9. Windows手动添加开机启动项

    @方法1. 添加程序完整路径到注册表HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Run下 或者添加到HKEY_CURREN ...

  10. fastboot 教程

    参考: http://blog.csdn.net/geniusmen/article/details/7892398 http://www.cnblogs.com/eastnapoleon/p/327 ...