题目记录6

题目:http://vj.bit-studio.cn/contest/209424#problem/E

方法:贪心,有一个巨坑,int x=2,y=3;double z=y/x;y居然会等于1然后搞得我一度怀疑sort有问题,

代码:

#include<iostream>
#include<stdio.h>
#include<map>
#include<algorithm>
using namespace std;
typedef struct mao
{
    int j;
    int f;
    double cm;
}mm;
bool compare(mm f1, mm f2)
{
    return f1.cm>f2.cm;
}
int main()
{
    int m, n;
    while ((cin >> m >> n) && m != -1&& n != -1)
    {
        mm *p = new mm[n];
        for (int i = 0; i < n; i++)
        {
            cin >> p[i].j >>p[i].f;
            double x = p[i].j, y = p[i].f;
            p[i].cm = x / y;
            //cout << p[i].cm << endl;
        }
        sort(p,p+n,compare);
        //for (int i = 0; i < n; i++)
        //{
            //for (int j = i + 1; j < n; j++)
            //{
                //if (p[i].cm < p[j].cm)swap(p[i], p[j]);
            //}
        //}
        double mt=m, nt=0;
        //for(int i=0;i<n;i++)
            //cout << p[i].j << "  " << p[i].f << endl;;
        for (int i = 0; i < n; i++)
        {
            mt -= p[i].f;
            nt += p[i].j;
            if (mt < 0)
            {
                nt -= p[i].j;
                double temp = (mt + p[i].f) / p[i].f*p[i].j;
                nt += temp;
                break;
            }
        }
        printf("%.3f
", nt);
    }
}
原文地址:https://www.cnblogs.com/miaos/p/miaoz-1-14.html