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](这样才能尽可能的利用),如果这个箱子已经被分配,就寻找编号小的箱子。所以按怪物的伤害排序就好了。
未优化的的代码如下:

贪心的思想没变,利用并查集优化其实是利用里面找根节点的函数。根节点的数值(也就是fun(d[i])代表d[i]最近的可以利用的那个编号。这样每次就可以直接找到现行的可以利用的箱子,而不是用for循环一个一个的往前找。
代码如下:

 
 

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

时间: 2024-10-13 17:28:31

poj 1827 A Bunch Of Monsters 贪心(并查集优化)的相关文章

POJ 1456——Supermarket——————【贪心+并查集优化】

Supermarket Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1456 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx

(贪心 + 并查集优化) poj 1456

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9452   Accepted: 4067 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is measured as an int

Supermarket poj 1456 贪心+并查集优化

Language: Default Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9512   Accepted: 4096 Description A supermarket has a set Prod of products on sale. It earns a profit px for each product x∈Prod sold by a deadline dx that is

Supermarket---poj456(贪心并查集优化)

题目链接:http://poj.org/problem?id=1456 题意是现有n个物品,每个物品有一个保质期和一个利润,现在每天只能卖一个商品,问最大的利润是多少,商品如果过期了就不能卖了: 暴力的方法: #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <stack> #i

Supermarket ——贪心(并查集优化)

题目链接 题意: 给你n个商品,每个商品都有两个参数 p d ,p为该商品卖出后的利润,d表明该商品只能在这个期限之前卖出,一天只能卖出一件商品. 问你这批商品最多能获得多少利润 题解: 贪心!!! 按照利润从大到小排序,如果利润相同就按照期限从大到小排序,这样才能保证在一定期限内卖更多的商品获得更大的利润 排序完成后,枚举每个商品的结束的时间,然后向前暴力(找到离这个期限最近的且可占用的时间),如果当前时间可以卖出即没被vis数组标记,就可以再在当前时间卖出 代码: #include<iost

poj1827A Bunch Of Monsters(贪心)

题目链接: 啊哈哈,点我点我 题目意思: 给出n个怪物可以拿到卡片的范围和这个怪物对主人公造成的伤害..然后求最后得到怪物对主人公的最小伤害.. 思路:对怪物造成的伤害从大到小排序,然后对n个怪物进行逐一枚举,枚举时对它的时间进行逆向枚举,然后没有被访问到的将其伤害降为0,最后统计一下即可. 题目: A Bunch Of Monsters Time Limit: 2000MS   Memory Limit: 30000K Total Submissions: 938   Accepted: 35

poj 1456 Supermarket (贪心+并查集)

# include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int fa[10010]; struct node { int p; int d; }; struct node a[10010]; bool cmp(node a1,node a2)//利润从大到小 { return a1.p>a2.p; } int find(int x) { if(fa[x]

POJ 1456 贪心 并查集

看一下中文版的题目就好,英文题目太晦涩了. 有两种方法可以解题 一种是贪心+优先队列 另一种是贪心+并查集 优先队列  需要说的都在代码注释里 #include<cstdio> #include<queue> #include<algorithm> using namespace std; struct s{ int day, val; }arr[100005]; bool cmp(s a, s b){ if(a.day != b.day) return a.day &

贪心 + 并查集 之 CODE[VS] 1069 关押罪犯 2010年NOIP全国联赛提高组

/* 贪心 + 并查集 之 CODE[VS] 1069 关押罪犯  2010年NOIP全国联赛提高组 两座监狱,M组罪犯冲突,目标:第一个冲突事件的影响力最小. 依据冲突大小,将M组罪犯按从大到小排序,按照排序结果,依次把每组罪犯分开放入两个监狱, 直到当前这组罪犯已经在同一个监狱中了,此时即为答案. 实现: 1)通过不在同一个监狱的罪犯,推断出在同一个监狱的罪犯.(依据:一共就两个监狱)      ftr[b] = a+n   // a和b是在不同监狱 ftr[c] = a+n   // a和