首页 > 代码库 > poj2976_01分数规划

poj2976_01分数规划

题目链接:http://poj.org/problem?id=2976

题意:给定A数组和B数组,从中选n-k对数,使得题目里给的表达式的值最小

思路:01分数规划

参考链接:http://blog.csdn.net/hhaile/article/details/8883652

代码:

#include<cstdio>#include<iostream>#include<cstring>#include<algorithm>#include<cstdlib>using namespace std;const int maxn = 1e6+10;double eps = 1e-6;struct node {    double u, v;} b[maxn];int n, m, k;double xx, ans;bool cmp(node a, node b) {    return a.u-xx*a.v>b.u-xx*b.v;}double check(double x) {    xx = x;    ans = 0;    nth_element(b, b+k-1, b+n, cmp);    for(int i=0; i<k; i++) {        ans += b[i].u-xx*b[i].v;    }    return ans;}int main() {    //freopen("test.txt", "r", stdin);    while(scanf("%d %d", &n, &m)!=EOF) {        if(n==0 && m==0) break;        k = n-m;        for(int i=0; i<n; i++) {            scanf("%lf", &b[i].u);        }        for(int i=0; i<n; i++) {            scanf("%lf", &b[i].v);        }        double l=0, r=1e7, mid;        while(l<r) {            double mid=(l+r)/2;            if(check(mid)<-eps)                r=mid-eps;            else                l=mid+eps;        }        printf("%.0lf\n", 100*xx);    }    return 0;}
View Code

 

poj2976_01分数规划