码迷,mamicode.com
首页 > 其他好文 > 详细

Codeforces Round #291 Div2 D

时间:2015-02-15 13:33:42      阅读:155      评论:0      收藏:0      [点我收藏+]

标签:双指针法   rmq   数据结构   

Problem

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?

Limits

TimeLimit(ms):2000

MemoryLimit(MB):256

n[1,105]

m[1,5]

k[0,109]

ai[0,108]

Look up Original Problem From here

Solution

用two-pointers的方法,设指针l=1r=1。求t=mj=1max(alj,,arj),其中aij表示第i个人的第j个属性。若t>=kr++,否则l++(保证l<=r),在此过程不断维护r?l+1的最值

Complexity

TimeComplexity:O(N×M×log2N)

MemoryComplexity:O(N×M)

My Code

//Hello. I‘m Peter.
#include<cstdio>
#include<iostream>
#include<sstream>
#include<cstring>
#include<string>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<functional>
#include<cctype>
#include<ctime>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
#define peter cout<<"i am peter"<<endl
#define input freopen("data.txt","r",stdin)
#define randin srand((unsigned int)time(NULL))
#define INT (0x3f3f3f3f)*2
#define LL (0x3f3f3f3f3f3f3f3f)*2
#define gsize(a) (int)a.size()
#define len(a) (int)strlen(a)
#define slen(s) (int)s.length()
#define pb(a) push_back(a)
#define clr(a) memset(a,0,sizeof(a))
#define clr_minus1(a) memset(a,-1,sizeof(a))
#define clr_INT(a) memset(a,INT,sizeof(a))
#define clr_true(a) memset(a,true,sizeof(a))
#define clr_false(a) memset(a,false,sizeof(a))
#define clr_queue(q) while(!q.empty()) q.pop()
#define clr_stack(s) while(!s.empty()) s.pop()
#define rep(i, a, b) for (int i = a; i < b; i++)
#define dep(i, a, b) for (int i = a; i > b; i--)
#define repin(i, a, b) for (int i = a; i <= b; i++)
#define depin(i, a, b) for (int i = a; i >= b; i--)
#define pi acos(-1.0)
#define eps 1e-9
#define MOD 1000000007
#define MAXN 100100
#define N
#define M 6
int n,m,k;
int matrix[MAXN][M];
struct Segment_Tree{
    int left,right;
    ll max;
}tree[MAXN<<2][M];
void plant_tree(int id,int l,int r,int c){
    tree[id][c].left=l,tree[id][c].right=r;
    if(l==r){
        tree[id][c].max=matrix[l][c];
        return;
    }
    int mid=(l+r)>>1;
    plant_tree(id<<1,l,mid,c);
    plant_tree(id<<1|1,mid+1,r,c);
    tree[id][c].max=max(tree[id<<1][c].max,tree[id<<1|1][c].max);
}
ll query_max(int id,int l,int r,int c){
    if(tree[id][c].left==l && tree[id][c].right==r){
        return tree[id][c].max;
    }
    int mid=(tree[id][c].left+tree[id][c].right)>>1;
    if(r<=mid) return query_max(id<<1,l,r,c);
    else if(mid<l) return query_max(id<<1|1,l,r,c);
    else return max(query_max(id<<1,l,mid,c),query_max(id<<1|1,mid+1,r,c));
}
ll query(int l,int r){
    ll res=0;
    repin(j,1,m){
        res+=query_max(1,l,r,j);
    }
    return res;
}
int ansl,ansr,ansnum;
int main(){
    scanf("%d %d %d",&n,&m,&k);
    repin(i,1,n){
        repin(j,1,m){
            scanf("%d",&matrix[i][j]);
        }
    }
    repin(j,1,m){
        plant_tree(1,1,n,j);
    }
    //two-pointers;
    int l=1,r=1;
    ansnum=-1;
    while(r<=n){
        ll t=query(l,r);
        if(t>k){
            l++;
            while(l>r) r++;
            continue;
        }
        if(r-l+1>ansnum){
            ansl=l,ansr=r;
            ansnum=r-l+1;
        }
        r++;
    }
    if(ansnum==-1){
        repin(j,1,m){
            if(j!=1) printf(" ");
            printf("%d",0);
        }
        printf("\n");
        exit(0);
    }
    repin(j,1,m){
        ll t=query_max(1,ansl,ansr,j);
        if(j!=1) printf(" ");
        printf("%I64d",t);
    }
    printf("\n");
}

Codeforces Round #291 Div2 D

标签:双指针法   rmq   数据结构   

原文地址:http://blog.csdn.net/uestc_peterpan/article/details/43834531

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!