Codeforces Round #291 (Div. 2)(D)

来源:互联网 发布:美蓝漫画连接不上网络 编辑:程序博客网 时间:2024/04/28 07:35
D. R2D2 and Droid Army
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard 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 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 ≤ 1051 ≤ m ≤ 50 ≤ 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.

Sample test(s)
input
5 2 44 01 22 10 21 3
output
2 2
input
3 2 41 21 32 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种抢,每种抢发射一次能是所有机器人对应的部件减1,总共不得发射超过k次,求在伤害机器人数目最大(机器人要求连续)的情况下每种抢发射的最少次数。

思路:线段树+二分,线段树用来求区间最大值,先二分出答案(连续伤害的机器人数),然后枚举起点,知道起点,有范围就知道了终点,然后求这范围内需要设计的最大数目看满不满足条件

注意:如果你的线段树的水平写不了叉姐的水准,乖乖的将结点开3~4万吧,开2万的话会RE。。。因为这TM我的水平写线段树写不了最完美的二叉树(mb)
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>using namespace std;const int MAXNODE = 400000+1000;const int MAX = 1000003;struct NODE{    int value[6];        // 结点对应区间的权值    int left,right;   // 区间 [left,right]} node[MAXNODE];int father[MAX];     // 每个点(当区间长度为0时,对应一个点)对应的结构体数组下标int a[MAXNODE][6];int n,m,k;int temp[6],ans[6];void BuildTree(int rt,int left,int right){    node[rt].left = left;    node[rt].right = right;    if (left == right)    {        for(int i = 1; i <= m; i++ )        {            node[rt].value[i] = a[left][i];        }        return;    }    int mid = (right+left)/2;    BuildTree(rt<<1, left, mid );    BuildTree(rt<<1|1, mid + 1, right);    for(int i = 1; i <= m; i++)    {        node[rt].value[i] = max(node[rt<<1].value[i], node[rt<<1|1].value[i]);    }}int  Query(int i,int l,int r,int x)  // i为区间的序号(对应的区间是最大范围的那个区间,也是第一个图最顶端的区间,一般初始是 1 啦){    if (l== node[i].left  && node[i].right == r)  // 找到了一个完全重合的区间    {        return  node[i].value[x];    }    int Max = 0;    i = i << 1; // get the left child of the tree node    if (l <= node[i].right)  // 左区间有涉及    {        if (r <= node[i].right) // 全包含于左区间,则查询区间形态不变            Max = Query(i, l, r,x);        else // 半包含于左区间,则查询区间拆分,左端点不变,右端点变为左孩子的右区间端点            Max =Query(i,l, node[i].right,x);    }    i += 1; // right child of the tree    if (r >= node[i].left)  // 右区间有涉及    {        if (l >= node[i].left) // 全包含于右区间,则查询区间形态不变            Max =max(Max, Query(i, l, r,x));        else // 半包含于左区间,则查询区间拆分,与上同理            Max =max(Max, Query(i, node[i].left, r,x));    }    return Max;}bool ok(int mid){    bool flag = false;    for(int i = 1; i <= n - mid+1; i++)    {        int num = 0;        //cout<<"i = "<<i;        for(int j = 1; j <= m; j++)        {            temp[j] = Query(1,i, i+mid-1 , j);            num += temp[j];            //cout<<temp[j]<<" ";        }        // cout<<endl;        if(num <= k)        {            flag = true;            memcpy(ans,temp,sizeof(temp));            // cout<<i<<" "<<ans[1]<<" "<<ans[2]<<endl;            break;        }        fill(temp,temp+6,0);    }    if(flag) return true;    else return false;}void solve(){    int left = 1, right = n;    while(left <= right)    {        int mid = (left + right)/2;        // cout<<mid<<endl;        if(ok(mid))        {            left = mid + 1;        }        else right = mid - 1;    }    printf("%d",ans[1]);    for(int i = 2; i <= m; i++) printf("% d",ans[i]);    printf("\n");}int main(){#ifdef xxz    freopen("in.txt","r",stdin);#endif // xxz    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]);        BuildTree(1,1,n);        solve();    }    return 0;}


0 0
原创粉丝点击