R2D2 and Droid Army - CodeForces 514 D 二分

R2D2 and Droid Army
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

An army of n droids is lined up in one row. Each droid is described by m integers a1, a2, ..., am, where ai is the number of details of thei-th type in this droid's mechanism. R2-D2 wants to destroy the sequence of consecutive droids of maximum length. He has m weapons, the i-th weapon can affect all the droids in the army by destroying one detail of the i-th type (if the droid doesn't have details of this type, nothing happens to it).

A droid is considered to be destroyed when all of its details are destroyed. R2-D2 can make at most k shots. How many shots from the weapon of what type should R2-D2 make to destroy the sequence of consecutive droids of maximum length?

Input

The first line contains three integers n, m, k (1 ≤ n ≤ 1051 ≤ m ≤ 50 ≤ k ≤ 109) — the number of droids, the number of detail types and the number of available shots, respectively.

Next n lines follow describing the droids. Each line contains m integers a1, a2, ..., am (0 ≤ ai ≤ 108), where ai is the number of details of the i-th type for the respective robot.

Output

Print m space-separated integers, where the i-th number is the number of shots from the weapon of the i-th type that the robot should make to destroy the subsequence of consecutive droids of the maximum length.

If there are multiple optimal solutions, print any of them.

It is not necessary to make exactly k shots, the number of shots can be less.

Sample test(s)
input
5 2 4
4 0
1 2
2 1
0 2
1 3
output
2 2
input
3 2 4
1 2
1 3
2 2
output
1 3

题意:给你n个机器人,你有k次发炮的机会,每次可以降低所有的机器人的某一种能力的一点,当机器人的能力值都为0时机器人死亡,问当连续的机器人死亡数目最多时,m个能力各发多少次炮。

思路:二分这个长度,然后每次用队列来从头到尾得到l-r的各个能力的最大值。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<deque>
using namespace std;
typedef long long ll;
int n,m,num[100010][6],ans[6];
deque<int> qu[6];
ll temp,k;
bool solve(int len)
{
    int i,j;
    for(i=1;i<=m;i++)
       while(!qu[i].empty())
         qu[i].pop_back();
    for(i=1;i<=n;i++)
    {
        for(j=1;j<=m;j++)
        {
            while(!qu[j].empty() && qu[j].back()<num[i][j])
               qu[j].pop_back();
            qu[j].push_back(num[i][j]);
        }
        if(i<len)
          continue;
        temp=0;
        for(j=1;j<=m;j++)
           temp+=qu[j].front();
        if(temp<=k)
        {
            for(j=1;j<=m;j++)
               ans[j]=qu[j].front();
            return true;
        }
        for(j=1;j<=m;j++)
           if(qu[j].front()==num[i-len+1][j])
             qu[j].pop_front();
    }

    return false;
}
int main()
{
    int i,j,l,r;
    scanf("%d%d%I64d",&n,&m,&k);
    for(i=1;i<=n;i++)
       for(j=1;j<=m;j++)
          scanf("%d",&num[i][j]);
    l=1;r=n;
    solve(1);
    while(l<r)
    {
        int mi=(l+r)/2;
        if(solve(mi))
          l=mi+1;
        else
          r=mi;
    }
    for(i=1;i<=m;i++)
       printf("%d ",ans[i]);
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值