http://poj.org/problem?id=2976
经典01分数规划裸题
题意:
n门功课,有该门课的得分和总分,求去掉k门课后平均绩点最高可以是多少?
根据01分数规划推导结论
有:
/*
* 0-1 分数规划
* t1 * x1 + t2 * x2 + ... + tn * xn
* r = ---------------------------------
* c1 * x1 + c2 * x2 + ... + cn * xn
* 给定t[1..n], c[1..n], 求x[1..n]使得sigma(xi)=k且r最大(小).
* 为了让r最大, 先设计子问题z(r) = (t1 * x1 + .. + tn * xn) - r * (c1 * xn + .. + cn * xn);
* 假设r的最优值为R. 则有:
* z(r) < 0 当且仅当 r > R;
* z(r) = 0 当且仅当 r = R;
* z(r) > 0 当且仅当 r < R;
* 于是可二分求R.
*/
由于z(r)满足单调性
因此本题,我们可以求得所有的d[i]=100*a[i]-b[i],排序后,尽量选最大的前n-k个,然后求和,对其二分逼近答案
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;
#define inf 2147483647
const int N = 1005;
double aa[N],bb[N],c[N];
int n,m;
double bin(double x)
{
for (int i=1; i<=n; i++)
c[i]=100.0*aa[i]-x*bb[i];
sort(c+1,c+1+n);
double sum=0;
for (int i=n; i>m; i--)
sum+=c[i];
return sum;
}
int main()
{
while(cin>>n>>m)
{
if (!n&&!m) break;
for (int i=1; i<=n; i++)
scanf("%lf",&aa[i]);
for (int i=1; i<=n; i++)
scanf("%lf",&bb[i]);
double l=0,r=1e16;
while(r-l>1e-8)
{
double mid=(l+r)/2;
if (bin(mid)>0)
l=mid;
else r=mid;
}
printf("%lld\n", (long long )(r+0.5));
}
return 0;
}