開始想按照獎勵大小排序,然後先幹獎勵多的,然後把獎勵多的放到對應的底限天數上。用一個map標記第幾天已經被用了第幾天沒被用。比如最大的是4 70,那麼就把第四天標記爲1,意思是第四天已經被用了。隨後再找獎勵次大的,如果當天沒被佔用,就放;佔用了,就往前找,直到找到前邊的某一個沒有被佔用的天。
但是tle了。。。
後來看了學姐的代碼,恍然大悟。原來可以這麼簡單。。。
因爲這裏只關心獎勵爲哪些的數被幹了,哪些沒被幹,並不關注幹這些工作的天分別是哪天。所以用一個優先級隊列保存被幹的工作的獎勵數。先把結構體按照時間排序,從第一件工作(也就是底限天數最少的工作)開始找。畢竟普通的思想就是每一件工作都在deadline這天完成,這樣做的工作還多一些。沒有超期,就放進隊列,超期了,就把隊列裏邊拿出一個獎勵數最小的,看看哪個大,如果發現新的超期的這個大,那原先在隊列裏那個就白白了~您雖然底限很早但是獎勵太少了,不幹了。有這時間我寧願幹一個時間要求既寬鬆獎勵又高的(因爲時間寬鬆的在哪天干的自由很大,只要不超底線,在底限之前的天裏都可以做它)。
#include <stdio.h>
#include <stdlib.h>
#include <map>
#include <queue>
#include <algorithm>
using namespace std;
struct no
{
int t, v;
}node[30005];
/*map <int, int> m;
int check(int t)
{
if(t <= 0)
return 0;
if(m[t] == 0)
return t;
return check(t-1);
}
int cmp(no a, no b)
{
return a.v>b.v;
}
int main()
{
int n, i, x;
long long sum;
while(~scanf("%d", &n))
{
m.clear();
for(i = 1 ; i <= n ; i++)
{
scanf("%d %d", &node[i].t, &node[i].v);
if(node[i].t > n)
node[i].t = n;
}
sort(node+1, node+n+1, cmp);
sum = 0;
for(i = 1 ; i <= n ; i++)
{
x = check(node[i].t);
if(x > 0)
{
m[x] = 1;
sum += node[i].v;
}
}
printf("%lld\n", sum);
}
return 0;
}*/
struct cmp1
{
int operator() (int &a, int &b)
{
return a>b;
}
};
priority_queue<int, vector<int>, cmp1> q;
int cmp2(no a, no b)
{
if(a.t != b.t)
return a.t < b.t;
return a.v > b.v;
}
int main()
{
int n, i, ti;
long long sum;
while(~scanf("%d", &n))
{
for(i = 0 ; i < n ; i++)
scanf("%d %d", &node[i].t, &node[i].v);
sort(node, node+n, cmp2);
q.push(node[0].v);
ti = 2;
for(i = 1 ; i < n ; i++)
{
//printf("%d %d %d\n", ti, node[i].t, node[i].v);
if(ti <= node[i].t)
{
q.push(node[i].v);
ti++;
}
else
{
if(q.top() < node[i].v)
{
q.pop();
q.push(node[i].v);
}
}
}
sum = 0;
while(!q.empty())
{
sum += q.top();
q.pop();
}
printf("%lld\n", sum);
}
return 0;
}