mirror of
https://github.com/Kiritow/OJ-Problems-Source.git
synced 2024-03-22 13:11:29 +08:00
Another way of NYOJ 208
This commit is contained in:
parent
2f77163d5c
commit
48d215f576
68
NYOJ/208_2.cpp
Normal file
68
NYOJ/208_2.cpp
Normal file
|
@ -0,0 +1,68 @@
|
|||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <algorithm>
|
||||
using namespace std;
|
||||
#define MAXN 10005
|
||||
|
||||
struct pack
|
||||
{
|
||||
int profit,deadline;
|
||||
};
|
||||
|
||||
pack p[MAXN];
|
||||
|
||||
int father[MAXN];
|
||||
|
||||
bool cmp(const pack& a,const pack& b)
|
||||
{
|
||||
return a.profit>b.profit;
|
||||
}
|
||||
|
||||
int GetFather(int pos)
|
||||
{
|
||||
if(father[pos]==-1)
|
||||
{
|
||||
/// This pos is itself's father
|
||||
return pos;
|
||||
}
|
||||
/// Else , this pos has a father. So it should know who is his Eldest Father.
|
||||
father[pos]=GetFather(father[pos]);
|
||||
/// And then , report back to it's caller.
|
||||
return father[pos];
|
||||
}
|
||||
|
||||
int main()
|
||||
{
|
||||
int t;
|
||||
while(scanf("%d",&t)==1)
|
||||
{
|
||||
/// Reset father
|
||||
/** This way caused TLE.
|
||||
for(int i=0;i<t;i++)
|
||||
{
|
||||
father[i]=i;
|
||||
}
|
||||
*/
|
||||
memset(father,-1,sizeof(int)*MAXN);
|
||||
for(int i=0;i<t;i++)
|
||||
{
|
||||
scanf("%d %d",&p[i].profit,&p[i].deadline);
|
||||
}
|
||||
sort(p,p+t,cmp);
|
||||
int sum=0;
|
||||
for(int i=0;i<t;i++)
|
||||
{
|
||||
int ans=GetFather(p[i].deadline);
|
||||
if(ans>0)
|
||||
{
|
||||
/// This pos's father is not 0 : which means it's possible to sell this thing.
|
||||
sum+=p[i].profit;
|
||||
/// And This pos become a son of the pos on its left.
|
||||
father[ans]=GetFather(ans-1);
|
||||
}
|
||||
}
|
||||
printf("%d\n",sum);
|
||||
}
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue
Block a user