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
题意:给你n个机器人,你有k次发炮的机会,每次可以降低所有的机器人的某一种能力的一点,当机器人的能力值都为0时机器人死亡,问当连续的机器人死亡数目最多时,m个能力各发多少次炮。
思路:二分这个长度,然后每次用队列来从头到尾得到l-r的各个能力的最大值。
AC代码如下:
#include<cstdio>
#include<cstring>
#include<deque>
using namespace std;
typedef long long ll;
int n,m,num[100010][6],ans[6];
deque<int> qu[6];
ll temp,k;
bool solve(int len)
{
int i,j;
for(i=1;i<=m;i++)
while(!qu[i].empty())
qu[i].pop_back();
for(i=1;i<=n;i++)
{
for(j=1;j<=m;j++)
{
while(!qu[j].empty() && qu[j].back()<num[i][j])
qu[j].pop_back();
qu[j].push_back(num[i][j]);
}
if(i<len)
continue;
temp=0;
for(j=1;j<=m;j++)
temp+=qu[j].front();
if(temp<=k)
{
for(j=1;j<=m;j++)
ans[j]=qu[j].front();
return true;
}
for(j=1;j<=m;j++)
if(qu[j].front()==num[i-len+1][j])
qu[j].pop_front();
}
return false;
}
int main()
{
int i,j,l,r;
scanf("%d%d%I64d",&n,&m,&k);
for(i=1;i<=n;i++)
for(j=1;j<=m;j++)
scanf("%d",&num[i][j]);
l=1;r=n;
solve(1);
while(l<r)
{
int mi=(l+r)/2;
if(solve(mi))
l=mi+1;
else
r=mi;
}
for(i=1;i<=m;i++)
printf("%d ",ans[i]);
}