• HDU


    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.
    
    

    题解:

    学过OS的应该看到这道题就觉的很熟悉,毕竟这道题的思想类似银行家算法(不知道也无所谓,不影响做题)。我实现了银行家算法版本(后面有代码),不过银行家算法是200%T的(复杂度O(n^2))。

    官方给的标准解法:

    这两种方法其实本质都一样,所以我这里只实现了一种。

    代码:

    银行家算法版:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    
    using namespace std;
    
    //--------------------DLS超级加速挂---------------------------------------
    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 == '
    ' || ch == '
    ' || ch == '	';
    	}
    	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;
    //-----------------------------------------------------------
    
    const int MAXN = 100005;
    
    int N,K; 
    
    struct D{
    	int v[6];
    	int EXP[6];
    	bool operator < (const struct D &b) const{
    		for(int i=0 ; i<K ; ++i){
    			if(v[i] == b.v[i])continue;
    			return v[i] < b.v[i];
    		}
    		return false;
    	}
    }data[MAXN];
    
    int board[6];
    bool used[MAXN];
    
    bool Kill(int x){//判断是否能杀 
    	for(int i=0 ; i<K ; ++i){
    		if(board[i] < data[x].v[i])return false;
    	}
    	return true;
    }
    
    inline void Add(int x){//加上怪物奖励属性 
    	for(int i=0 ; i<K ; ++i){
    		board[i] += data[x].EXP[i];
    	}
    }
    
    int main(){
    	
    	int T;
    	read(T);
    	while(T--){
    		read(N),read(K);
    		memset(used,false,sizeof used);
    		for(int i=0 ; i<K ; ++i)read(board[i]);
    		for(int i=0 ; i<N ; ++i){
    			for(int j=0 ; j<K ; ++j)read(data[i].v[j]);
    			for(int j=0 ; j<K ; ++j)read(data[i].EXP[j]);
    		}
    		sort(data,data+N);
    		bool flag = true;
    		int ans = 0;
    		while(flag){
    			flag = false;
    			for(int i=0 ; i<N ; ++i){
    				if(used[i])continue;
    				if(Kill(i)){
    					used[i] = true;
    					Add(i);
    					flag = true;
    					ans++;
    				}
    			}
    		}
    		printf("%d
    ",ans);
    		for(int i=0 ; i<K-1 ; ++i)printf("%d ",board[i]);
    		printf("%d
    ",board[K-1]);
    	}
    	
    	return 0;
    }

    解法1:

    #include <cstdio>
    #include <queue>
    #include <iostream>
    
    using namespace std;
    
    const int MAXN = 100005;
    
    int N,K;
    
    int board[6];
    
    struct D{
    	int value,id;
    	D(int _value,int _id):value(_value),id(_id){}
    };
    
    struct cmp{
    	bool operator ()(struct D a,struct D b){
    		return a.value > b.value;
    	}
    };
    
    priority_queue<D,vector<D>,cmp> Q[6];
    
    struct DD{
    	int v[10];
    	int EXP[10];
    }data[MAXN];
    
    inline void Add(int x){
    	for(int i=0 ; i<K ; ++i){
    		board[i] += data[x].EXP[i];
    	}
    }
    
    inline void init(){
    	for(int i=0 ; i<6 ; ++i)while(!Q[i].empty())Q[i].pop();
    }
    
    //------------------------DLS超级加速挂----------------------------------
    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 == '
    ' || ch == '
    ' || ch == '	';
    	}
    	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;
    //-----------------------------------------------------------
    
    
    int main(){
    	
    	int T;
    	read(T);
    	while(T--){
    		init();
    		read(N),read(K);
    		for(int i=0 ; i<K ; ++i)read(board[i]);
    		for(int i=0 ; i<N ; ++i){
    			for(int j=0 ; j<K ; ++j)read(data[i].v[j]);
    			Q[0].push(D(data[i].v[0],i));
    			for(int j=0 ; j<K ; ++j)read(data[i].EXP[j]);
    		}
    		int ans = 0;
    		while(1){
    			for(int i=0 ; i<K-1 ; ++i){
    				if(Q[i].empty())continue;
    				struct D t = Q[i].top();
    				while(t.value <= board[i]){
    					Q[i+1].push(D(data[t.id].v[i+1],t.id));
    					Q[i].pop();
    					if(Q[i].empty())break;
    					t = Q[i].top();
    				}
    			}
    			if(Q[K-1].empty() || Q[K-1].top().value > board[K-1])break;
    			while(!Q[K-1].empty() && Q[K-1].top().value <= board[K-1]){
    				Add(Q[K-1].top().id);
    				ans++;
    				Q[K-1].pop();
    			}
    		}
    		printf("%d
    ",ans);
    		for(int i=0 ; i<K-1 ; ++i)printf("%d ",board[i]);
    		printf("%d
    ",board[K-1]);
    	}
    	
    	return 0;
    }
  • 相关阅读:
    普通图片转ascii码字符图
    基于RAF的一个小动画框
    HTML5给我们带来了什么
    记坑
    以后的博客可能会偏向游戏开发方向
    总结:js中4类修改样式的方法
    小记:css特殊性
    css小记
    探究绑定事件的this指向以及event传参的小问题
    手把手教你图片转ASCII码图
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514001.html
Copyright © 2020-2023  润新知