HDU-6396 Swordsman

版权声明:选经典题目,写精品文章. https://blog.csdn.net/nka_kun/article/details/81636155

                                                                                          Swordsman

                                                                   Time Limit: 2000/1000 MS (Java/Others)


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

题意:每个怪物有k个防御值,只有剑客的每个攻击值都大于等于这个怪物的对应防御值才可以杀死他,问最多杀多少.

思路:按照每种防御值排个序,把生成的k个序列从低到高依次杀,如果这个怪物有一个值能杀了,怪物编号就++,当所有值都满足了,这个怪物就over了...当然这个题得输入优化一下下~

代码:

#include<bits/stdc++.h>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
const int maxn = 2e5+5;

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;

struct node
{
	int a;
	int pos;
	friend bool operator < (node x,node y)
	{
		return x.a< y.a;
	}
} e[6][maxn];

int n,k;
int v[6],ans;
int b[maxn][6],num[maxn],p[6];

void add(int x)
{
	ans++;
	for(int i = 1;i<= k;i++)
		v[i]+= b[x][i];
}

bool solve()
{
	int f = 0;
	for(int i = 1;i<= k;i++)
	{
		while(p[i]<= n&&e[i][p[i]].a<= v[i])
		{
			f = 1;
			num[e[i][p[i]].pos]++;
			if(num[e[i][p[i]].pos] == k)
				add(e[i][p[i]].pos);
			p[i]++;
		}
	}
	return f;
}

int main()
{
	int t;
	read(t);
	
	while(t--)
	{
		ans = 0;
		read(n);
		read(k);
		for(int i = 1;i<= k;i++) read(v[i]),p[i] = 1;
		for(int i = 1;i<= n;i++)
		{
			num[i] = 0; 
			for(int j = 1;j<= k;j++)
			{
				read(e[j][i].a);
				e[j][i].pos = i;
			}
			for(int j = 1;j<= k;j++)
				read(b[i][j]);
		}
		
		for(int i = 1;i<= k;i++)
			sort(e[i]+1,e[i]+n+1);
		
		while(solve());
		
		printf("%d\n",ans);
		for(int i = 1;i< k;i++) printf("%d ",v[i]);
		printf("%d\n",v[k]);
	}
	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/nka_kun/article/details/81636155