2018 Multi-University Training Contest 7 HDU6396 Swordsman 对接优先队列+ 读入挂

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

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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
const int maxn = 1e5  + 100;
const int maxm = 4e5 + 100;
const int INF = 0x3f3f3f3f;
//const int mod = 1e9 + 7;
typedef pair<int,int> P;
typedef long long LL;
const LL mod = 1e9 + 7;

#define PI 3.1415926
#define sc(x)  scanf("%d",&x)
#define pf(x)  printf("%d",x)
#define pfn(x) printf("%d\n",x)
#define pfln(x) printf("%I64d\n",x)
#define pfs(x) printf("%d ",x)
#define rep(i,a,n) for(int i = a; i < n; i++)
#define per(i,a,n) for(int i = n-1; i >= a; i--)
#define mem(a,x) memset(a,x,sizeof(a))
#define pb(x)  push_back(x);
//读入挂
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;}


// void read(int &x){
// 	char ch = getchar();x = 0;
// 	for (; ch < '0' || ch > '9'; ch = getchar());
// 	for (; ch >='0' && ch <= '9'; ch = getchar()) x = x * 10 + ch - '0';
// }

int a[maxn][6];
int b[maxn][6];
int v[6];
int n,k;



int main()
{
	fread(Buf,1,BUF,stdin);//读入挂
        int T;
	read(T);
	while(T--)
	{
	  priority_queue< P,vector<P>,greater<P> >q[5];
		read(n);read(k);
		rep(i,0,k) read(v[i]);
		rep(i,0,n) {rep(j,0,k) read(a[i][j]);rep(j,0,k) read(b[i][j]);}
		rep(i,0,n) q[0].push(P(a[i][0],i));
		int ans = 0;
    while(1)
		{
			//cout << "*";
			bool res = true;
			for(int i = 0; i < k; i++)
			{
				while(!q[i].empty())
				{
				P p = q[i].top();
				if(p.first <= v[i])
				{
					q[i].pop();
					if(i == k-1) {res = false; ans ++ ;rep(j,0,k) v[j] += b[p.second][j];}
				  else q[i+1].push(P(a[p.second][i+1],p.second));
				}
				else break;
			  }
			}
			if(res) break;
		}
		pfn(ans);
		rep(i,0,k-1) pfs(v[i]);
		pfn(v[k-1]);
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值