Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

c++ 代码 O(nlogn)

Posted by 201300301324 at 2015-03-19 20:57:04 on Problem 2431
#include <cstdio>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
struct st
{
    int d,f;

} s[10005];
struct cmp
{
    bool operator ()(st x, st y)
    {
        return x.f < y.f;//小的优先级高
    };
};
bool cmp_1(st a,st b)
{
    return a.d<b.d;
}
int n,L,P;
int main()
{
    scanf("%d",&n);
    for(int i=0; i<n; i++)
    {
        scanf("%d%d",&s[i].d,&s[i].f);
    }

    scanf("%d%d",&L,&P);
    priority_queue<st,vector<st>,cmp> q;
    for(int i=0; i<n; i++)
    {

        s[i].d=L-s[i].d;
    }
    sort(s,s+n,cmp_1);
    int i=0;
    int count=0;
    while(P<L)
    {
        for(;s[i].d<=P&&i<n;i++)
        {
            q.push(s[i]);
        }
        if(q.empty())
        {
            printf("-1\n");
            return 0;
        }
        P+=q.top().f;
        q.pop();
        //printf("%d ",P);
        count++;
    }
    printf("%d",count);
}

Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator