您的位置:首页 > 其它

R2D2 and Droid Army : CodeForces - 514D RMQ+单调性

2017-06-14 14:52 274 查看

题目:CodeForces - 514D

time limit per test2 seconds

memory limit per test256 megabytes

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.

Examples

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个武器,第i个武器可以使所有机器人的第i个部件减少一个.

求,在至多使用k次武器的情况下,能够摧毁的机器人的最长连续长度是多少.一个机器人被摧毁当且仅当所有部件个数都为0.

输出每件武器的使用次数,a1,a2,a3,...am,∑mi=1ai≤k.

思路:

利用RMQ我们可以求得一串机器人m种部件各自的最大值,若这些最大值相加<=k,则说明可以歼灭这一串机器人.

很多人说用二分+RMQ,其实不用,因为我们可以发现最长长度len是符合单调性的,而且如果[l,l+len-1]符合条件的话,所有[l,l+i-1](i<=len)都会符合条件.所以我们只要不断更新扩大这个len值,若RMQ[i,i+len-1]>k,则直接跳过i,否则不断扩大len即可.由于每个点最多只可能扩展一次,所以算法的复杂度是有保证的.

算法复杂度O(NlogN+N)主要是RMQ预处理的开销.

代码:

#include <bits/stdc++.h>
#define rep(i,a,b) for(int i=(a);i<=(b);++i)
#define per(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
const int MAX_N = 100000 +3;
int maxm[MAX_N];
int A[MAX_N][6],f[6][MAX_N][18];
int N,M,K;
void initRMQ()
{
rep(k,1,M)
rep(i,1,16)
rep(j,1,N)
f[k][j][i] = max(f[k][j][i-1],f[k][j+(1<<i-1)][i-1]);
}
inline int log2(int x)
{
int a=0;
for(int t=1; t<=x; t<<=1,++a);
return --a;
}
int RMQ(int m,int l,int r)
{
int tmp = log2(r-l+1);
return max(f[m][l][tmp],f[m][r-(1<<tmp)+1][tmp]);
}
int totRMQ(int l,int r)
{
int tot=0;
rep(i,1,M)
tot+=RMQ(i,l,r);
return tot;
}
int main()
{
scanf("%d%d%d",&N,&M,&K);
rep(i,1,N)
rep(j,1,M) {
scanf("%d",&A[i][j]);
f[j][i][0] = A[i][j];
}
initRMQ();
int l,r,ansl,ansr,len=0;
for (l=1; l<=N; ++l){
while (l+len<=N && totRMQ(l,l+len)<=K) ++len,ansl=l;
}
rep(i,1,M)
printf("%d ",len>0?RMQ(i,ansl,ansl+len-1):0);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  RMQ