Swordsman 优先队列

[1] Swordsman

Problem Description
Lawson is a magic swordsman with k kinds of magic attributes v1,v2,v3,…,vk. Now Lawson is faced with n monsters and the i-th monster also has k kinds of defensive attributes ai,1,ai,2,ai,3,…,ai,k. If v1≥ai,1 and v2≥ai,2 and v3≥ai,3 and … and vk≥ai,k, Lawson can kill the i-th monster (each monster can be killed for at most one time) and get EXP from the battle, which means vj will increase bi,j for j=1,2,3,…,k.
Now we want to know how many monsters Lawson can kill at most and how much Lawson’s magic attributes can be maximized.
Input
There are multiple test cases. The first line of input contains an integer T, indicating the number of test cases. For each test case:
The first line has two integers n and k (1≤n≤105,1≤k≤5).
The second line has k non-negative integers (initial magic attributes) v1,v2,v3,…,vk.
For the next n lines, the i-th line contains 2k non-negative integers ai,1,ai,2,ai,3,…,ai,k,bi,1,bi,2,bi,3,…,bi,k.
It’s guaranteed that all input integers are no more than 109 and vj+∑i=1nbi,j≤109 for j=1,2,3,…,k.

It is guaranteed that the sum of all n ≤5×105.
The input data is very large so fast IO (like fread) is recommended.
Output
For each test case:
The first line has one integer which means the maximum number of monsters that can be killed by Lawson.
The second line has k integers v′1,v′2,v′3,…,v′k and the i-th integer means maximum of the i-th magic attibute.
Sample Input
1
4 3
7 1 1
5 5 2 6 3 1
24 1 1 1 2 1
0 4 1 5 1 1
6 0 1 5 3 1
Sample Output
3
23 8 4
Hint
For the sample, initial V = [7, 1, 1]
① kill monster #4 (6, 0, 1), V + [5, 3, 1] = [12, 4, 2]
② kill monster #3 (0, 4, 1), V + [5, 1, 1] = [17, 5, 3]
③ kill monster #1 (5, 5, 2), V + [6, 3, 1] = [23, 8, 4]
After three battles, Lawson are still not able to kill monster #2 (24, 1, 1)
because 23 < 24.
Source
2018 Multi-University Training Contest 7

题意:有n只怪兽,每只怪兽有k个防御力和k个增幅。有一个剑客,他有k个攻击力,他只有当他的k个攻击力都严格大于某只怪兽的k个防御力时,他才能杀死这只怪兽,同时他的k个攻击力会从他杀死的这只怪兽这里获得增幅,问他最多能杀死多少只怪兽,此时他的k个攻击力分别是多少。

思路:由于k最大为5,可以借助最多5个优先队列,每个优先队列quei保存的是所有怪兽的血量hpi,先把所有怪兽存在第一个优先队列,然后从第一个优先队列开始,找出所有hpi<=ADi的怪兽并把它移到下一个优先队列quei+1,当到了最后一个队列,移出去的怪兽代表被杀死,然后更新队列,如果最后一个队列没有移除怪兽,代表已经杀不死任何怪兽了,循环结束,输出结果.

注意:
数据量较大,要用到快速输入挂
每次输出完结果后要清空栈,以便下一组数据输入

#include<bits/stdc++.h>
#include<iostream>
#include<queue>
#include<algorithm>
using namespace std;

#define reads(n) FastIO::read(n)
namespace FastIO {
    const int SIZE = 1 << 16;
    char buf[SIZE], obuf[SIZE], str[60];
    int bi = SIZE, bn = SIZE, opt;
    int read(char *s) {
        while (bn) {
            for (; bi < bn && buf[bi] <= ' '; bi++);
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        int sn = 0;
        while (bn) {
            for (; bi < bn && buf[bi] > ' '; bi++) s[sn++] = buf[bi];
            if (bi < bn) break;
            bn = fread(buf, 1, SIZE, stdin);
            bi = 0;
        }
        s[sn] = 0;
        return sn;
    }
    bool read(int& x) {
        int n = read(str), bf;
        
        if (!n) return 0;
        int i = 0; if (str[i] == '-') bf = -1, i++; else bf = 1;
        for (x = 0; i < n; i++) x = x * 10 + str[i] - '0';
        if (bf < 0) x = -x;
        return 1;
    }
};

const int maxn = int(1e5) + 7;

struct Tmp {
    int hp, index;
    bool operator <(const Tmp &tmp) const {
        return hp < tmp.hp;
    }
    bool operator >(const Tmp &tmp) const {
        return hp > tmp.hp;
    }
} buf;

struct Monster {
    int hp[7], ad[7], index;
} m[maxn];

int main() {
	int t, n, k, AD[7];
	priority_queue<Tmp,vector<Tmp>,greater<Tmp> > que[7];
//	for (scanf("%d", &t); t; t--) {
	reads(t);
	while(t--){
//      scanf("%d%d", &n, &k);
		reads(n);
		reads(k);
        for (int i = 1; i <= k; ++i) reads(AD[i]);
//		scanf("%d", &AD[i]);
        for (int i = 1; i <= n; ++i) {
            for (int j = 1; j <= k; ++j) reads(m[i].hp[j]);
//			 scanf("%d", &m[i].hp[j]);
            for (int j = 1; j <= k; ++j) reads(m[i].ad[j]);
//			 scanf("%d", &m[i].ad[j]);
            m[i].index = i;
        }
        
        for (int i = 1; i <= n; i++){
        	struct Tmp t ;
			t.hp = m[i].hp[1];
			t.index = i;
			que[1].push(t);  
//			que[1].push({m[i].hp[1], i});
		}
		
        int pre = 0, ans = 0;
        
		while (true) {
            for (int i = 1; i < k; i++) {
                while (!que[i].empty() && (buf = que[i].top(), buf.hp <= AD[i])) {
                    int index = buf.index;
                  struct Tmp t ;
					t.hp = m[index].hp[i + 1];
					t.index = index;
					que[i + 1].push(t);  
//					que[i + 1].push({m[index].hp[i + 1], index});
                    que[i].pop();
                }
            }
            while (!que[k].empty() && (buf = que[k].top(), buf.hp <= AD[k])) {
                ans++;
                int index = buf.index;
                for (int i = 1; i <= k; i++) AD[i] += m[index].ad[i];
                que[k].pop();
            }
            if (ans == pre) break;
            pre = ans;
        }
        
        printf("%d\n", ans);
        
        for (int i = 1; i <= k; i++)
			printf("%d%c", AD[i], i == k ? '\n' : ' ');
		
		for(int i=0;i<=6;i++)
        {
            while(!que[i].empty())que[i].pop();
        }
    }
    return 0;
}
 
  • 0
    点赞
  • 0
    收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

陈杉菜

你的鼓励将是我创作的最大动力

¥2 ¥4 ¥6 ¥10 ¥20
输入1-500的整数
余额支付 (余额:-- )
扫码支付
扫码支付:¥2
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值