三分 - HNU 13409 Flowers
Flowers
Problem's Link: http://acm.hnu.cn/online/?action=problem&type=show&id=13409&courseid=0
Mean:
有N颗种子,每颗种子初始时营养值为0。当一颗种子营养值达到th后就会开花。
有两种操作:
1.给所有的种子浇w升水;
2.给某个种子施f升肥;
对于第i颗种子,每浇1升水会增加vw点营养值,每施1升肥可以增加vf点营养值,该种种子的肥料单价为pf,当营养值达到th后开花。
浇水必须一起浇,而且水的单价是一样的,都是pw。花的th可能为负,而且有的花浇水营养值会减少。
analyse:
很明显的凸函数。
由于浇水的量所有种子都相同,所以三分水量即可。
Time complexity: O(N)
Source code:
/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-16-19.52
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define eps 1e-14
#define LL __int64
#define ULL unsigned __int64
using namespace std;
const int MAXN=10+(int)1e5;
LL n,pw,vw[MAXN],pf[MAXN],vf[MAXN],th[MAXN];
double tmp,t;
double cal(double& w)
{
tmp=w*pw;
for(int i=0;i<n;++i)
{
t=th[i]-w*vw[i];
if(t<=0) continue;
tmp+=(t)*pf[i]*1./vf[i];
}
return tmp;
}
double work(double l,double r)
{
double mid,mmid;
int st=100;
while(st--)
{
mid=(l+r)/2;
mmid=(mid+r)/2;
if(cal(mid)-cal(mmid)>eps)
l=mid;
else r=mmid;
}
return l;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(scanf("%I64d",&n),n)
{
scanf("%I64d",&pw);
for(int i=0;i<n;++i)
{
scanf("%I64d %I64d %I64d %I64d",&vw[i],&pf[i],&vf[i],&th[i]);
}
double val=work(0,200);
printf("%.6lf\n",cal(val));
}
return 0;
}
/*
*/
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-08-16-19.52
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define eps 1e-14
#define LL __int64
#define ULL unsigned __int64
using namespace std;
const int MAXN=10+(int)1e5;
LL n,pw,vw[MAXN],pf[MAXN],vf[MAXN],th[MAXN];
double tmp,t;
double cal(double& w)
{
tmp=w*pw;
for(int i=0;i<n;++i)
{
t=th[i]-w*vw[i];
if(t<=0) continue;
tmp+=(t)*pf[i]*1./vf[i];
}
return tmp;
}
double work(double l,double r)
{
double mid,mmid;
int st=100;
while(st--)
{
mid=(l+r)/2;
mmid=(mid+r)/2;
if(cal(mid)-cal(mmid)>eps)
l=mid;
else r=mmid;
}
return l;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(0);
while(scanf("%I64d",&n),n)
{
scanf("%I64d",&pw);
for(int i=0;i<n;++i)
{
scanf("%I64d %I64d %I64d %I64d",&vw[i],&pf[i],&vf[i],&th[i]);
}
double val=work(0,200);
printf("%.6lf\n",cal(val));
}
return 0;
}
/*
*/