版权声明:本人蒟蒻,如有大佬转发,感激不尽,带上我的博客链接即可。 https://blog.csdn.net/qq_36300700/article/details/81668155
转自:http://www.cnblogs.com/AWCXV/p/9470006.html
题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=6396
思路:
开k个优先队列。每个队列都满足从小到大那种。。
首先将所有的怪物加入到第一个队列中。
然后对于v[i]>=pq[i].top()的怪物,把这个怪物加入到i+1个队列。
然后每个队列都这么做。
直到不会有怪物从一个队列转移到另外一个队列为止。
当某个怪物在第k个队列中也满足转移之后。。那么这个怪物就是能被打败的了。。
每个队列就是一种检验吧。。。如果k个都能。。那就都满足了。。
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <map>
#include <queue>
#include <vector>
using namespace std;
const int maxn =1e5+10;
int T,n,k;
int v[10],a[maxn][10],b[maxn][10];
priority_queue<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > >pq[6];
namespace IO {
const int MX = 4e7;
char buf[MX]; int c, sz;
void begin() {
c = 0;
sz = fread(buf, 1, MX, stdin);
}
inline bool read(int &t) {
while(c < sz && buf[c] != '-' && (buf[c] < '0' || buf[c] > '9')) c++;
if(c >= sz) return false;
bool flag = 0; if(buf[c] == '-') flag = 1, c++;
for(t = 0; c < sz && '0' <= buf[c] && buf[c] <= '9'; c++) t = t * 10 + buf[c] - '0';
if(flag) t = -t;
return true;
}
}
int main()
{
//freopen("out.txt","w",stdout);
IO::begin();
IO::read(T);
while(T--)
{
for(int i=1; i<=5; i++)
while(!pq[i].empty())
pq[i].pop();
IO::read(n);
IO::read(k);
for(int i=1; i<=k; i++)
IO::read(v[i]);
for(int i=1; i<=n; i++)
{
for(int j=1; j<=k; j++)
IO::read(a[i][j]);
for(int j=1; j<=k; j++)
IO::read(b[i][j]);
}
for(int i=1; i<=n; i++)
pq[1].push(make_pair(a[i][1],i));
bool ok =false;
int cnt = 0;
while(!ok)
{
ok = true;
for(int j=1; j<=k; j++)
{
while(!pq[j].empty())
{
int val=pq[j].top().first, id=pq[j].top().second;
if(val>v[j])
break;
if(j==k)
{
for(int i=1; i<=k; i++)
v[i]+=b[id][i];
ok=false;
cnt++;
pq[j].pop();
}
else
{
pq[j+1].push(make_pair(a[id][j+1],id));
pq[j].pop();
}
}
}
}
printf("%d\n",cnt);
for(int i=1; i<=k; i++)
{
printf("%d",v[i]);
if(i==k) puts("");
else putchar(' ');
}
}
return 0;
}