zoj3433(贪心+优先队列)

Gu Jian Qi Tan


Time Limit: 2 Seconds      Memory Limit: 65536 KB


Gu Jian Qi Tan is a very hot Chinese RPG in this summer vacation. SuSu is the most important role in the game. When SuSu first meet the ancient dragon QianYu, he will have a very difficult battle with QianYu. In order to resist QianYu‘s powerful attack, SuSu must make a special armor, which needs some precious materials ( We call it "Ice Heart" ).

Before this legend fight, SuSu must pass M dangerous labyrinth in order. In each labyrinth, there is a very powerful monster (We usually call it "BOSS") keeping watching the exit. So SuSu must defeat each BOSS if he wants to leave each labyrinth. When SuSu is fighting with a BOSS, he must attack its main body to kill it. however, some BOSS may have one or more special position of its body (such as wings, hands, weapons). SuSu can choose to attack these special positions and he will get one Ice Heart if he break the defence of one special position. Of course SuSu can choose not to attack special positions but just attack the BOSS‘s main body (in this situation, he can still kill the BOSS, but he cannot get Ice Heart).

The defence of all special positions are so strong that normal attack are not available. SuSu must use magic skill to attack it. Once he uses a magic skill, his mana point will decrease to 0. In order to recover mana point, he can eat a special food "Dan Gui Hua Gao" (a kind of cake).

In each labyrinth, SuSu can collect some cakes by killing small monsters. When he fights with a BOSS, his initial mana point will be 0. Different BOSS may have different amount of special positions, and the defence of different position may also be different ( that is, some positions may need just one magic attack but some may need to be attacked many times )
Notice: cakes in previous labyrinth can be accumulated and brought to later labyrinth.

Can you tell how many Ice heart can SuSu get at most?

Input

The first line of the input is a single integer T (T <= 20) indicating the number of test cases.
In each case, fisrt there is a line containing one integer M ( M <= 1000) indicating the number of labyrinth.
Then M lines follow. In the ith line, first there is an integer n (n <= 1000) indicating the amount of special positions this BOSS has.
Then followd by n integers, the ith integer ( no more than 20 ) indicating the amount of magic attacks SuSu must use to break the ith special position.
Finally there is a line containing M integers, the ith integer ( no more than 20 ) indicating how many cakes SuSu can collect in the ith labyrinth.

Output

For each case, output one line, containing the maximum number of Ice Heart SuSu can get.

Sample Input

1
2
1 10
2 1 2
10 0

Sample Output

2
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 #include<vector>
 6 using namespace std;
 7 int n,t,m,b;
 8 int main()
 9 {
10     scanf("%d",&t);
11     while(t--)
12     {
13         vector<int>a[2005];
14         scanf("%d",&m);
15         for(int i=0;i<m;i++)
16         {
17             scanf("%d",&n);
18             for(int j=0;j<n;j++)
19             {
20                 scanf("%d",&b);
21                 a[i].push_back(b);
22             }
23         }
24         int sum=0;
25         int k=0;
26         int amp;
27         priority_queue<int>q;
28         for(int i=0;i<m;i++)
29         {
30             scanf("%d",&amp);
31             //q.push(amp);
32             sum+=amp;
33             for(int j=0;j<a[i].size();j++)
34             {
35                 sum-=a[i][j];
36                 k++;
37                 q.push(a[i][j]);
38             }
39             while(sum<0)
40             {
41                 sum+=q.top();
42                 q.pop();
43                 k--;
44             }
45
46         }
47         printf("%d\n",k);
48     }
49     return 0;
50 }
时间: 2024-10-18 14:35:57

zoj3433(贪心+优先队列)的相关文章

poj3190Stall Reservations(贪心+优先队列)

题目链接: 啊哈哈,点我点我 思路: 首先根据挤奶时间的先后顺序排序...然后将第一头牛加入优先队列..然后就是加入优先队列的牛应该根据越早结束挤奶那么优先级更高,如果时间结束点相等,那么开始时间早的优先级高... 然后从前向后枚举.如果碰到有牛的挤奶时间的开始值大于优先队列的首部的结束值,那么说明这两头牛可以一起公用一个挤奶房..然后从优先队列中删除这头牛..那么这个问题就得到解决了... 题目: Language: Default Stall Reservations Time Limit:

poj 2431 Expedition (贪心+优先队列)

Expedition Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6890   Accepted: 2065 Description A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poor drivers, the cows unfortunately managed to

BZOJ 2151 种树 贪心+优先队列+HASH

题意:链接 方法:贪心+优先队列 解析: 首先裸上贪最大的肯定不对. DP可行么?可行,两个数组一个记录选一个记录不选好像差不多n^2? 不过还是想想贪心. 贪最大的为什么不对? 因为有可能它旁边的两个加起来比它更优越? 所以能否找到一点关系呢? 既然话都说到这了,两个加起来可能更优越. 所以我们在选了最大的之后是否应该回推一个值呢?代表选旁边俩. 我们发现选旁边俩是两个单位,选一个是一个单位,如果我们推回去某个值后这个值相当于对应删去一个单位选两个单位,即增加一个单位,这显然不会影响最终我们选

POJ1456Supermarket(贪心+优先队列)

Supermarket Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9103   Accepted: 3891 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

[POJ1456]Supermarket(贪心 + 优先队列 || 并查集)

传送门 1.贪心 + 优先队列 按照时间排序从前往后 很简单不多说 ——代码 1 #include <queue> 2 #include <cstdio> 3 #include <iostream> 4 #include <algorithm> 5 #define N 10001 6 7 int n, t, ans; 8 std::priority_queue <int, std::vector <int>, std::greater &l

POJ 3190 Stall Reservations(贪心+优先队列优化)

Description Oh those picky N (1 <= N <= 50,000) cows! They are so picky that each one will only be milked over some precise time interval A..B (1 <= A <= B <= 1,000,000), which includes both times A and B. Obviously, FJ must create a reserv

hihoCoder 1309:任务分配 贪心 优先队列

#1309 : 任务分配 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 给定 N 项任务的起至时间( S1, E1 ), ( S2, E2 ), ..., ( SN, EN ), 计算最少需要多少台机器才能按时完成所有任务. 同一时间一台机器上最多进行一项任务,并且一项任务必须从头到尾保持在一台机器上进行.任务切换不需要时间. 输入 第一行一个整数 N,(1 ≤ N ≤ 100000),表示任务的数目. 以下 N 行每行两个整数 Si, Ei,(0 ≤ Si < 

Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列

题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个最大的取k次,所以最优解由此推出. 如果不管p,先拿,最后再减去那些行列交叉点,因为每个点的值只能取一次,而交叉点的值被加了两次,所以要减掉1次,如果取行A次,取列B次,那么最后答案为: res = dp1[A] + dp2[B] - B*(k-A)*p,可以细细体会一下后面那部分. 其中: dp1

POJ 1862 Stripies 贪心+优先队列

http://poj.org/problem?id=1862 题目大意: 有一种生物能两两合并,合并之前的重量分别为m1和m2,合并之后变为2*sqrt(m1*m2),现在给定n个这样的生物,求合并成一个的最小重量 思路: m1+m2 >=  2*sqrt(m1*m2) 所以每次取大的去合并,能变小. 直接优先队列就可以啦. #include<cstdio> #include<cmath> #include<queue> using namespace std;

UVA 1422 - Processor (二分+贪心+优先队列)

先对开始时间进行排序,在利用优先队列是结束时间早点先出队: 因为时间只有20000,我们可以去枚举每个单位时间,看要给分配给那个任务, 如果某个时间队列中还有结束时间大于枚举的时间,就跳出判断是在mid的右边. #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> #include<stdlib.h> #include<math.h> #