zoukankan      html  css  js  c++  java
  • Codeforces 735B

    735B - Urbanization

    思路:贪心。人数少的城市住钱最多的那几个人。

    不证明了,举个例子吧:a1<a2<a3<a4<a5

    (a1+a2+a3)/3+(a4+a5)/2==(2*a1+2*a2+2*a3+3*a4+3*a5)/6①

    (a1+a2)/2+(a3+a4+a5)/3==(3*a1+3*a2+2*a3+2*a4+2*a5)/6    ②

    因为a4+a5>a1+a2所以①式大于②式,同理可证其他方案都比①式小。

    本质就是分母通分后都一样,在分子中把最大的分母和最大的钱数相乘,使得最大的钱数的权值也最大。

    代码:

    #include<bits/stdc++.h>
    using namespace std; 
    #define ll long long
    const int N=1e5+5;
    int a[N];
    bool cmp(int &a,int &b)
    {
        return a>b;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
        int n,n1,n2;
        cin>>n>>n1>>n2;
        for(int i=0;i<n;i++)cin>>a[i];
        
        sort(a,a+n,cmp);
        if(n1>n2)swap(n1,n2);
        int c=0,t1=n1,t2=n2;
        double sum1=0,sum2=0;
        while(n1)
        {
            sum1+=a[c];
            c++;
            n1--;
        }
        while(n2)
        {
            sum2+=a[c];
            c++;
            n2--;
        }
        cout<<fixed<<setprecision(8)<<sum1/t1+sum2/t2<<endl;
        return 0;
    }
  • 相关阅读:
    poj3253Fence Repair (Huffman)
    poi3617Best Cow Line ——贪心法
    高级排序之——归并排序
    Aizu
    初级排序——冒泡排序
    cookie会话
    加载web资源文件
    servlet
    Http

  • 原文地址:https://www.cnblogs.com/widsom/p/7340426.html
Copyright © 2011-2022 走看看