zoukankan      html  css  js  c++  java
  • FatMouse' Trade

    /*
    problem: FatMouse' Trade
    this is greedy problem.
    firstly:we should calculate the average  J[i]/F[i] and sort it
    secondly: according to the m and choose the most high ones
    */
    #include<iostream>
    #include<vector>
    #include<algorithm>
    using namespace std;
    
    struct cell
    {
        double j;
        double f;
        double rate;
    };
    bool cmp(cell x,cell y)
    {
        return x.rate > y.rate;
    }
    int main()
    {
        int m,n;
        cell temp;
        vector<cell> vec;
        while (scanf("%d%d", &m, &n) != EOF)
        {
            if ((m == -1)&&(n == -1))
                break;
            double count = 0.0;
            while (n--)
            {
                cin>>temp.j>>temp.f;
                if (temp.f != 0)
                {
                    temp.rate = (double)temp.j/temp.f;
                    vec.push_back(temp);
                }
                else
                {
                    count += temp.j;
                }
    
            }
            sort(vec.begin(), vec.end(), cmp);
            for (int i = 0; (i < vec.size())&&(m > 0); i++)
            {
                if (m-vec[i].f >= 0)
                {
                    count += vec[i].j;
                    m -= vec[i].f;
                }
                else
                {
                    //count += (double)((double)(m/vec[i].f))*vec[i].j;
                    double temp1 = (double)m/vec[i].f;
                    count += temp1*vec[i].j;
                    m = 0;
                }
            }
            vec.clear();
            printf("%.3f
    ",count);
        }
    }
    /*此题除了要满足例子以外,还要满足一些条件才能真正算ac:
    0 1
    1 0
    1.000
    
    1 0
    0.000
    
    5 4
    10000 5
    2000 2
    100 0
    300 0
    10400.000
    
    数据类型用double,就这样*/
  • 相关阅读:
    POJ2774 Long Long Message
    Lyndon Word相关
    后缀自动机(SAM)
    后缀数组(SA)
    [THUSC2016]补退选
    [HNOI2008]GT考试
    CF1080E Sonya and Matrix Beauty
    [JSOI2008]火星人
    两道FFT题目略解
    网络流概念+EdmondKarp算法+Dinic(Dinitz)
  • 原文地址:https://www.cnblogs.com/maverick-fu/p/3961680.html
Copyright © 2011-2022 走看看