D. R2D2 and Droid Army
time limit per test
memory limit per test
input
output
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
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
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.
用RMQ求区间最大值
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int dp[5][100005][20];
int n, m, k;
int p1[5], p2[5];
void RMQ(int h){
for(int i = 1; (1<<i) <= n; i++){
for(int j = 0; j+(1<<i) <= n; j++){
dp[h][j][i] = max(dp[h][j][i-1], dp[h][j+(1<<(i-1))][i-1]);
}
}
}
int Query(int l, int r){
int k = 0;
while(1<<(1+k) <= r - l + 1)
k++;
int ans = 0;
for(int i = 0; i < m; i++){
p2[i]= max(dp[i][l][k], dp[i][r-(1<<k)+1][k]);
ans += p2[i];
}
return ans;
}
int main(){
// freopen("in.txt", "r", stdin);
scanf("%d%d%d", &n, &m, &k);
for(int i = 0; i < n; i++){
for(int j = 0; j < m; j++){
scanf("%d", &dp[j][i][0]);
}
}
for(int i = 0; i < m; i++){
RMQ(i);
}
int l = 0, maxs = 1;
while(l + maxs <= n){
int d = Query(l, l+maxs-1);
if(k >= d){
memcpy(p1, p2, sizeof(p2));
maxs++;
}
else
l++;
}
printf("%d", p1[0]);
for(int i = 1; i < m; i++)
printf(" %d", p1[i]);
puts("");
return 0;
}