zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    双元素非递增(容斥)--Number Of Permutations Educational Codeforces Round 71 (Rated for Div. 2)
    两两内积为0(牛客多校第七场)-- CDMA
    十进制快速幂(牛客多校第五场)-- generator 1
    分层最短路(牛客第四场)-- free
    %300为0的个数(牛客第四场)-- number
    3的倍数 或运算构造x(牛客第四场)-- triples I
    笛卡尔树--牛客第四场(sequence)
    线性基求交(线段树)--牛客第四场(xor)
    最短meeting路线(树的直径)--牛客第四场(meeting)
    最长相同01数的子串(map搞搞)--牛客第三场 -- Crazy Binary String
  • 原文地址:https://www.cnblogs.com/artoriax/p/10375709.html
Copyright © 2011-2022 走看看