Codeforces 514 D R2D2 and Droid Army(RMQ+二分)

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?≤?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.

Sample test(s)

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),RMQ对于查询来说非常方便。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<bitset>
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
typedef long long LL;
typedef pair<int,int>pil;
const int INF = 0x3f3f3f3f;
const int maxn=1e5+10;
int n,m,kk;
int dp[10][maxn][20];//第j种特性
int num[maxn][10];
int res[10],ans[10];
void init()
{
    REPF(i,1,n)
      REPF(j,1,m)//多个
        dp[j][i][0]=num[i][j];
    for(int k=1;k<=m;k++)
      for(int j=1;(1<<j)<=n;j++)
        for(int i=1;i+(1<<j)-1<=n;i++)
              dp[k][i][j]=max(dp[k][i][j-1],dp[k][i+(1<<(j-1))][j-1]);
}
int RMQ(int id,int l,int r)
{
    int k=(int)(log(r-l+1)/log(2.0));
    return max(dp[id][l][k],dp[id][r-(1<<k)+1][k]);
}
bool ok(int x)
{
    for(int i=1;i+x-1<=n;i++)
    {
        int sum=0;
        for(int j=1;j<=m;j++)
        {
            res[j]=RMQ(j,i,i+x-1);
            sum+=res[j];
        }
        if(sum<=kk)
        {
            REPF(j,1,m)  ans[j]=res[j];
            return true;
        }
    }
    return false;
}
void BS()
{
    int l=0,r=n;
    while(l<=r)
    {
        int mid=(l+r)>>1;
        if(ok(mid)) l=mid+1;
        else r=mid-1;
    }
}
int main()
{
    while(~scanf("%d%d%d",&n,&m,&kk))
    {
        REPF(i,1,n)
          REPF(j,1,m)  scanf("%d",&num[i][j]);
        init();BS();
        REPF(i,1,m)  printf("%d ",ans[i]);
        puts("");
    }
    return 0;
}
时间: 2024-10-08 14:51:05

Codeforces 514 D R2D2 and Droid Army(RMQ+二分)的相关文章

CodeForces 514D R2D2 and Droid Army RMQ+二分

题目链接:点击打开链接 题意:给定n m k 下面是n*m的矩阵 最多可以操作k次,每次操作可以使任意一列上所有的数 -= 1,( 0还是0) 要求得到连续最多的行数(每行里的整数都为0),输出任意一个方案(在每一列上操作的次数) 思路: 把每列单独考虑 枚举每行,二分找这行往下最多能清空的行数, RMQ维护一列的最大值. import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.PrintWr

Codeforces #291 (Div. 2) D. R2D2 and Droid Army(RMQ+二分)

题意: 有n*m的矩阵,然后你有k发子弹.现在你可以朝着任意列发射子弹,每一发子弹都会使该列上的数值-1,最小减少到0. 现在问你连续最长的行数,在k发子弹内,使得这些行上的数值全部为0. 思路: 简单的二分枚举最长行数区间,每个区间的最大值决定了要发射的子弹数,所以是RMQ问题,当然这里的枚举全部枚举,用尺取法也可以. //889 ms #include<cstdio> #include<algorithm> #include<cstring> #include<

codeforces 514D R2D2 and Droid Army

题目: 思路: 尺取法+堆

【Codeforces #291 B】R2D2 and Droid Army

因为题目中要求使连续死亡的机器人最多,令人联想到二分答案. 考虑如何检验这之中是否存在一段连续的长度为md的区间,其中花最多k步使得它们都死亡. 这个条件等价于区间中m个最大值的和不超过k. 枚举起点,可以用 $ O(mlogn) $ 的时间确定这段区间是否合法,最终check的复杂度是 $ O(nmlogn) $. 总复杂度是 $ O(nmlog^{2}n) $. $ \bigodot $ 技巧&套路: 最大(小)值的问题,可以考虑二分答案. check时用线段树优化区间平移,来枚举每一个长度

hdu 5289 Assignment(2015多校第一场第2题)RMQ+二分(或者multiset模拟过程)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:给你n个数和k,求有多少的区间使得区间内部任意两个数的差值小于k,输出符合要求的区间个数 思路:求出区间的最大最小值,只要他们的差值小于k,那么这个区间就符合要求,但是由于n较大,用暴力一定超时,所以就要用别的方法了:而RMQ是可以求区间的最值的,而且预处理的复杂度只有O(nlogn),而查询只是O(1)处理,这样相对来说节约了时间,再根据右端点来二分枚举左端点(其实不用二分好像更快,估

HDU 5089 Assignment(rmq+二分 或 单调队列)

Assignment Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 557    Accepted Submission(s): 280 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbered fr

Codeforces Round #262 (Div. 2) 460C. Present(二分)

题目链接:http://codeforces.com/problemset/problem/460/C C. Present time limit per test 2 seconds memory limit per test 256 megabytes input standard input output standard output Little beaver is a beginner programmer, so informatics is his favorite subjec

BZOJ 题目3172: [Tjoi2013]单词(AC自动机||AC自动机+fail树||后缀数组暴力||后缀数组+RMQ+二分等五种姿势水过)

3172: [Tjoi2013]单词 Time Limit: 10 Sec  Memory Limit: 512 MB Submit: 1890  Solved: 877 [Submit][Status][Discuss] Description 某人读论文,一篇论文是由许多单词组成.但他发现一个单词会在论文中出现很多次,现在想知道每个单词分别在论文中出现多少次. Input 第一个一个整数N,表示有多少个单词,接下来N行每行一个单词.每个单词由小写字母组成,N<=200,单词长度不超过10^6

玲珑杯 Round 19 B Buildings (RMQ + 二分)

DESCRIPTION There are nn buildings lined up, and the height of the ii-th house is hihi. An inteval [l,r][l,r](l≤r)(l≤r) is harmonious if and only if max(hl,-,hr)?min(hl,-,hr)≤kmax(hl,-,hr)?min(hl,-,hr)≤k. Now you need to calculate the number of harmo