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;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    Fast RCNN 训练自己数据集 (2修改数据读取接口)
    Caffe Python MemoryDataLayer Segmentation Fault
    Caffe 单独测试添加的layer
    Caffe源码解析7:Pooling_Layer
    Caffe源码解析6:Neuron_Layer
    Caffe源码解析5:Conv_Layer
    Caffe源码解析4: Data_layer
    Caffe源码解析3:Layer
    Caffe源码解析2:SycedMem
    Raft论文学习笔记
  • 原文地址:https://www.cnblogs.com/Zforw/p/10692928.html
Copyright © 2011-2022 走看看