题目:http://acm.hdu.edu.cn/showproblem.php?pid=6396
题意:有T组数据,n个怪兽,各有k种属性值和k种经验值,剑客也有k个属性,若剑客的k个属性值比怪兽对应的那k个属性值大,那么剑客可以杀掉怪兽,并获得对应的k种经验值,分别对应着加到原来自己的属性值上。求剑客可以杀掉的怪兽数,和最终剑客的各属性值。
思路:化整体到分块考虑,本以为需要循环很多次,整体地去比较,其实可以从局部 利用优先队列 和 对id打标记做,非常巧妙。
有k种属性,开k个优先队列(priority_queue<PII,vector<PII>,greater<PII> > que[10] 按递增地排),把怪兽的k种属性值和自己的 id make_pair 放入到 k个优先队列中。
遍历k种属性,若怪兽的属性值 que[ j ].top().fi 有比 剑客对应的属性值 man[ j ] 小的(这时要pop(),所以一开始建议用 tmp 来表示que[i].top() ),那么 对这个怪兽做标记 vis[ que[ j ].top().se ]++ 即 vis[怪兽的 id ]++ ,每+1,表示剑客在当前情况下,能胜过该怪兽 vis[id] 个属性,而只有当全部属性都胜过时,才能加 对应属性的经验值,即 若此时 vis[ id ]恰 == k,那么就表示剑客可以完全胜出,那么就把对应的属性值加上。继续循环遍历(随着剑客属性值的增高,能杀的也越来越多了)。直到某一全部k个属性遍历下来,剑客的属性值都小于怪兽的属性值时跳出。
为了防止T,需要注意:1.用更快的快读快写(把模板中不用的那些函数删掉会快些)(一般只用个快读,输出直接用 printf 就行了)(当然这个模板在本地运行时,窗口可能无法正常地输入输出,还在测试时可以先用普通的输入输出) 2.取队列的 top(),注意先判断一下队列是否已经空了,否则可能会出现想不到的状况,可能WA或者T 3.原以为 关同步 会快一些,其实不写 fio ,即不关同步 时间更短
https://blog.csdn.net/qq_40858062/article/details/81635856
#include <iostream>
#include <cstdio>
#include <iomanip>
#include <functional>
#include <algorithm>
#include <bitset>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <cstddef>
#include <memory>
#include <vector>
#include <cctype>
#include <string>
#include <cmath>
#include <queue>
#include <deque>
#include <list>
#include <ctime>
#include <stack>
#include <sstream>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#pragma GCC optimize(3)
//#pragma GCC optimize("unroll-loops")
//#pragma comment(linker, "/stack:200000000")
//#pragma GCC optimize("Ofast,no-stack-protector")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#define fio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define pb push_back
#define mkp(a,b) make_pair(a,b)
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define fi first
#define se second
#define lc (d<<1) //d*2
#define rc (d<<1|1) //d*2+1
#define eps 1e-9
#define stn(a) setprecision(a)//小数总有效位数
#define stfl setiosflags(ios::fixed)//点后位数:cout<<stfl<<stn(a);
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const double PI=3.1415926535897932;
const int MAXN=1e5+10;
const ll mod=1e9+7;
ll inline mpow(ll a,ll b){ll ans=1;a%=mod;while(b){if(b&1)ans=(ans*a)%mod;a=(a*a)%mod,b>>=1;}return ans;}
int inline sgn(double x){return (x>-eps)-(x<eps);} //a<b:sgn(a-b)<0
priority_queue<int,vector<int>,greater<int> > qu; //up
priority_queue<int,vector<int>,less<int> > qd; //dn
const int inf = 0x3f3f3f3f; //9
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
namespace IO{
#define BUF_SIZE 100000
#define OUT_SIZE 100000
#define ll long long
//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;}
//{printf("IO error!\n");system("pause");for (;;);exit(0);}
}
return *p1++;
}
inline bool blank(char ch){return ch==' '||ch=='\n'||ch=='\r'||ch=='\t';}
inline void read(int &x){
bool sign=0; char ch=nc(); x=0;
for (;blank(ch);ch=nc());
if (IOerror)return;
if (ch=='-')sign=1,ch=nc();
for (;ch>='0'&&ch<='9';ch=nc())x=x*10+ch-'0';
if (sign)x=-x;
}
};
int T;
int n,k;
int man[10];
int a[10],b[100010][10];
int vis[100010];
priority_queue<PII,vector<PII>,greater<PII> > que[10];
PII tmp;
int main()
{
// fio;
IO::read(T);
// cin>>T;
while(T--)
{
IO::read(n);IO::read(k);
// cin>>n>>k;
for(int i=0;i<k;i++)
while(!que[i].empty())
que[i].pop();
for(int i=0;i<k;i++)
IO::read(man[i]);
// cin>>man[i];
// memset(vis,0,sizeof(vis));
for(int i=0;i<n;i++)
{
vis[i]=0;
for(int j=0;j<k;j++)
{IO::read(a[j]);que[j].push(mkp(a[j],i));}
for(int j=0;j<k;j++)
IO::read(b[i][j]);
// cin>>b[i][j];
}
int cnt=0;
for(int i=0;i<=k*2*n;i++)
{
int flag=0;
for(int j=0;j<k;j++)
if(que[j].size())
{
tmp=que[j].top();
if(tmp.fi<=man[j])
{
vis[tmp.se]++;
que[j].pop();
flag=1;
}
if(vis[tmp.se]==k)
{
for(int kk=0;kk<k;kk++)
man[kk]+=b[tmp.se][kk];
cnt++;
}
}
if(flag==0)
break;
}
printf("%d\n",cnt);
for(int i=0;i<k-1;i++)
printf("%d ",man[i]);
printf("%d\n",man[k-1]);
}
}
/*
1
2 2
100 100
100 1000 1 1
101 1 1 1
*/