洛谷P2179 [NOI2012]骑行川藏(拉格朗日乘数法)

题面

传送门

题解

\(mashirosky\)大佬的题解吧……这里

//minamoto
#include<bits/stdc++.h>
#define R register
#define inf 0x3f3f3f3f
#define inline __inline__ __attribute__((always_inline))
#define fp(i,a,b) for(R int i=(a),I=(b)+1;i<I;++i)
#define fd(i,a,b) for(R int i=(a),I=(b)-1;i>I;--i)
#define go(u) for(int i=head[u],v=e[i].v;i;i=e[i].nx,v=e[i].v)
using namespace std;
char buf[1<<21],*p1=buf,*p2=buf;
inline char getc(){return p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++;}
int read(){
    R int res,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(res=ch-'0';(ch=getc())>='0'&&ch<='9';res=res*10+ch-'0');
    return res*f;
}
double readdb()
{
    R double x=0,y=0.1,f=1;R char ch;
    while((ch=getc())>'9'||ch<'0')(ch=='-')&&(f=-1);
    for(x=ch-'0';(ch=getc())>='0'&&ch<='9';x=x*10+ch-'0');
    for(ch=='.'&&(ch=getc());ch>='0'&&ch<='9';x+=(ch-'0')*y,y*=0.1,ch=getc());
    return x*f;
}
const int N=10005;const double eps=1e-13;
inline int sgn(R double x){return x<-eps?-1:x>eps;}
double s[N],k[N],vp[N],v[N],mx[N],E;
int n;
bool ck(double lam){
	double res=0,l,r,mid;
	fp(i,1,n){
		l=max(0.0,vp[i]),r=mx[i],mid;
		while(sgn(r-l)>0){
			mid=(l+r)*0.5;
			(sgn(2*lam*mid*mid*k[i]*(mid-vp[i])+1)>=0)?l=mid:r=mid;
		}
		v[i]=l,res+=k[i]*(v[i]-vp[i])*(v[i]-vp[i])*s[i];
		if(sgn(E-res)<0)return false;
	}
	return true;
}
int main(){
//	freopen("testdata.in","r",stdin);
	n=read(),E=readdb();
	fp(i,1,n){
		s[i]=readdb(),k[i]=readdb(),vp[i]=readdb();
		mx[i]=sgn(s[i])?sqrt(E/k[i]/s[i])+vp[i]:inf;
	}
	double l=-inf,r=0,mid;
	while(sgn(r-l)>0){
		mid=(l+r)*0.5;
		ck(mid)?l=mid:r=mid;
	}
	ck(r);
	double res=0;
	fp(i,1,n)res+=s[i]/v[i];
	printf("%.8lf\n",res);
	return 0;
}
posted @ 2019-04-14 15:54  bztMinamoto  阅读(244)  评论(0编辑  收藏  举报
Live2D