hdu 6396(优先队列)

题意:一个人有k种攻击力,每个怪物有k种防御力,攻击力大于防御力就能杀死怪物,并获得攻击力的提升,问你最多杀死几个怪物。最后你的攻击力是多少。

思路:k最多5,所以建立5个优先队列,每个k占一个。当前怪物k1小于人的k1时,就把这只怪物扔到k2堆里,每个怪物最多被扔5次。最后一次出来时说明可以杀死他,答案记录下让他出队即可。这题输入很容易被卡,我最开始用普通的读入优化也被卡了,后来换了个读入才过。

#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
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 k[6],b[6];
} a[100005];
int n,m;

struct cmp1
{
	bool operator() (const node&a,const node&b)
	{
		return a.k[1]>b.k[1];
	}
};
priority_queue<node,vector<node>,cmp1> q1;
struct cmp2
{
	bool operator() (const node&a,const node&b)
	{
		return a.k[2]>b.k[2];
	}
};
priority_queue<node,vector<node>,cmp2> q2;
struct cmp3
{
	bool operator() (const node&a,const node&b)
	{
		return a.k[3]>b.k[3];
	}
};
priority_queue<node,vector<node>,cmp3> q3;
struct cmp4
{
	bool operator() (const node&a,const node&b)
	{
		return a.k[4]>b.k[4];
	}
};
priority_queue<node,vector<node>,cmp4> q4;
struct cmp5
{
	bool operator() (const node&a,const node&b)
	{
		return a.k[5]>b.k[5];
	}
};
priority_queue<node,vector<node>,cmp5> q5;
int k[6];
int main()
{


	int t;
	read(t);
	while(t--)
	{
		memset(k,0,sizeof(k));
		read(n);
		read(m);
		for(int i=1; i<=m; i++) read(k[i]);
		for(int i=0; i<n; i++)
		{
			node u;
			u.k[0]=0;
			u.k[1]=0;
			u.k[2]=0;
			u.k[3]=0;
			u.k[4]=0;
			u.k[5]=0;
			u.b[0]=0;
			u.b[1]=0;
			u.b[2]=0;
			u.b[3]=0;
			u.b[4]=0;
			u.b[5]=0;
			for(int j=1; j<=m; j++)
			{
				read(a[i].k[j]);
				u.k[j]=a[i].k[j];
			}
			for(int j=1; j<=m; j++)
			{
				read(a[i].b[j]);
				u.b[j]=a[i].b[j];
			}
			q1.push(u);
		}
		int ans=0;

		while(1)
		{
			int flag=0;

			for(int i=1; i<=m-1; i++)
			{
				if(i==1)
				{
					while(!q1.empty())
					{
						if(q1.top().k[1]>k[1]) break;
						q2.push(q1.top());
						q1.pop();
						flag=1;
					}
				}
				else if(i==2)
				{
					while(!q2.empty())
					{
						if(q2.top().k[2]>k[2]) break;
						q3.push(q2.top());
						q2.pop();
						flag=1;
					}
				}
				else if(i==3)
				{
					while(!q3.empty())
					{
						if(q3.top().k[3]>k[3]) break;
						q4.push(q3.top());
						q3.pop();
						flag=1;
					}
				}
				else if(i==4)
				{
					while(!q4.empty())
					{
						if(q4.top().k[4]>k[4]) break;
						q5.push(q4.top());
						q4.pop();
						flag=1;
					}
				}
			}
			if(m==1)
			{
				while(!q1.empty())
				{
					if(q1.top().k[1]>k[1])
					{
						break;
					}
					else
					{
						ans++;
						for(int i=1; i<=m; i++) k[i]+=q1.top().b[i];
						flag=1;
						q1.pop();
					}
				}
			}
			else if(m==2)
			{
				while(!q2.empty())
				{
					if(q2.top().k[2]>k[2])
					{
						break;
					}
					else
					{
						ans++;
						for(int i=1; i<=m; i++) k[i]+=q2.top().b[i];
						flag=1;
						q2.pop();
					}
				}
			}
			else if(m==3)
			{
				while(!q3.empty())
				{
					if(q3.top().k[3]>k[3])
					{
						break;
					}
					else
					{
						ans++;
						for(int i=1; i<=m; i++) k[i]+=q3.top().b[i];
						flag=1;
						q3.pop();
					}
				}
			}
			else if(m==4)
			{
				while(!q4.empty())
				{
					if(q4.top().k[4]>k[4])
					{
						break;
					}
					else
					{
						ans++;
						for(int i=1; i<=m; i++) k[i]+=q4.top().b[i];
						flag=1;
						q4.pop();
					}
				}
			}
			else if(m==5)
			{
				while(!q5.empty())
				{
					if(q5.top().k[5]>k[5])
					{
						break;
					}
					else
					{
						ans++;
						for(int i=1; i<=m; i++) k[i]+=q5.top().b[i];
						flag=1;
						q5.pop();
					}
				}
			}
			if(flag==0) break;
		}
		while(!q1.empty())q1.pop();
		while(!q2.empty())q2.pop();
		while(!q3.empty())q3.pop();
		while(!q4.empty())q4.pop();
		while(!q5.empty())q5.pop();
		printf("%d\n",ans);
		for(int i=1; i<=m; i++)if(i==m) printf("%d\n",k[m]);
			else printf("%d ",k[i]);
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37632935/article/details/81665422