zoukankan      html  css  js  c++  java
  • POJ3111 K Best

    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 = {i1, i2, …, ik} as

    [ s(S) = frac{sum_{j=1}^k v_{ij}}{sum_{j=1}^kw_{ij}}$$. Demy would like to select such k jewels that their specific value is maximal possible. Help her to do so. ## Analysis: 不妨令 $$ frac{sum_{j=1}^k v_{ij}}{sum_{j=1}^kw_{ij}} geq x ]

    [sum_{j=1}^k v_{ij} geq x sum_{j=1}^kw_{ij} ]

    [sum_{j=1}^k v_{ij} - x sum_{j=1}^kw_{ij} geq 0 ]

    只需要判断二分的每个k是否满足 ≥ 0即可。

    Code

    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #define N 100001
    #define INF 0x3f3f3f3f
    using namespace std;
    const double EPS = 0.00001;//精度
    int v[N],w[N],ans[N],n,k;
    struct Node{
    	int id;
    	double val;
    	bool operator < (const Node& x) const {
    		return val > x.val;
    	}
    }f[N];
    bool C(double x){
    	for(int i = 0;i < n;++i){
    		f[i].val = v[i] - w[i]*x;
    		f[i].id = i + 1;
    	}
    	sort(f,f + n);
    	double sum = 0;
    	for(int i = 0;i < k;++i){
    		sum += f[i].val;
    		ans[i] = f[i].id;
    	}
    	return sum >= 0;
    }
    void solve(){
    	double lb = 0,ub = INF;
    	while(lb + EPS < ub){
    		double mid = (lb + ub)/2;
    		if(C(mid)) lb = mid;
    		else ub = mid;
    	}
    	for(int i = 0;i < k;++i){
    		printf("%d ",f[i].id);
    	}
    }
    int main(){
    	scanf("%d%d",&n,&k);
    	for(int i = 0;i < n;++i){
    		scanf("%d %d",&v[i],&w[i]);
    	}
    	solve();
    	return 0;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    Swift -- Swfit 笔记
    web -- CSS 图片宽高不固定的垂直居中方法
    web -- Angularjs 笔记2
    web -- Angularjs 笔记
    web -- Angularjs 备忘录应用
    Swift -- swift 函数代码
    Swift -- 创建空数组和空字典
    Linux -- FresBSD的镜像文件说明
    Linux -- ubuntu下安装程序的三种方法
    Linux -- Ubuntu 命令2
  • 原文地址:https://www.cnblogs.com/Zforw/p/10692928.html
Copyright © 2011-2022 走看看