原题链接:
http://codeforces.com/contest/514/problem/D
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.
题目大意:
n个机器人,每个机器人有m个零件,每个机器人的每个零件有不同的生命值ai,一个人有m个武器,每个武器分别对应攻击m个零件中的一个,现在他一共可以发k发子弹,他每次用第i个武器发射k枚子弹,所有机器人的第i个零件的生命值都会降i,生命值的最低是0,而已知一个机器人死掉的条件是它的每个零件生命值都为0,输出当方案能够打死连续的数量最多的机器人时,每一种武器分别打出了多少子弹。
思路:
①由于m很小,线段树维护区间每种零件的最大生命值。
②二分最长连续可以打死的机器人数量,枚举是否存在长度为mid的区间满足总共需要的子弹数小于k。
//交两次居然就过了有点小惊讶,因为很少能做出D题,虽然是比赛结束以后,还是比较开心~~不过代码写得比较挫
代码:
#include "stdio.h"
#include "iostream"
#include "string.h"
#include "stdlib.h"
#include "algorithm"
#include "math.h"
using namespace std;
const int MAXN=100010;
int a[MAXN][5];
int n,m,k;
struct NODE{
int a[5];
int left,right;
}node[4*MAXN];
struct ANS{
int a[5];
}ans2;
void PushUp(int t)
{
for(int i=0;i<m;i++)
{
node[t].a[i]=max(node[t<<1].a[i],node[(t<<1)+1].a[i]);
}
}
void CreatTree(int l,int r,int t)
{
node[t].left=l;
node[t].right=r;
if(l==r)
{
for(int i=0;i<m;i++)
node[t].a[i]=a[l][i];
return;
}
CreatTree(l,(l+r)/2,t<<1);
CreatTree((l+r)/2+1,r,(t<<1)+1);
PushUp(t);
}
struct ANS max2(struct ANS a1,struct ANS a2)
{
struct ANS ans;
for(int i=0;i<m;i++)
ans.a[i]=max(a1.a[i],a2.a[i]);
return ans;
}
struct ANS Query(int l,int r,int t)
{
ANS ans;
for(int i=0;i<m;i++)
ans.a[i]=0;
if(node[t].left==l&&node[t].right==r)
{
for(int i=0;i<m;i++)
ans.a[i]=node[t].a[i];
return ans;
}
int w=t<<1;
if(l<=node[w].right)
{
if(r<=node[w].right)
ans=max2(Query(l,r,w),ans);
else
ans=max2(Query(l,node[w].right,w),ans);
}
w+=1;
if(r>=node[w].left)
{
if(l>=node[w].left)
ans=max2(Query(l,r,w),ans);
else
ans=max2(Query(node[w].left,r,w),ans);
}
return ans;
}
int fun(int mid)
{
for(int i=1;i<=n-mid+1;i++)
{
ans2=Query(i,i+mid-1,1);
int sum=0;
for(int j=0;j<m;j++)
{
sum+=ans2.a[j];
}
if(sum<=k)
return 1;
}
return 0;
}
int main()
{
scanf("%d%d%d",&n,&m,&k);
for(int i=1;i<=n;i++)
for(int j=0;j<m;j++)
scanf("%d",&a[i][j]);
CreatTree(1,n,1);
long long r,l,mid;
l=1;
r=n;
struct ANS ans3;
for(int i=0;i<m;i++)
ans3.a[i]=0;
while(l<=r)
{
mid=(l+r)/2;
if(fun(mid))
{
ans3=ans2;
l=mid+1;
}
else
r=mid-1;
}
for(int i=0;i<m;i++)
printf("%d ",ans3.a[i]);
printf("\n");
return 0;
}