SSLOJ 1394 Leopard学霸[study]
Description–
马上假期就要到了,THU的神犇Leopard假期里都不忘学霸,现在有好多门功课,每门功课都耗费他1单位时间来学习。
他的假期从0时刻开始,有1000000000个单位时间(囧rz)。在任意时刻,他都可以任意一门功课(编号1~n)来学习。
因为他在每个单位时间只能学习一门功课,而每门功课又都有一个截止日期,所以他很难完成所有n门功课。
对于第i门功课,有一个截止时间Di,若他能学完这门功课,他能够获得知识Pi。
在给定的功课和截止时间下,Leopard能够获得的知识最多为多少呢?
Input–
第一行,一个整数n,表示功课的数目
接下来n行,每行两个整数,Di和Pi
Output–
输出一行一个整数,表示最多学得的知识数
Sample Input–
3
2 10
1 5
1 7
Sample Output–
17
说明–
【样例说明】
第一个单位时间学习第3个功课(1,7),然后在第二个单位时间学习第1个功课(2,10)
【数据范围限制】
10% n<=25
60% n<10000
100% 1<=n<=100000,Di、Pi<=1000000000
最后的答案可(bu)能(hui)超过32位整型
代码–
#include<algorithm>
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
priority_queue <int, vector <int> , greater <int> > d;
int n, t, ans;
struct ooo
{
int x, y;
}a[100005];
bool cmp(ooo xx, ooo yy)
{
return xx.x < yy.x;
}
int main()
{
scanf("%d", &n);
for (int i = 1; i <= n; ++i)
scanf("%d%d", &a[i].x,&a[i].y);
sort(a + 1, a + n + 1, cmp);//按截止时间从小到大排序
for (int i = 1; i <= n; ++i)
if (t < a[i].x)
{
t++, ans += a[i].y;
d.push(a[i].y);
}
else if (d.top() < a[i].y)
{
ans = ans - d.top() + a[i].y;//把最小知识数的功课踢掉
d.pop(), d.push(a[i].y);
}
printf("%d", ans);
return 0;
}