poj2010-Moo University-Financial Aid

来源:互联网 发布:诺基亚n9价格淘宝 编辑:程序博客网 时间:2024/05/01 10:09

     

#include <iostream>

#include <queue>

#include<functional>

using namespace std;

struct student

{

           

            int score;

            int finance;

};

struct cmpScore

{

       bool operator()(student a,student b)

       {

            return a.score<b.score;

       }

};

struct cmpFinance

{

       bool operator()(student a,student b)

       {

            return a.finance>b.finance;

       }

};

int main()

{

    int n,c,f;

    while(cin>>c>>n>>f)

    {

        int i,j,k;

        int meds;

        int num=(c-1)/2;

        int max_med=0;

        int aid;

        int medScore;

        typedef student* stu_ptr;

        stu_ptr p;

        p=new student[n];

        priority_queue<student,vector<student>,cmpScore> qScore;

        priority_queue<student,vector<student>,cmpFinance> qFinance1;

        priority_queue<student,vector<student>,cmpFinance> qFinance2;

        for(i=1;i<=n;i++)

        {

            cin>>p[i].score>>p[i].finance;

        }

        for(meds=num+1;meds<=n-num;meds++)

        {

            aid=0;

            for(i=1;i<=n;i++)

            {

                qScore.push(p[i]);

            }

            for(j=1;j<meds;j++)

            {

                qFinance1.push(qScore.top());

                qScore.pop();

            }

            aid+=qScore.top().finance;

            medScore=qScore.top().score;

            qScore.pop();

            for(j=1;j<=n-meds;j++)

            {

                qFinance2.push(qScore.top());

                qScore.pop();

            }

            while(!qScore.empty())

            {

                qScore.pop();

            }

            for(k=1;k<=num;k++)

            {

                aid+=qFinance1.top().finance;

                aid+=qFinance2.top().finance;

                qFinance1.pop();

                qFinance2.pop();

            }

            if(!qFinance1.empty())

            {

                qFinance1.pop();

            }

            if(!qFinance2.empty())

            {

                qFinance2.pop();

            }

            if(aid<=f)

            {

                max_med=medScore;

                break;

            }

        }

        if(max_med>0)

            cout<<max_med<<endl;

        else

            cout<<"-1"<<endl;

        delete []p;

    }

    return 0;

}

提交时系统显示超时,应该是效率较低的原因,并无出错。

原创粉丝点击