Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

请问用RMQ的话如何能不超内存?我求了两次RMQ,代码在下面。

Posted by zehzxm at 2007-03-07 00:11:46 on Problem 2823
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator