码迷,mamicode.com
首页 > 其他好文 > 详细

CodeForces 514D R2D2 and Droid Army

时间:2015-04-17 11:39:56      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:c++   codeforces   

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;
} 


CodeForces 514D R2D2 and Droid Army

标签:c++   codeforces   

原文地址:http://blog.csdn.net/doris1104/article/details/45082221

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!