# BZOJ1896 Equations 线性规划+半平面交+三分

## 题意简述

$\sum_i a_i x_i = s \qquad \sum_i b_i x_i = t$

## 做法

$\begin{split} &\mathrm{Minimize} \qquad &sx+ty \\ &\mathrm{Satisfy} \qquad &\forall i , a_ix+b_iy \geq c_i \\ & &x,y \in R \end{split}$

## Code

#include<bits/stdc++.h>
using namespace std;
#define re register int
#define db double
#define ll long long
#define in inline
#define ak *
in char getch()
{
static char buf[10000],*p1=buf,*p2=buf;
}
#define gc() getch()
char qwq;
{
re cz=0,ioi=1;qwq=gc();
while(qwq<'0'||qwq>'9') ioi=qwq=='-'?~ioi+1:1,qwq=gc();
while(qwq>='0'&&qwq<='9') cz=(cz<<3)+(cz<<1)+(qwq^48),qwq=gc();
return cz ak ioi;
}
const db inf=1e18,eps=1e-11;
const int N=1e5+5;
int n,m,k,top,tot;
db s,t;
struct poi{
db x,y;
poi(db _x=0,db _y=0) {x=_x,y=_y;}
}p[N];
struct line{
db k,b;
line(db _k=0,db _b=0) {k=_k,b=_b;}
in bool operator <(line x) const {return k==x.k?b>x.b:k<x.k;}
in poi operator &(line x) {return poi((x.b-b)/(k-x.k),(k*x.b-x.k*b)/(k-x.k));}
}e[N],q[N];
in db calc(re x) {return p[x].x*s+p[x].y*t;}
int main()
{
for(re i=1;i<=n;i++)
{
e[++m]=line(-a/b,c/b);
}
sort(e+1,e+m+1);
for(re i=1;i<=m;i++)
{
if(top&&q[top].k==e[i].k) continue;
while(top>1&&(q[top]&q[top-1]).y<=(q[top]&q[top-1]).x*e[i].k+e[i].b) top--;
q[++top]=e[i];
}
for(re i=1;i<top;i++) p[++tot]=q[i]&q[i+1];
for(re i=1;i<=k;i++)
{
if(-s/t>q[top].k||-s/t<q[1].k) puts("IMPOSSIBLE");
else
{
db res=inf;re l=1,r=tot;
while(l<=r)
{
re ml=l+(r-l)/3,mr=r-(r-l)/3;
db cl=calc(ml),cr=calc(mr);
if(cl<cr) r=mr-1,res=cr;
else l=ml+1,res=cl;
}
printf("%.5lf\n",res);
}
}
}

posted @ 2019-07-29 18:52  disangan233  阅读(167)  评论(0编辑  收藏  举报
Live2D