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?
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.
题意:n个机器人,每个机器人有m个值,你可以射击k次,每个机器人的m个值全为0,则认为机器人dead,问,最多让多少个机器人dead(要求连续)
思路:线段树查询区间最大值,二分死亡的机器人个数,枚举区间起点,区间m个值的最大值的和如果小于k则满足条件
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
const int INF=100000;
const int SIGMA_SIZE=6;
int a[maxn][SIGMA_SIZE];
int tmp[SIGMA_SIZE],ans[SIGMA_SIZE];
int N,M,K;
struct IntervalTree
{
int maxv[maxn<<2][SIGMA_SIZE];
void build(int o,int l,int r)
{
if(l==r)
{
for(int i=1;i<=M;i++)
maxv[o][i]=a[l][i];
return ;
}
int mid=(l+r)>>1;
build(o<<1,l,mid);
build(o<<1|1,mid+1,r);
pushup(o);
}
void pushup(int o)
{
for(int i=1;i<=M;i++)
maxv[o][i]=max(maxv[o<<1][i],maxv[o<<1|1][i]);
}
void query(int o,int l,int r,int q1,int q2)
{
if(q1<=l&&r<=q2)
{
for(int i=1;i<=M;i++)
tmp[i]=max(tmp[i],maxv[o][i]);
return ;
}
int mid=(l+r)>>1;
if(q1<=mid)query(o<<1,l,mid,q1,q2);
if(q2>mid)query(o<<1|1,mid+1,r,q1,q2);
}
}tree;
bool can(int len)
{
int sum=0;
for(int i=1;i<=N-len+1;i++)
{
memset(tmp,0,sizeof(tmp));
tree.query(1,1,N,i,i+len-1);
int cnt=0;
for(int j=1;j<=M;j++)
cnt+=tmp[j];
if(cnt<=K)
{
for(int j=1;j<=M;j++)ans[j]=tmp[j];
return true;
}
}
return false;
}
void solve()
{
int l=0,r=N;
while(l<r)
{
int mid=l+(r-l+1)/2;
if(can(mid))l=mid;
else r=mid-1;
}
for(int i=1;i<=M;i++)printf("%d ",ans[i]);
printf("\n");
}
int main()
{
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]);
tree.build(1,1,N);
solve();
}
return 0;
}