HDU 6396 Swordsman(优先队列+超神输入挂)

Swordsman

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


 

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.

扫描二维码关注公众号,回复: 2878295 查看本文章

 

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组输入。每组输入包含n(n<=1e5),m(1<=m<=5) ,然后给出你的m个属性的初始值。

然后对于n种怪,给出每个怪的m个属性和吃掉它你的m个属性能提高的值。如果你的每种属性都大于等于怪的对应的属性,你就可以吃掉这个怪。求你最多能吃掉多少怪,输出你最终的m个属性的值。

思路:优先队列。用m个优先队列(小的在队头,用pair,默认按第一个排序)。首先把所有怪的第一个属性和怪的编号(用pair)加入到第一个队列当中。每次从第1个属性开始到第m个属性,如果当前属性小于等于你的属性,就把这个怪的下一个属性和怪的编号加入到下一个优先队列中。直到最后一个优先队列中对应的属性小于等于你的最后一个属性,就累加吃掉这个怪你能提高的每个属性的值。然后此时你的各项属性都已经提高,再重复此操作直到吃掉的怪数不再变化。然而这样还是会TLE。需要抄一个超神输入挂。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

namespace fastIO {
    #define BUF_SIZE 100000
    //fread -> read
    bool IOerror = 0;
    inline char nc() {
        static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
        if(p1 == pend) {
            p1 = buf;
            pend = buf + fread(buf, 1, BUF_SIZE, stdin);
            if(pend == p1) {
                IOerror = 1;
                return -1;
            }
        }
        return *p1++;
    }
    inline bool blank(char ch) {
        return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
    }
    inline void read(int &x) {
        char ch;
        while(blank(ch = nc()));
        if(IOerror) return;
        for(x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
    }
    #undef BUF_SIZE
};
using namespace fastIO;
// void read(int &x){scanf("%d",&x);} //调试的时候用这个,把上面的注释掉。

const int maxn=100010;
int n,m,k,a[maxn][10],b[maxn][10],v[maxn];
typedef pair<int, int> pp;
typedef priority_queue< pp, vector<pp>, greater<pp> > QQ;
QQ q[10],p;
int main()
{
    int T,cas=1;
    read(T);
    while(T--)
    {
       read(n);read(m);
        for(int i=0;i<m;i++) read(v[i]);
        for(int i=0;i<m;i++) q[i]=QQ();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            read(a[i][j]);
            q[0].push({a[i][0],i});
            for(int j=0;j<m;j++)
            read(b[i][j]);
        }
        int ans=0,tmp=-1;
        while(tmp!=ans){
        tmp=ans;
        for(int i=0;i<m-1;i++)
        {
            while(!q[i].empty()&&q[i].top().first<=v[i])
            {
                int x=q[i].top().second;q[i].pop();
                q[i+1].push({a[x][i+1],x});
            }
        }
        while(!q[m-1].empty()&&q[m-1].top().first<=v[m-1])
            {
                int x=q[m-1].top().second;q[m-1].pop();
                ++ans;
                for(int i=0;i<m;i++)
                v[i]+=b[x][i];
            }
        }
        int flag=1;
        printf("%d\n",ans);
        for(int i=0;i<m;i++)
        if(flag){flag=0;printf("%d",v[i]);}
        else printf(" %d",v[i]);
        puts("");
    }
      //  if(flag) puts("Yes"); else puts("No");
    return 0;
}

猜你喜欢

转载自blog.csdn.net/LSD20164388/article/details/81637550