自创数组模拟指针 2645: Working in JLU

来源:互联网 发布:心理医生在线解答软件 编辑:程序博客网 时间:2024/05/16 19:51

#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
struct t
{
 int p,d;
}s[100001];
int f[100001];
bool cmp(t a,t b)
{
 return (a.p>b.p);
}
int main()
{
 int n,i,j;
 long long max;
 while(scanf("%d",&n)!=EOF)
 {
        for(i=1;i<=100000;i++)  f[i]=i;
        max=0;
  for(i=0;i<n;i++) scanf("%d%d",&s[i].p,&s[i].d);
        sort(s,s+n,cmp);
  for(i=0;i<n;i++)
  {
   bool flag=false;
   if(f[s[i].d]==s[i].d)
            {
               flag=true;//找到
               f[s[i].d]=s[i].d-1;;
            }
   else
   {
            int temp=f[f[s[i].d]];    
   while(!flag&&temp>0)
   {
               temp=f[f[s[i].d]];                  
               if(f[temp]==temp&&f[temp]>0)
               {
           flag=true;
                }
                if(temp>1&&flag)
                f[temp]=f[f[temp-1]];
                else
                if(flag)
                f[temp]=0;
                if(!flag) f[f[s[i].d]]=f[temp];
   }
            }
   if(flag) max+=s[i].p;
  }
  printf("%lld/n",max);
 }
 return 0;
}

原创粉丝点击