您的位置:首页 > 其它

R2D2 and Droid Army--CSU-ACM2017暑假集训比赛1

2017-07-26 17:32 337 查看
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.

Example

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个属性值。现可以用m种武器打机器人,打一次则所有机器人的相应属性减1. 当一个机器人所有属性都减为0时就死亡。一共可以打k次,问分别用这m种武器打多少枪可以使得杀死的连在一起的机器人最多?

思路:

1、比较经典的模型,对于连续的X个人,假如都将其干掉的时候,需要对于每种属性使用的最少操作,就是对应这连续的X个人每种属性的最大值。

2、那么问题转化到区间最大值上来,这里我们可以使用RMQ来解,也可以用线段树来解。

接下来我们可以考虑枚举人数,然后O(NLogN)的去维护当前情况是否可行,直到枚举到不可行为止前的那个答案,就是最终答案。

由此看来,枚举人数是具有单调性的,要干掉更多的人,就需要更多的操作,那么我们可以二分这个人数。

对于可行方案,增加人数,不可行方案,减少人数。

3、二分过程中,维护最后一次可行解的答案,输出即可。

AC代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>

using namespace std;

const int k=log(100009.0)/log(2.0)+1;

int store[100009][9];
int STstore[100009][9][k];
int n,m,l,cnt,finalcnt;
int ans[9],output[9];

void STini()
{
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
STstore[i][j][0]=store[i][j];
for(int x=1;x<=m;x++)
for(int j=1;j<=k;j++)
for(int i=1;i<=n;i++)
{
if(i+(1<<j)-1>n)break;
STstore[i][x][j]=max(STstore[i][x][j-1],STstore[i+(1<<(j-1))][x][j-1]);
}
}

bool judge(int mid)
{
bool flag=0;
cnt=1;
for(int i=1;i<=n;i++)
{
if(i+mid-1>n)break;
int a=i,b=i+mid-1;
int len=log(double(mid))/log(double(2));
for(int z=1;z<=m;z++)
{
ans[z]=max(STstore[a][z][len],STstore[b-(1<<len)+1][z][len]);
}

int sum=0;
for(int z=1;z<=m;z++)
sum+=ans[z];
if(sum<=l)
{

for(int z=1;z<=m;z++)
output[z]=ans[z];
flag=1;
}
}

return flag;

}

int main()
{
cin>>n>>m>>l;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
scanf("%d",&store[i][j]);

STini();

int ll=0,rr=n,mid;
while(ll<=rr)
{
mid=(ll+rr)/2;
if(judge(mid))
ll=mid+1;
else
rr=mid-1;
}

for(int i=1;i<=m;i++)
{
printf("%d ",output[i]);
}
printf("\n");

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: