CodeForces 514D-R2D2 and Droid Army

2 篇文章 0 订阅
2 篇文章 0 订阅

题目

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 the i-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 ≤ 105, 1 ≤ m ≤ 5, 0 ≤ 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.

Examples
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个描述,你有k次机会,每次都可以选择m中的一个,让n个机器人的这个描述减1,当某个机器人的m个描述都减为0了,这个机器人就没了,问的是用这k次机会可以消灭最多多少连续的机器人。

解法

如果某一段[l,r]是可以被消灭的,那么机器人每个描述在[l,r]上的最大值的和必须小于等于k,那么就可以先做m遍rmq,然后枚举左端点,二分右端点,取到最右的端点时更新答案就行了。

代码

#include<cstdio>
#include<cstring>
int a[100005][6],maxn[100005][20][6];
int max(int a,int b)
{
	return a>b?a:b;
}
void init(int n,int m)
{
	int i,j,k;  
    for(i=0;i<n;i++)  
    	for(j=0;j<m;j++)
        	maxn[i][0][j]=a[i][j];  
    for(j=1;(1<<j)<=n;j++)
        for(i=0;i+(1<<j)-1<n;i++)  
        	for(k=0;k<m;k++)
            	maxn[i][j][k]=max(maxn[i][j-1][k],maxn[i+(1<<(j-1))][j-1][k]);  
}
int rmq(int l,int r,int id)
{
	int k=0;
    while((1<<(k+1))<=r-l+1) k++;  
    return max(maxn[l][k][id],maxn[r-(1<<k)+1][k][id]);  
}
int f(int l,int r,int m)
{
	int i,res=0;
	for(i=0;i<m;i++)
		res+=rmq(l,r,i);
	return res;
}
int main()
{
	int n,m,k,i,j;
	scanf("%d%d%d",&n,&m,&k);
	for(i=0;i<n;i++)
		for(j=0;j<m;j++)
			scanf("%d",&a[i][j]);
	init(n,m);
	int ans=-1,pos1,pos2;
	for(i=0;i<n;i++)
	{
		int l=i,r=n-1,mid;
		while(l<=r)
		{
			mid=(l+r)/2;
			int tmp=f(i,mid,m);
			if(tmp>k)
				r=mid-1;
			else
				l=mid+1;
		}
		int res=r-i+1;
		if(res>ans)//记录一下答案对应的左右端点
		{
			ans=res;
			pos1=i;
			pos2=r;
		}
	}
	if(ans<=0)//如果不能消灭任何机器人,就输出m个0
	{
		for(i=0;i<m;i++)
			printf("0 ");
		puts("");
		return 0;
	}
	for(i=0;i<m;i++)
		printf("%d ",rmq(pos1,pos2,i));
	puts("");
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值