• 1257 背包问题 V3——分数规划


    N个物品的体积为W1,W2......Wn(Wi为整数),与之相对应的价值为P1,P2......Pn(Pi为整数),从中选出K件物品(K <= N),使得单位体积的价值最大。
    Input
    第1行:包括2个数N, K(1 <= K <= N <= 50000)
    第2 - N + 1行:每行2个数Wi, Pi(1 <= Wi, Pi <= 50000)
    Output
    输出单位体积的价值(用约分后的分数表示)。
    Input示例
    3 2
    2 2
    5 3
    2 1
    Output示例
    3/4
    ————————————————————————————
    第一眼看题目以为是贪心QAQ 后来发现不行
    因为如果你现在已有的价值/体积是最佳
    而现在有两个价值很小的物品 a b a价值比b大
    但是a的体积远大于b的话 此时b肯定是更优的
    所以正解应该是二分答案 判断是否合法就好辣
    #include<cstdio>
    #include<cstring>
    #include<algorithm>
    #define LL long long
    using namespace std;
    const int M=50007;
    int read(){
        int ans=0,f=1,c=getchar();
        while(c<'0'||c>'9'){if(c=='-') f=-1; c=getchar();}
        while(c>='0'&&c<='9'){ans=ans*10+(c-'0'); c=getchar();}
        return ans*f;
    }
    int n,k;
    int c[M],w[M];
    LL sumx,sumy,ansx,ansy;
    LL gcd(LL x,LL y){
        while(y){
            LL p=x%y;
            x=y;
            y=p;
        } 
        return x;
    }
    struct node{double v; int pos;}e[M];
    bool cmp(node a,node b){return a.v-b.v>1e-6;}
    bool check(double mid){
        for(int i=1;i<=n;i++) e[i].v=1.0*w[i]-1.0*c[i]*mid,e[i].pos=i; 
        sort(e+1,e+1+n,cmp);
        double sum=0; sumx=0; sumy=0;
        for(int i=1;i<=k;i++){
            sum+=e[i].v;
            sumx+=w[e[i].pos];
            sumy+=c[e[i].pos];
        }
        return sum>=0;
    }
    int main()
    {
        n=read(); k=read();
        for(int i=1;i<=n;i++) c[i]=read(),w[i]=read();
        double l=0,r=50000;
        while(r-l>1e-6){
            double mid=(l+r)/2;
            if(check(mid)) l=mid,ansx=sumx,ansy=sumy;
            else r=mid;
        }
        LL d=gcd(ansx,ansy);
        printf("%lld/%lld
    ",ansx/d,ansy/d);
        return 0;
    }
    View Code
  • 相关阅读:
    BZOJ1912 [Apio2010]patrol 巡逻
    BZOJ2432 [Noi2011]兔农
    BZOJ1010 [HNOI2008]玩具装箱toy
    BZOJ3240 [Noi2013]矩阵游戏
    洛谷【P1303】A*B Problem
    洛谷【2142】高精度减法
    洛谷【P1100】高低位交换
    OIer应该知道的二进制知识
    洛谷【P3908】异或之和
    洛谷【P2431】正妹吃月饼
  • 原文地址:https://www.cnblogs.com/lyzuikeai/p/7395789.html
Copyright © 2020-2023  润新知