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;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    gitblit.cmd运行自动关闭
    用SourceTree轻松Git项目图解
    GUI for git|SourceTree|入门基础
    SourceTree的简单使用
    Windows平台使用Gitblit搭建Git服务器图文教程
    使用Gitblit 搭建Windows Git服务器
    Git使用详细教程
    Kafka 设计与原理详解
    Kafka 客户端实现逻辑分析
    js判断只能输入数字或小数点
  • 原文地址:https://www.cnblogs.com/Zforw/p/10692928.html
Copyright © 2011-2022 走看看