Leopard学霸——贪心+优先队列

题目描述

马上假期就要到了,THU的神犇Leopard假期里都不忘学霸,现在有好多门功课,每门功课都耗费他1单位时间来学习。 他的假期从0时刻开始,有1000000000个单位时间(囧rz)。在任意时刻,他都可以任意一门功课(编号1~n)来学习。 因为他在每个单位时间只能学习一门功课,而每门功课又都有一个截止日期,所以他很难完成所有n门功课。 对于第i门功课,有一个截止时间Di,若他能学完这门功课,他能够获得知识Pi。 在给定的功课和截止时间下,Leopard能够获得的知识最多为多少呢?

输入

第一行,一个整数n,表示功课的数目 接下来n行,每行两个整数,Di和Pi

输出

输出一行一个整数,表示最多学得的知识数

样例输入

3 
2 10 
1 5 
1 7 

样例输出

17

提示

样例说明:第一个单位时间学习第3个功课(1,7),然后在第二个单位时间学习第1个功课(2,10)
【数据范围】
10%数据满足:n<=25
60%数据满足:n<10000
100%数据满足:1<=n<=100000,Di、Pi<=1000000000 最后的答案可能超过32位整型。

#pragma GCC optimize (2)
#pragma G++ optimize (2)
#include <bits/stdc++.h>
#include <algorithm>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define wuyt main
typedef long long ll;
#define HEAP(...) priority_queue<__VA_ARGS__ >
#define heap(...) priority_queue<__VA_ARGS__,vector<__VA_ARGS__ >,greater<__VA_ARGS__ > >
template<class T> inline T min(T &x,const T &y){return x>y?y:x;}
template<class T> inline T max(T &x,const T &y){return x<y?y:x;}
//#define getchar()(p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 1 << 21, stdin), p1 == p2) ? EOF : *p1++)
//char buf[(1 << 21) + 1], *p1 = buf, *p2 = buf;
ll read(){ll c = getchar(),Nig = 1,x = 0;while(!isdigit(c) && c!='-')c = getchar();
if(c == '-')Nig = -1,c = getchar();
while(isdigit(c))x = ((x<<1) + (x<<3)) + (c^'0'),c = getchar();
return Nig*x;}
#define read read()
const ll inf = 1e15;
const int maxn = 1e6 + 7;
const int mod = 1e9 + 7;
int num[maxn];
map<string,ll>mp;
map<string,ll>::iterator it;
string ss[maxn];
priority_queue <int, vector <int> , greater <int> > duilie;
ll n,cnt, ans;
struct node
{
    ll x,y;
}a[100005];
bool cmp(node xx, node yy)
{
    return xx.x < yy.x;
}
int main()
{
    n=read;
    for (int i=1;i<=n;++i)
        a[i].x=read,a[i].y=read;
    sort(a+1,a+n+1,cmp);
    for(int i=1;i<=n;++i)
    {
        if (cnt<a[i].x)
        {
            cnt++;
            ans+=a[i].y;
            duilie.push(a[i].y);
        }
        else if (duilie.top()<a[i].y)
        {
            ans=ans-duilie.top()+a[i].y;
            duilie.pop();
            duilie.push(a[i].y);
        }
    }
    cout<<ans<<endl;
    return 0;
}
/**************************************************************
    Language: C++
    Result: 正确
    Time:87 ms
    Memory:39604 kb
****************************************************************/
posted @ 2020-03-15 13:34  PushyTao  阅读(171)  评论(0编辑  收藏  举报