• POJ-3111 K Best


    题目链接

    https://vjudge.net/problem/POJ-3111

    题面

    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. She decided to keep such jewels that their specific value is as large as possible. That is, denote the specific value of some set of jewels S = ({i_1, i_2, …, i_k}) as

    [s(S)=frac{sum_limits{j=1}^kv_{i_j}}{sum_limits{j=1}^kw_{i_j}} ]

    Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so.

    Input

    The first line of the input file contains n — the number of jewels Demy got, and k— the number of jewels she would like to keep (1 ≤ kn ≤ 100 000).

    The following n lines contain two integer numbers each — vi and wi (0 ≤ vi(10^6), 1 ≤ wi(10^6), both the sum of all vi and the sum of all wi do not exceed (10^7)).

    Output

    Output k numbers — the numbers of jewels Demy must keep. If there are several solutions, output any one.

    Sample Input

    3 2
    1 1
    1 2
    1 3
    

    Sample Output

    1 2
    

    题意

    给定n个数对((v_i,w_i)),必须选择k个数,问他们的s最小是多少,s的定义见题面

    题解

    这题不太容易想到是二分,但其实这是个二分答案的题,我们先二分一个s,那么我们可以化简一下下面这个式子

    [s(S)=frac{sum_limits{j=1}^kv_{i_j}}{sum_limits{j=1}^kw_{i_j}} ]

    变成

    [s(S) imes sum_limits{j=1}^kw_{i_j}=sum_limits{j=1}^kv_{i_j} ]

    那么对于(i_1...i_k)都可以得到一个(v_{i_j}-s(S) imes w_{i_J}),显然s(S)越大这个值越小,那么我们就找出我们把这个值存进一个数组中从大到小排一下序,选取k个,如果能大于0的话说明s(S)可以更大,小于零则不行,实数二分一下即可,最后输出选取的k个数即可

    AC代码

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cmath>
    #include <cstdlib>
    #include <cstring>
    #define N 100050
    #define eps 1e-8
    using namespace std;
    typedef long long ll;
    int n, k;
    struct node {
    	double v, w, tmp;
    	int id;
    } a[N];
    bool cmp(node a, node b) {
    	return a.tmp > b.tmp;
    }
    bool check(double x) {
    	for (int i = 1; i <= n; i++) {
    		a[i].tmp = a[i].v - a[i].w * x;
    	}
    	sort(a + 1, a + n + 1, cmp);
    	double nowsum = 0;
    	for (int i = 1; i <= k; i++) {
    		nowsum += a[i].tmp;
    	}
    	return nowsum >= 0;
    }
    int main() {
    	scanf("%d%d", &n, &k);
    	for (int i = 1; i <= n; i++) {
    		scanf("%lf%lf", &a[i].v, &a[i].w);
    		a[i].id = i;
    	}
    	double l = 0, r = 1e6;
    	double mid;
    	while(r - l > eps) {
    		mid = (l + r) / 2;
    		if (check(mid)) {
    			l = mid;
    		}
    		else r = mid;
    	}
    	for (int i = 1; i <= k; i++) {
    		printf("%d ", a[i].id);
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    linux内核编译步骤
    Linux strace命令
    通过Wifi调试Android应用
    [Java 7][msvcr100.dll] Error when load Eclipse
    Cobar 关系型数据的分布式处理系统
    升级SUSE Linux内核的完整步骤!
    Qt 5.7 亮瞎眼的更新
    QT5.11下载与安装教程
    Delphi 对象模型学习笔记(转)
    内存共享【Delphi版】
  • 原文地址:https://www.cnblogs.com/artoriax/p/10375709.html
Copyright © 2020-2023  润新知