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?
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.
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.
5 2 4 4 0 1 2 2 1 0 2 1 3
2 2
3 2 4 1 2 1 3 2 2
1 3
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.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXNODE = 400000+1000;
const int MAX = 1000003;
struct NODE
{
int value[6]; // 结点对应区间的权值
int left,right; // 区间 [left,right]
} node[MAXNODE];
int father[MAX]; // 每个点(当区间长度为0时,对应一个点)对应的结构体数组下标
int a[MAXNODE][6];
int n,m,k;
int temp[6],ans[6];
void BuildTree(int rt,int left,int right)
{
node[rt].left = left;
node[rt].right = right;
if (left == right)
{
for(int i = 1; i <= m; i++ )
{
node[rt].value[i] = a[left][i];
}
return;
}
int mid = (right+left)/2;
BuildTree(rt<<1, left, mid );
BuildTree(rt<<1|1, mid + 1, right);
for(int i = 1; i <= m; i++)
{
node[rt].value[i] = max(node[rt<<1].value[i], node[rt<<1|1].value[i]);
}
}
int Query(int i,int l,int r,int x) // i为区间的序号(对应的区间是最大范围的那个区间,也是第一个图最顶端的区间,一般初始是 1 啦)
{
if (l== node[i].left && node[i].right == r) // 找到了一个完全重合的区间
{
return node[i].value[x];
}
int Max = 0;
i = i << 1; // get the left child of the tree node
if (l <= node[i].right) // 左区间有涉及
{
if (r <= node[i].right) // 全包含于左区间,则查询区间形态不变
Max = Query(i, l, r,x);
else // 半包含于左区间,则查询区间拆分,左端点不变,右端点变为左孩子的右区间端点
Max =Query(i,l, node[i].right,x);
}
i += 1; // right child of the tree
if (r >= node[i].left) // 右区间有涉及
{
if (l >= node[i].left) // 全包含于右区间,则查询区间形态不变
Max =max(Max, Query(i, l, r,x));
else // 半包含于左区间,则查询区间拆分,与上同理
Max =max(Max, Query(i, node[i].left, r,x));
}
return Max;
}
bool ok(int mid)
{
bool flag = false;
for(int i = 1; i <= n - mid+1; i++)
{
int num = 0;
//cout<<"i = "<<i;
for(int j = 1; j <= m; j++)
{
temp[j] = Query(1,i, i+mid-1 , j);
num += temp[j];
//cout<<temp[j]<<" ";
}
// cout<<endl;
if(num <= k)
{
flag = true;
memcpy(ans,temp,sizeof(temp));
// cout<<i<<" "<<ans[1]<<" "<<ans[2]<<endl;
break;
}
fill(temp,temp+6,0);
}
if(flag) return true;
else return false;
}
void solve()
{
int left = 1, right = n;
while(left <= right)
{
int mid = (left + right)/2;
// cout<<mid<<endl;
if(ok(mid))
{
left = mid + 1;
}
else right = mid - 1;
}
printf("%d",ans[1]);
for(int i = 2; i <= m; i++) printf("% d",ans[i]);
printf("\n");
}
int main()
{
#ifdef xxz
freopen("in.txt","r",stdin);
#endif // xxz
while(scanf("%d%d%d",&n,&m,&k) != EOF)
{
for(int i = 1; i <= n; i++)
for(int j = 1; j <= m; j++)
scanf("%d",&a[i][j]);
BuildTree(1,1,n);
solve();
}
return 0;
}