HDU Problem - 6396 Swordsman(优先队列,模拟)

题目链接

Problem Description

Lawson is a magic swordsman with k kinds of magic attributes v 1 , v 2 , v 3 , , v k . Now Lawson is faced with n monsters and the i -th monster also has k kinds of defensive attributes a i , 1 , a i , 2 , a i , 3 , , a i , k . If v 1 a i , 1 and v 2 a i , 2 and v 3 a i , 3 and and v k a i , 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 v j will increase b i , 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 10 5 , 1 k 5 ).The second line has k non-negative integers (initial magic attributes) v 1 , v 2 , v 3 , , v k .For the next n lines, the i -th line contains 2 k non-negative integers a i , 1 , a i , 2 , a i , 3 , , a i , k , b i , 1 , b i , 2 , b i , 3 , , b i , k .It’s guaranteed that all input integers are no more than 10 9 and v j + i = 1 n b i , j 10 9 for j = 1 , 2 , 3 , , k .It is guaranteed that the sum of all n 5 × 10 5 .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.

AC

  • 优先队列循环模拟每个属性,满足当前属性的妖怪进入这个队列,直到最后一个队列没有怪物,每次技能加上最后一个队列的怪物的加成
  • 这个题要加上读写挂,如果你写的代码超时,先试试我这个挂
#include <iostream>
#include <stdio.h>
#include <map>
#include <vector>
#include <queue>
#include <algorithm>
#define N 100005
#define ll long long
#define P pair<int, int>
#define mk make_pair
// 加快读写 
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 std;
int v[10], a[N][15];

using namespace IO;
int main() {
#ifndef ONLINE_JUDGE
    freopen("in.txt", "r", stdin);
#endif
begin();
    priority_queue<P, vector<P>, greater<P> >q[15];
    int t;
    read(t);
    while (t--) {
        int n, k, sum = 0;
        read(n);read(k);
        for (int i = 1; i <= k; ++i) 
            read(v[i]);
        for (int i = 1; i <= n; ++i) 
            for (int j = 1; j <= 2 * k; ++j) 
                read(a[i][j]);
        for (int i = 1; i <= n; ++i) {
            q[0].push(mk(a[i][1], i));
        }
        while (1) {
            // 对队列进行循环 
            for (int i = 0; i < k; ++i) {
                while (! q[i].empty()) {
                    P t = q[i].top();
                    if (t.first > v[i + 1]) break;
                    q[i].pop();
                    q[i + 1].push(mk(a[t.second][i + 2], t.second));
                }
            }
            if (q[k].empty())   break;
            while (!q[k].empty()) {
                P t = q[k].top();
                q[k].pop();
                for (int i = 1; i <= k; ++i) {
                    v[i] += a[t.second][i + k];
                }
                sum++;
            }
        }
        // 注意输出格式 
        printf("%d\n%d", sum, v[1]);
        for (int i = 2; i <= k; ++i) {
            printf(" %d", v[i]);
        }   
        printf("\n");
        // 清空队列 
        for (int i = 0; i <= k; ++i) {
            while (!q[i].empty()) {
                q[i].pop();
            }
        }
    } 

    return 0;
} 

猜你喜欢

转载自blog.csdn.net/henuyh/article/details/81747840