Chinaunix首页 | 论坛 | 博客
  • 博客访问: 342940
  • 博文数量: 122
  • 博客积分: 5000
  • 博客等级: 大校
  • 技术积分: 1191
  • 用 户 组: 普通用户
  • 注册时间: 2009-10-24 11:12
文章分类

全部博文(122)

文章存档

2010年(122)

我的朋友

分类: C/C++

2010-04-24 00:30:02

一、问题描述

Description

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.

Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

Input

Line 1: Two space-separated integers, N and Q.
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ ABN), representing the range of cows from A to B inclusive.

Output

Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

Sample Input

6 3

1

7

3

4

2

5

1 5

4 6

2 2

Sample Output

6

3

0

Source

 

二、解题思路

这是个RMQ问题,可以使用ST算法或者是线段树求解。提交结果显示,线段树方法时间上要好一些。

三、ST算法实现

 

#include <iostream>
#include <math.h>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))
using namespace std;
const int maxn=50001;
int h[maxn];
int mx[maxn][16],mn[maxn][16];
int n,q;

void rmq_init()
{
    int i,j;
    for(j=1;j<=n;j++) mx[j][0]=mn[j][0]=h[j];
    int m=floor(log((double)n)/log(2.0));
    for(i=1;i<=m;i++)
    {
        for(j=n;j>0;j--)
        {
            mx[j][i]=mx[j][i-1];
            if(j+(1<<(i-1))<=n) mx[j][i]=max(mx[j][i],mx[j+(1<<(i-1))][i-1]);
        }
    }
    for(i=1;i<=m;i++)
    {
        for(j=n;j>0;j--)
        {
            mn[j][i]=mn[j][i-1];
            if(j+(1<<(i-1))<=n) mn[j][i]=min(mn[j][i],mn[j+(1<<(i-1))][i-1]);
        }
    }
}
int rmq(int l,int r)
{
    int m=floor(log((double)(r-l+1))/log(2.0));
    int a=max(mx[l][m],mx[r-(1<<m)+1][m]);
    int b=min(mn[l][m],mn[r-(1<<m)+1][m]);
    return a-b;
}
int main()
{
    int i,l,r;
    scanf("%d%d",&n,&q);
    for(i=1;i<=n;i++) scanf("%d",&h[i]);
    rmq_init();
    for(i=0;i<q;i++){
        scanf("%d%d",&l,&r);
        printf("%d\n",rmq(l,r));
    }
    return 0;
}


四、线段树实现

 

#include<iostream>
using namespace std;
struct Node
{
    int l;
    int r;
    int mx;
    int mn;
    Node(){mx=0;mn=0x7FFFFFFF;}
};
Node T[200005];
int mxval;
int mnval;
int N,Q;
void build(int a,int b,int n)
{
    T[n].l=a;
    T[n].r=b;
    if(a<b)
    {
        int t=(a+b)/2;
        build(a,t,2*n);
        build(t+1,b,2*n+1);
    }
}
void insert(int x,int n,int val)
{
    if(T[n].l == x && T[n].r== x)
    {
        T[n].mx=val;
        T[n].mn=val;
        return ;
    }
    else
    {
        int t=(T[n].l+T[n].r)/2;
        //修改范围内的最大最小值

        if(T[n].mx<val)
            T[n].mx=val;
        if(T[n].mn>val)
                T[n].mn=val;
        if(x<=t)
            insert(x,2*n,val);
        else
            insert(x,2*n+1,val);
    }
}
void query(int a,int b,int n)
{
    if(T[n].l == a && T[n].r == b)
    {
        if(mxval<T[n].mx)
            mxval=T[n].mx;
        if(mnval>T[n].mn)
            mnval=T[n].mn;
    }
    else
    {
        int t=(T[n].l+T[n].r)/2;
        if(b<=t)
            query(a,b,2*n);
        else
        {
            if(a>=t+1)
                query(a,b,2*n+1);
            else
            {
                query(a,t,2*n);
                query(t+1,b,2*n+1);
            }
        }
    }
}
int main()
{
    int a,b;
    //=

    int i;
    int h;
    scanf("%d%d",&N,&Q);
    build(1,N,1);
    for(i=1;i<=N;++i)
    {
        scanf("%d",&h);
        insert(i,1,h);
    }
    for(i=1;i<=Q;++i)
    {
        scanf("%d%d",&a,&b);
        mxval=0;
        mnval=0x7fffffff;
        query(a,b,1);
        printf("%d\n",mxval-mnval);

    }
    return 0;
}


阅读(862) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~