ZOJ 3433 Gu Jian Qi Tan

ZOJ Problem Set - 3433
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" ).

SuSu and QianYu

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

Author: HUANG, Qiao
Contest: ZOJ Monthly, November 2010

方法一

解法:贪心。对于一个ice heart,如果当前cake数大于或等于该ice heart的消耗,则直接取得,如果不,则用前面消耗的最大cake的与当前ice heart比较,当前ice heart消耗小些,则交换,赚一点cake,否则不换。用一个最大堆维护即可。

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int M,n,i,j,t,T;
    int k,temp;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&M);
        vector<int> a[1003];
        priority_queue<int> Q;
        for(i=0;i<M;i++)
        {
            scanf("%d",&n);
            for(j=0;j<n;j++)
               scanf("%d",&t),a[i].push_back(t);
        }
         for(temp=k=i=0;i<M;i++)
         {
            scanf("%d",&t);
            temp+=t;
            for(j=0;j<a[i].size();j++)
             {
                 if(a[i][j]<=temp)
                     {
                         Q.push(a[i][j]);
                         k++;temp-=a[i][j];
                     }
                 else
                 {
                    if(!Q.empty()&&Q.top()>a[i][j])
                    {   temp+=(Q.top()-a[i][j]);
                        Q.pop();
                        Q.push(a[i][j]);
                    }
                 }
             }
         }
        printf("%d\n",k);
    }
    return 0;
}

方法二

走到一个迷宫后,尽可能地占下所有的ice heart,如果占下某个ice heart时将当前积累的Cake消耗成了负数,则放弃之前取得的一个消耗Cake最大的ice heart

#include <iostream>
#include <cstdio>
#include <queue>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
    int M,n,i,j,t,T;
    int k,temp;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&M);
        vector<int> a[1003];
        priority_queue<int> Q;
        for(i=0;i<M;i++)
        {
            scanf("%d",&n);
            for(j=0;j<n;j++)
               scanf("%d",&t),a[i].push_back(t);
        }
         for(temp=k=i=0;i<M;i++)
         {
             scanf("%d",&t);
              temp+=t;
             for(j=0;j<a[i].size();j++)
               {
                   Q.push(a[i][j]);
                   temp-=a[i][j];
                   k++;
               }
            while(temp<0)
            {
                temp+=Q.top();
                Q.pop();
                k--;
            }
         }
         printf("%d\n",k);

    }
    return 0;
}

 

转载于:https://www.cnblogs.com/372465774y/archive/2012/07/09/2583754.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值