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;
    }
    
    岂能尽如人意,但求无愧我心
  • 相关阅读:
    洛谷 P1092 虫食算
    2018.3.25校内互测
    洛谷 P2915 [USACO08NOV]奶牛混合起来Mixed Up Cows
    洛谷 P1879 [USACO06NOV]玉米田Corn Fields
    洛谷 P3052 [USACO12MAR]摩天大楼里的奶牛Cows in a Skyscraper
    ZJOI Day 2 游记
    editorial-render A
    BZOJ2904
    BZOJ 1600
    构造脚本语言
  • 原文地址:https://www.cnblogs.com/Zforw/p/10692928.html
Copyright © 2011-2022 走看看