CodeForces 514D R2D2 and Droid Army

D. R2D2 and Droid Army

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
Note

In the first test the second, third and fourth droids will be destroyed.

In the second test the first and second droids will be destroyed.

题意:有n个机器人,每个机器人有m个标识(用来表示属性),R2D2这个武器最多能打K次,每打一次,所有机器人相对应的属性就要减1,问至少要打几次,能使得连续的机器人属性全部变为0的长度最大,将每个属性打的枪数全部输出来。

思路:很明显这道题要用到线段树,用来记录连续区域间每个属性的最大值(即在这个区间内要把所有的机器人的属性全部变为0所需要打的次数),为了寻找那个最长的连续区域,我们需要用到二分,避免超时。

代码如下:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define Mid(x,y) (x+y)>>1
const int maxn=1e5+10;
int a[maxn][6], ans[6], temp[6];
__int64 k;
int m,n;

struct node
{
	int left, right, val[6];
}rob[4*maxn];

void Build(int l, int r, int rt)
{
	rob[rt].left=l;
	rob[rt].right=r;	
	if(l==r)
	{
		for(int i=1; i<=m; i++)
		{
			rob[rt].val[i]=a[l][i];
		}
		return;
	}
	int mid=Mid(l,r);
	Build(l, mid, L(rt));
	Build(mid+1, r, R(rt));
	for(int i=1; i<=m; i++)
	{
		rob[rt].val[i]=max(rob[L(rt)].val[i], rob[R(rt)].val[i]);
	}
}

int Query(int l, int r, int rt, int x)
{
	if(l==rob[rt].left && r==rob[rt].right)
	{
		return rob[rt].val[x];
	}
	int Max=0;
	if(l<=rob[L(rt)].right)
	{
		if(r<=rob[L(rt)].right)
		{
			Max=Query(l,r,L(rt), x);
		}
		else
		{
			Max=Query(l,rob[L(rt)].right, L(rt), x);
		}
	}
	if(r>=rob[R(rt)].left)
	{
		if(l>=rob[R(rt)].left)
		{
			Max=Query(l,r,R(rt),x);
		}
		else Max=max(Max, Query(rob[R(rt)].left, r, R(rt), x));
	}
	return Max;
}

bool OK(int mid)
{
	bool flag=false;
	for(int i=1; i<=n-mid+1; i++)
	{
		int num=0;
		for(int j=1; j<=m; j++)
		{
			temp[j]=Query(i, i+mid-1, 1, j);
			num+=temp[j];
		}
		if(num<=k)
		{
			flag=true;
			memcpy(ans,temp,sizeof(temp));
			break;
		}
		fill(temp, temp+6, 0);
	}
	if(flag) return true;
	else return false;
}

int main()
{
	while(scanf("%d%d%I64d", &n, &m, &k)!=EOF)
	{
		for(int i=1; i<=n; i++)
		{
			for(int j=1; j<=m; j++)
			{
				scanf("%d", &a[i][j]);
			}
		}
		Build(1,n,1);
	    int le=1, ri=n;
	    while(le<=ri)
	    {
	    	int mid=(le+ri)/2;
	    	if(OK(mid))
			{
				le=mid+1;
			}
			else ri=mid-1;
	    }
	   // printf("%d", ans[1]);
	    for(int i=1; i<m; i++)
	    {
	    	printf("%d ", ans[i]);
	    }
	    printf("%d\n", ans[m]);
	}
	return 0;
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值