hdu 6396 Swordsman 读入优化+思维

roblem 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<map>
#include<set>
#include<queue>
#include<stack>
#include<cmath>
#include<deque>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const int M = 1e5 + 10;
int t;
int n,k;
struct node
{
    int id;
    ll num;
}a[6][M],b[6][M];
ll ans[6];
int kp[6];
int vis[M];
bool cmp(node a,node b)
{
    return a.num<b.num;
}
namespace In  {
# define In_Len 2000000
    static std :: streambuf *fb ( std :: cin.rdbuf ( ) ) ;
    static char buf [In_Len], *ss ( 0 ), *tt ( 0 ) ;
# define pick( )  ( (ss == tt) ? ( tt = buf + fb -> sgetn ( ss = buf, In_Len ), ((ss == tt) ? -1 : *(ss ++)) ) :*(ss ++) )

    inline char getc ( )  {  register char c; while ( isspace ( c = pick ( ) ) ) ;  return c ;  }

    inline int read ( )  {
        register int x, c ;
        bool opt ( 1 ) ;
        while ( isspace ( c = pick ( ) ) && ( c ^ -1 ) ) ;
        if ( c == 45 )  c = pick ( ), opt = 0 ;
        for ( x = -48 + c ; isdigit ( c = pick ( ) ) ; ( x *= 10 ) += c - 48 ) ;
        return opt ? x : -x ;
    }
# undef pick
# undef In_Len
}
using namespace In;
namespace Out  {
# define Out_Len 2000000
    std :: streambuf *fb ( std :: cout.rdbuf ( ) ) ;
    char buf [Out_Len], *ss ( buf ), *tt ( buf + Out_Len ) ;
# define echo( c )  ( (ss == tt) ? ( fb -> sputn ( ss = buf, Out_Len ), *ss ++ = c ) : ( *ss ++ = c ) )
    inline void putc ( char c )  {  echo ( c ) ;  }

    inline void print ( register int x )  {
        static int st [30], tp ( 0 ) ;
        if ( ! x )  {  echo ( 48 ) ; return ;  }
        if ( x < 0 )  {  echo ( 45 ) ; x = -x ;  }
        while ( x ) st [++ tp] = x % 10 | 48, x /= 10 ;
        while ( tp )  {  echo ( st [tp] ) ; -- tp ;  }
    }

    inline void flush ( )  {  fb -> sputn ( buf, ss - buf ) ;  }

# undef echo
# undef Out_Len
}
using namespace Out;
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        n=read();
        k=read();
        //scanf("%d%d",&n,&k);
        for(int i=0;i<k;i++)
            ans[i]=read();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<k;j++)
            {
                a[j][i].num=read();
                a[j][i].id=i;
            }
            for(int j=0;j<k;j++)
            {
                b[j][i].num=read();
                b[j][i].id=i;
            }
        }
        memset(vis,0,sizeof(vis));
        for(int i=0;i<k;i++)
            sort(a[i],a[i]+n,cmp);
        int cnt=0;
        memset(kp,0,sizeof(kp));
        while(1)
        {
            int ok=cnt;
            for(int i=0;i<k;i++)
            {
                while(kp[i]<n && a[i][kp[i]].num<=ans[i])
                {
                    vis[a[i][kp[i]].id]++;
                    if(vis[a[i][kp[i]].id]==k)
                    {
                        cnt++;
                        for(int kk=0;kk<k;kk++)
                            ans[kk]+=b[kk][a[i][kp[i]].id].num;
                    }
                    kp[i]++;
                }
            }
            if(ok==cnt)
                break;
        }
        printf("%d\n",cnt);
        for(int i=0;i<k;i++)
        {
            printf("%lld%c",ans[i],i==k-1?'\n':' ');
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/liluoyu_1016/article/details/81774673