POJ - 3111 K Best 平均值最大(01分数规划)

题目大意:有n颗珍珠,每颗珍珠有相应的价值和代价,要从中挑出K颗珍珠,使得(这k颗珍珠的总价值/这k颗珍珠的代价 )达到最大

解题思路:平均值最大,要注意精度

给出01分数规划的详细解读

< http://www.cnblogs.com/perseawe/archive/2012/05/03/01fsgh.html>

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define maxn 100010
int n, k, ans[maxn];
double Sum;
struct jewels {
    double v, w, d;
    int id;
}J[maxn];

bool cmp(const jewels a, const jewels b) {
    return a.d > b.d;
}

bool judge(double mid) {

    for(int i = 0; i < n; i++)
        J[i].d = J[i].v - mid * J[i].w;
    sort(J, J + n, cmp);
    double t = 0.0;
    for(int i = 0; i < k; i++)
        t += J[i].d;
    return t >= 0;
}

void solve() {
    double l = 0, r = 1e7 + 1;
    while(r - l > 1e-6) {
        double mid = (l + r) / 2;
        if(judge(mid))
            l = mid;
        else
            r = mid;
    }
}

int main() {
    while(scanf("%d%d", &n, &k) != EOF) {
        for(int i = 0; i < n; i++) {
            scanf("%lf%lf", &J[i].v, &J[i].w);
            J[i].id = i + 1;
        }
        solve();
        printf("%d", J[0].id);
        for(int i = 1; i < k; i++)
            printf(" %d", J[i].id);
        printf("\n");
    }
    return 0;
}
时间: 2024-08-01 02:27:22

POJ - 3111 K Best 平均值最大(01分数规划)的相关文章

『POJ 2976』Dropping tests (01分数规划)

题目链接 Descrip In a certain course, you take n tests. If you get ai out of bi questions correct on test i, your cumulative average is defined to be . Given your test scores and a positive integer k, determine how high you can make your cumulative avera

Desert King (poj 2728 最优比率生成树 0-1分数规划)

Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   Accepted: 6187 Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels

POJ 3621 Sightseeing Cows 最大密度环 01分数规划

最大密度环 01分数规划 首先的一个结论就是,不会存在环套环的问题,即最优的方案一定是一个单独的环,而不是大环套着小环的形式.这个的证明其实非常的简单,大家可以自己想一下(提示,将大环上的收益和记为x1,花费为y1,小环上的为x2,y2.重叠部分的花费为S.表示出来分类讨论即可).有了这个结论,我们就可以将花费和收益都转移到边上来了,因为答案最终一定是一个环,所以我们将每一条边的收益规定为其终点的收益,这样一个环上所有的花费和收益都能够被正确的统计. 解决了蛋疼的问题之后,就是01分数规划的部分

poj 3111 K Best (二分搜索之最大化平均值之01分数规划)

Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband John got broke after recent financial crises, Demy has decided to sell some jewels. She has decided that she would keep k best jewels for herself. Sh

POJ 3111 K Best &amp;&amp;NYOJ 914 (二分+ 贪心,最大化平均值)

链接:NYOJ:click here, POJ:click here 题意:(最大化平均值,挑战编程P143) 有n个物品的重量和价值分别是w[i]和v[i],从中选出K个物品使得单位重量的价值最大.(1<=k<=n<=10^41<=w[i],v[i]<=10^6) 一般想到的是按单位价值对物品排序,然后贪心选取,但是这个方法是错误的,比如对nyoj的例题来说,从大到小地进行选取,输入的结果是5/7=0.714对于有样例不满足.我们一般用二分搜索来做(其实这就是一个01分数规

poj 3111 K Best 最大化平均值 二分思想

poj 3111 K Best 最大化平均值 二分思想 题目链接: http://poj.org/problem?id=3111 思路: 挑战程序竞赛书上讲的很好,下面的解释也基本来源于此书 设定条件C(x):=可以选择使得单位重量的价值不小于x 如何判定C(x)是否可行 假设选了某个物品的集合是S,那么单位重量的价值是:\[ \sum\limits_{i \in S} {v_i } /\sum\limits_{i \in S} {w_i } \] 因此就变成了判断是否存在S满足下面的条件:\[

POJ 2728 Desert King (最优比率生成树---01分数规划)

题目地址:POJ 2728 01分数规划的应用之一-最优比率生成树. 跟普通的01分数规划类似,只是这题的验证函数改成了最小生成树来验证.弱用的迭代法. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map>

POJ 3621 Sightseeing Cows 【01分数规划+spfa判正环】

题目链接:http://poj.org/problem?id=3621 Sightseeing Cows Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11526   Accepted: 3930 Description Farmer John has decided to reward his cows for their hard work by taking them on a tour of the big ci

POJ - 2976 Dropping tests (01分数规划)

终于把01分数规划这个坑填上了. 题意是有二维平面上的n个向量$(a_i,b_i)$,让你选择其中的m个,使得这些向量和的斜率,即$\frac{\sum a_i}{\sum b_i}$最小. 二分斜率,设$\frac{\sum a_i}{\sum b_i}\geqslant k$,即$\sum a_i\geqslant \sum kb_i$,即$\sum a_i-kb_i\geqslant 0$,选择$a_i-kb_i$前m大的向量判断是否大于0即可. 每次交POJ都得和CE刚上一番... 1