2018hdu杭电多校第七场 hdu6396 Swordsman(暴力优先队列)

Swordsman

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 1805    Accepted Submission(s): 519

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.


题意:t组样例,有个魔法剑士有k个属性,有m只怪,每一只怪也有k个属性,a数组中是怪本身属性,b数组中是杀死怪获得的奖励属性,当魔法剑士3种属性都大于一只怪时才能杀死它,求最大杀死怪的只数和杀死这些怪之后魔法剑士的属性。

ps: 题目提醒输入量大 用如 fread 的快速io,不然光输入就TLE了

题解:用k个优先队列存储属性 当魔法剑士该属性符合能杀死这只怪的条件(魔法剑士的属性大于该怪的属性)就进入下一个属性的优先队列, 循环操作到不能操作为止。 最后剩下的就是能杀死的怪,简单处理后就是答案。

代码:

#include 
#define INF 0x3f3f3f3f
#define X first
#define no second
#define rep(i,a,n) for (int i=a;i pii;
//------------------------------------head------------------------------------
namespace IO {
    const int MX = 4e7;
    char buf[MX]; int c, sz;
    void begin() {
        c = 0;
        sz = fread(buf, 1, MX, stdin);
    }
    inline bool read(int &t) {
        while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
        if(c >= sz) return false;
        bool flag = 0; if(buf[c] == '-') flag = 1, c++;
        for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
        if(flag) t = -t;
        return true;
    }
}
using namespace IO;

const int N = 1e5+5, K = 7;

priority_queue, greater > pq[K];

int n, k, _;
int v[K], a[N][K], b[N][K];

int main() {
//    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
    begin();
    for (read(_);_;_--) {
        rep (i, 0, 5) while (!pq[i].empty()) pq[i].pop();
        read(n), read(k);
        rep (i, 0, k) read(v[i]);
        rep (i, 0, n) {
            rep(j, 0, k) read(a[i][j]);
            rep(j, 0, k) read(b[i][j]);
        }
        rep (i, 0, n) pq[0].push(mp(a[i][0], i));
        bool ok = false;
        int cnt = 0;
        while (!ok) {
            ok = true;
            rep (i, 0, k) { // 遍历每一个属性
                while (!pq[i].empty()) {
                    int val = pq[i].top().X, id = pq[i].top().no;
                    if (v[i] < val) break; // 怪属性比人高
                    if (i == k-1) { // 留到最后一个队列的怪就是能杀死的
                        ok = false;
                        cnt++;
                        pq[i].pop();
                        rep (j, 0, k) v[j] += b[id][j];
                    } else {
                        pq[i+1].push(mp(a[id][i+1], id));
                        pq[i].pop();
                    }
                }
            }
        }
        printf("%d\n", cnt);
        rep (i, 0, k) printf("%d", v[i]), putchar(i==k-1?'\n':' ');
    }
}

 

你可能感兴趣的:(hdu,2018多校,题解)