HDU多校 6396 Swordsman(优先队列

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ffgcc/article/details/81635219

Swordsman
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 188 Accepted Submission(s): 54

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.
解析:直接用五个队列来做,每当满足当前条件时就进入下一个队列继续判断,当满足k个条件,cnt++即可

#include<bits/stdc++.h>
#define ll long long
#define inf 0x3f3f3f3f
#define pb push_back
#define mod 1000000007
#define rep(i,a,b) for(int i=a;i<b;i++)
#define rep1(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
const int N=1e5+100;
int ar[N][10];
int now[10];
struct node
{
    int a,b;
    friend bool operator <(node c,node d)
    {
        return c.b>d.b;
    }
};
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;
    }
}
priority_queue<node >pq[6];
int main()
{
    IO::begin();
    int t ;
    IO::read(t);
    while(t--)
    {
        for(int i=0;i<6;i++)
            while(!pq[i].empty()) pq[i].pop();
        int n,k;
        IO::read(n);
        IO::read(k);
        int kk=2*k;
        for(int i=0;i<k;i++)
            IO::read(now[i]);
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<kk;j++)
            {
                IO::read(ar[i][j]);
            }
            pq[0].push({i,ar[i][0]});
        }
        int pp=-1,cnt=0;
        while(1)
        {
            pp=0;
            for(int i=0;i<k;i++)
            {
                while(!pq[i].empty())
                {
                    node t=pq[i].top();

                    if(now[i]>=t.b)
                    {
                        if(i==k-1)
                        {
                            for(int j=k;j<kk;j++)
                                now[j-k]+=ar[t.a][j];
                            cnt++;pp=1;
                        }
                        else
                            pq[i+1].push({t.a,ar[t.a][i+1]});
                        pq[i].pop();
                    }
                    else
                    {
                        break;
                    }
                }
            }
            if(pp==0) break;
        }
        printf("%d\n",cnt);
        for(int i=0;i<k-1;i++)
            printf("%d ",now[i]);
        printf("%d\n",now[k-1]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/ffgcc/article/details/81635219