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.
题解:
1.题意是由n个怪兽和一个勇士,都有k个属性值(K<5),只有当勇士的所有属性都大于或等于某一个怪兽的属性才能杀掉这个怪兽,并且获得相应的各个属性的提升。
2.一开始用sort写了一下,发现是n^2logn的复杂度,所以不行。然后想用一个优先队列,但是发现优先队列不能随着v的增加而增加。
3.正确的做法为开k个优先队列,没有优先队列代表一个属性,一开始将所有n个怪兽的第一个属性全部放入第一个有限队列,然后依次遍历k个个优先队列,第i个优先队列里的怪兽如果第i个属性满足条件则放入到第i+1个优先队列中去,一直到最后一个优先队列,则代表被勇士干掉,勇士增加属性值。
因为每个怪兽最多入队k此,出队k此,所以复杂度为Knlongn.
4.这题用read优化都不能过,会TLE,需要用fread读入挂。
const int BUF=40000000;
char Buf[BUF], *buf=Buf;
inline void read(int& a) {for(a=0;*buf<48;buf++); while(*buf>47) a=a*10+*buf++-48;}
int main()
{
fread(Buf,1,BUF,stdin);//读入挂
}
AC代码
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include