poj 1827 A Bunch Of Monsters 贪心(并查集优化)

Description

Background  
Jim is a brave explorer. One day, he set out for his next destination, a mysterious hill. When he arrived at the foot of the hill, he was told that there were a bunch of monsters living in that hill, and was dissuaded from continuing his trip by the residents near the hill. Nevertheless, our Jim was so brave that he would never think of giving up his exploration.  
The monsters do exist! When he got into that hill, he was caught by a bunch of fearful monsters.  
Fortunately, the monsters didn’t plan to kill him or eat him for they were planning a big party. They wanted to invite Jim, a clever human being, to their party, in order to let human beings know that the monsters also have wonderful parties.  
Problem  
At the end of the party, the monsters promised that, after the last game, they would set Jim free. The game is described as follow:  
1. There are a great many boxes of treasure, which are numbered from 1 to X. One box has the only one number; one number can only appear on one box. Furthermore, we can assume that X is INFINITY, because the monsters have got a lot of treasure from the men they caught.  
2. There are N monsters in this game. Each picks up a card randomly. After that, he / she (it?) opens it, getting a positive integer number d[i], and cannot change it or pick up another card again. The range of d[i] is from 1 to M. If the i-th monster get the number d[i], he can only get the treasure box numbered equal to or less than d[i]. What’s more, one box only can be distributed to one monster; one monster can only get one box.  
3. Of course, there are many ways to distribute the boxes to the monsters when N monsters get their numbers; and not every monster can get a box in many cases. Jim has the right to make the arrangement; however, he also knows that the monsters that don’t get the boxes will also punish him.  
Jim knows the strength of the N monsters. The i-th one has the strength s[i]. We call the sum of strength s[i] of all the monsters that don’t get the boxes --- the DAMAGE to Jim. Your task is to help Jim find out the minimum DAMAGE to him.  

Input

The input consists of several test cases. In the first line of each test case, there are two positive integers N and M (1<=N<=50000, 1<=M<=50000), indicating the number of monsters and the range of numbers the monsters possibly get on the cards. Then there are N integers d[i] (1<=d[i]<=M) in the following lines, which are the numbers those monsters got. And in the rest lines of one test case, there are other N positive integers s[i] (1<=s[i]<=20000), indicating the strength of each monsters. The test case starting with 2 zeros is the final test case and has no output.

Output

For each test case, print your answer, the minimum DAMAGE, in one line without any redundant spaces.

Sample Input

1 1
1
1
7 7
6 4 4 2 3 4 3
10 70 20 60 30 50 40
0 0

Sample Output

0
50
题意及分析:
n个怪,每个怪有一个号码d[i],伤害值为s[i],怪需要分配一个箱子(分配箱子的编号<=d[i]),箱子编号为1-INF。一个怪一个箱子,一个箱子只分配一次。
贪心的思想在这里:1,怪的伤害越高,就应该首先被满足。2,如果有足够的箱子,那么第i的怪获得的箱子就得是d[i](这样才能尽可能的利用),如果这个箱子已经被分配,就寻找编号小的箱子。所以按怪物的伤害排序就好了。
未优化的的代码如下:
 
 
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
         #include 
         
           #include 
          
            #include 
           
             #define INF 0x3f3f3f3f using namespace std; struct monster { int d; int s; }a[50000+10]; int vis[50010]; int cmp(monster a,monster b) { if(a.s==b.s) return a.d>b.d; //这个排序其实无所谓,具体细节可以自己想想,稍微分个类就清楚了。 return a.s>b.s; } int main() { int m,n,i,j; while(scanf("%d%d",&n,&m),n||m) { for(i=1;i<=n;i++) scanf("%d",&a[i].d); for(i=1;i<=n;i++) scanf("%d",&a[i].s); sort(a+1,a+n+1,cmp); memset(vis,0,sizeof(vis)); //标记箱子是否被使用 long long ans=0; for(i=1;i<=n;i++) { int k=a[i].d; if(!vis[k]) //未被使用 vis[k]=1; else //否则往前面找,直到找到合适的编号最大的 { int flag=0; for(j=k-1;j>0;j--) { if(!vis[j]) { vis[j]=1; flag=1; break; } } if(!flag) ans+=a[i].s; } } printf("%I64d\n",ans); } return 0; } 
            
           
         
       
       
      
      
     
     
    
    
贪心的思想没变,利用并查集优化其实是利用里面找根节点的函数。根节点的数值(也就是fun(d[i])代表d[i]最近的可以利用的那个编号。这样每次就可以直接找到现行的可以利用的箱子,而不是用for循环一个一个的往前找。
代码如下:
 
 
#include
    
    
     
     
#include
     
     
      
      
#include
      
      
       
       
#include
       
       
        
        
#include
        
         #include 
         
           #include 
          
            #include 
           
             #define INF 0x3f3f3f3f using namespace std; typedef long long ll; struct monster { int d; int s; }a[50010]; int root[50010]; int cmp(monster m1,monster m2) { return m1.s>m2.s; } int fun(int x) { int t=x; while(x!=root[x]) x=root[x]; while(t!=root[t]) { t=root[t]; root[t]=x; } return x; } void mer(int a,int b) { int ra=fun(a); int rb=fun(b); root[rb]=ra; } int main() { int m,n,i,j; while(scanf("%d%d",&n,&m),n||m) { for(i=1;i<=n;i++) scanf("%d",&a[i].d); for(i=1;i<=n;i++) scanf("%d",&a[i].s); sort(a+1,a+n+1,cmp); for(i=0;i<=m;i++) root[i]=i; ll ans=0; for(i=1;i<=n;i++) { int k=fun(a[i].d); if(k==0) //为0的话,说明没有可以选择的箱子。 ans+=a[i].s; else mer(k-1,k); //否则的话,就利用可以选择地箱子(它的根节点),并更新根节点为下一个编号的根节点 } printf("%I64d\n",ans); } return 0; } 
            
           
         
       
       
      
      
     
     
    
    
 
 
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值