码迷,mamicode.com
首页 > 其他好文 > 详细

poj2976_01分数规划

时间:2014-11-20 23:20:56      阅读:407      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

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

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

思路:01分数规划

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

代码:

bubuko.com,布布扣
#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分数规划

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://www.cnblogs.com/sayeter/p/4111558.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!