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.
思路:
这是一道很简单的贪心题,主要是需要用到输入挂。
代码:
#include <bits/stdc++.h>
#define ll long long
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;
using namespace std;
const ll mod=1e9+7;
const int maxn=5e6+100;
int a[10];
struct point
{
int w[10];
int v[10];
}G[maxn];
struct poin
{
int id,sum;
poin(int id,int sum):id(id),sum(sum){}
poin(){}
bool operator <(const poin &b)const
{
return sum<b.sum;
}
bool operator >(const poin &b)const
{
return sum>b.sum;
}
};
priority_queue <poin,vector<poin>,greater<poin> > q[7];
int main()
{
int t;
read(t);
while(t--)
{
int n,m;
read(n);
read(m);
for(int i=1;i<=m;i++)
{
read(a[i]);
}
for(int i=0;i<n;i++)
{
for(int j=1;j<=m;j++)
{
read(G[i].w[j]);
if(j==1)q[j].push(poin(i,G[i].w[1]));
}
for(int j=1;j<=m;j++)
{
read(G[i].v[j]);
}
}
int ans=0;
int pre=0;
while(1)
{
for(int i=1;i<=m;i++)
{
while(!q[i].empty())
{
poin e=q[i].top();
int id=e.id;
//cout<<e.id<<" "<<e.sum<<endl;
if(e.sum<=a[i])
{
q[i].pop();
if(i!=m)e.sum=G[id].w[i+1];
q[i+1].push(e);
}
else break;
}
//cout<<i+1<<"*********"<<endl;
}
pre=ans;
while(!q[m+1].empty())
{
ans++;
poin e=q[m+1].top();
q[m+1].pop();
int id=e.id;
for(int i=1;i<=m;i++)
{
a[i]+=G[id].v[i];
}
}
if(ans==pre)break;
}
printf("%d\n",ans);
for(int i=1;i<=m;i++)
{
if(i!=m)printf("%d ",a[i]);
else printf("%d\n",a[i]);
}
for(int i=0;i<=6;i++)
{
while(!q[i].empty())q[i].pop();
}
}
return 0;
}