Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
请问用RMQ的话如何能不超内存?我求了两次RMQ,代码在下面。#include<iostream> #include<cmath> using namespace std; int a[1000001]; int d[20][1000001]; void make_min_RMQ(int n) { int i,j; for (i=1;i<=n;i++) d[0][i]=a[i]; for (j=1;j<=int(log((double)n)/log(2.0));j++) for(i=1;i+(1<<j)-1<=n;i++) d[j][i]=min(d[j-1][i],d[j-1][i+(1<<(j-1))]); return; } void make_max_RMQ(int n) { int i,j; for (i=1;i<=n;i++) d[0][i]=a[i]; for (j=1;j<=int(log((double)n)/log(2.0));j++) for(i=1;i+(1<<j)-1<=n;i++) d[j][i]=max(d[j-1][i],d[j-1][i+(1<<(j-1))]); return; } int min_RMQ(int i,int j) { int k=int(log((double)(j-i+1))/log(2.0)); return min(d[k][i],d[k][j-(1<<k)+1]); } int max_RMQ(int i,int j) { int k=int(log((double)(j-i+1))/log(2.0)); return max(d[k][i],d[k][j-(1<<k)+1]); } int main() { int n,i,k; while(scanf("%d%d",&n,&k)!=EOF) { for(i=1;i<=n;i++) scanf("%d",&a[i]); make_min_RMQ(n); for(i=1;i<=n-k+1;i++) printf("%d ",min_RMQ(i,i+k-1)); printf("\n"); make_max_RMQ(n); for(i=1;i<=n-k+1;i++) printf("%d ",max_RMQ(i,i+k-1)); printf("\n"); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator