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;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    【LInux】查看Linux系统版本信息
    【Linux】常用命令,持续更新
    【Linux】rpm常用命令及rpm参数介绍
    【CentOS】设置服务开机自动启动
    查看所使用的Linux系统是32位还是64 位的方法
    spring中@param和mybatis中@param使用区别
    Linux下Mycat安装配置和使用
    CentOS 7下MySQL服务启动失败的解决思路
    java的排序算法
    File 操作
  • 原文地址:https://www.cnblogs.com/Zforw/p/10692928.html
Copyright © 2011-2022 走看看