An army of
n droids is lined up in one row. Each droid is described bym integersa1,?a2,?...,?am , whereai 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 hasm weapons, thei -th weapon can affect all the droids in the army by destroying one detail of thei -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 mostk shots. How many shots from the weapon of what type shouldR2?D2 make to destroy the sequence of consecutive droids of maximum length?
TimeLimit(ms):2000
MemoryLimit(MB):256
n∈[1,105]
m∈[1,5]
k∈[0,109]
ai∈[0,108]
用two-pointers的方法,设指针
l=1 ,r=1 。求t=∑mj=1max(alj,…,arj) ,其中aij 表示第i 个人的第j 个属性。若t>=k 则r ++,否则l ++(保证l<=r ),在此过程不断维护r?l+1 的最值…
TimeComplexity:O(N×M×log2N)
MemoryComplexity:O(N×M)
//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");
}
原文地址:http://blog.csdn.net/uestc_peterpan/article/details/43834531