HDU 6396 Swordsman(优先队列+超神输入挂)

Swordsman

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 643    Accepted Submission(s): 180


 

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.

题意:给你T组输入。每组输入包含n(n<=1e5),m(1<=m<=5) ,然后给出你的m个属性的初始值。

然后对于n种怪,给出每个怪的m个属性和吃掉它你的m个属性能提高的值。如果你的每种属性都大于等于怪的对应的属性,你就可以吃掉这个怪。求你最多能吃掉多少怪,输出你最终的m个属性的值。

思路:优先队列。用m个优先队列(小的在队头,用pair,默认按第一个排序)。首先把所有怪的第一个属性和怪的编号(用pair)加入到第一个队列当中。每次从第1个属性开始到第m个属性,如果当前属性小于等于你的属性,就把这个怪的下一个属性和怪的编号加入到下一个优先队列中。直到最后一个优先队列中对应的属性小于等于你的最后一个属性,就累加吃掉这个怪你能提高的每个属性的值。然后此时你的各项属性都已经提高,再重复此操作直到吃掉的怪数不再变化。然而这样还是会TLE。需要抄一个超神输入挂。

代码:

#include<bits/stdc++.h>
#define ll long long
using namespace std;

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;
// void read(int &x){scanf("%d",&x);} //调试的时候用这个,把上面的注释掉。

const int maxn=100010;
int n,m,k,a[maxn][10],b[maxn][10],v[maxn];
typedef pair<int, int> pp;
typedef priority_queue< pp, vector<pp>, greater<pp> > QQ;
QQ q[10],p;
int main()
{
    int T,cas=1;
    read(T);
    while(T--)
    {
       read(n);read(m);
        for(int i=0;i<m;i++) read(v[i]);
        for(int i=0;i<m;i++) q[i]=QQ();
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<m;j++)
            read(a[i][j]);
            q[0].push({a[i][0],i});
            for(int j=0;j<m;j++)
            read(b[i][j]);
        }
        int ans=0,tmp=-1;
        while(tmp!=ans){
        tmp=ans;
        for(int i=0;i<m-1;i++)
        {
            while(!q[i].empty()&&q[i].top().first<=v[i])
            {
                int x=q[i].top().second;q[i].pop();
                q[i+1].push({a[x][i+1],x});
            }
        }
        while(!q[m-1].empty()&&q[m-1].top().first<=v[m-1])
            {
                int x=q[m-1].top().second;q[m-1].pop();
                ++ans;
                for(int i=0;i<m;i++)
                v[i]+=b[x][i];
            }
        }
        int flag=1;
        printf("%d\n",ans);
        for(int i=0;i<m;i++)
        if(flag){flag=0;printf("%d",v[i]);}
        else printf(" %d",v[i]);
        puts("");
    }
      //  if(flag) puts("Yes"); else puts("No");
    return 0;
}

 

对于HDU4546问题,还可以使用优先队列(Priority Queue)来解决。以下是使用优先队列的解法思路: 1. 首先,将数组a进行排序,以便后续处理。 2. 创建一个优先队列(最小堆),用于存储组合之和的候选值。 3. 初始化优先队列,将初始情况(即前0个数的组合之和)加入队列。 4. 开始从1到n遍历数组a的元素,对于每个元素a[i],将当前队列中的所有候选值取出,分别加上a[i],然后再将加和的结果作为新的候选值加入队列。 5. 重复步骤4直到遍历完所有元素。 6. 当队列的大小超过k时,将队列中的最小值弹出。 7. 最后,队列中的所有候选值之和即为前k小的组合之和。 以下是使用优先队列解决HDU4546问题的代码示例: ```cpp #include <iostream> #include <vector> #include <queue> #include <functional> using namespace std; int main() { int n, k; cin >> n >> k; vector<int> a(n); for (int i = 0; i < n; i++) { cin >> a[i]; } sort(a.begin(), a.end()); // 对数组a进行排序 priority_queue<long long, vector<long long>, greater<long long>> pq; // 最小堆 pq.push(0); // 初始情况,前0个数的组合之和为0 for (int i = 0; i < n; i++) { long long num = pq.top(); // 取出当前队列中的最小值 pq.pop(); for (int j = i + 1; j <= n; j++) { pq.push(num + a[i]); // 将所有加和结果作为新的候选值加入队列 num += a[i]; } if (pq.size() > k) { pq.pop(); // 当队列大小超过k时,弹出最小值 } } long long sum = 0; while (!pq.empty()) { sum += pq.top(); // 求队列中所有候选值之和 pq.pop(); } cout << sum << endl; return 0; } ``` 使用优先队列的方法可以有效地找到前k小的组合之和,时间复杂度为O(nklog(k))。希望这个解法对你有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值