【RMQ問題】uva11235Frequent values,rmq問題

來源:互聯網
上載者:User

【RMQ問題】uva11235Frequent values,rmq問題

/*RMQ問題-----------------------------------------------------------------------------------------------用數組:cnt[i]:第i段中數的個數(每一段是指所有數都同的一段數)(在這裡該數組相當於RMQ問題中的A[]數組)num[i]:位置i所在段的編號left1[i]:位置i左端點的位置right1[i]:位置i右端點的位置----------------------------------------------------------------------------------------------right1[L]-L+1:從L到該段末尾元素的個數R-left[R]+1:從該段開始元素到R處的元素個數RMQ(num[L]+1, num[R]-1):從num[L]+1段到num[R]-1段中的最大cnt;最後的結果為以上三個數的最大值:----------------------------------------------------------------------------------------------若:num[L] == num[R](即:L和R在同一段中),結果為R-L+1;----------------------------------------------------------------------------------------------RMQ:void RMQ_init()預先處理{    int n = size;    for(int i=0; i<n; i++)        d[i][0] = cnt[i];    for(int j=1; (1<<j) <= n; j++)    {        for(int i=0; i + (1<<j) - 1 < n; i++)        {            d[i][j] = max(d[i][j-1], d[i + (1<<(j-1))][j-1]);        }    }}int RMQ(int L, int R)查詢{    if(L > R)        return 0;    int k = 0;    while((1<<(k+1)) <= R-L+1)        k++;    return max(d[L][k], d[R-(1<<k)+1][k]);}--------------------------------------------------------------------------------------cnt數組,left1數組,right1數組的構建:int x,p = 100000000;----------------------------P要大於x的取值範圍int ct = -1;------------------------------------輸入資料的段數int _left;--------------------------------------當前段的左端點for(int i=0; i<n; i++){    scanf("%d",&x);    if(x == p)    {        cnt[ct]++;------------------------------如果輸入的數和前一個數同,該段個數+1        num[i] = ct;----------------------------不斷更新位置i所在的段        left1[i] = _left;-----------------------更左端點    }    else    {        if(ct >= 0)        {            for(int j=_left; j<=i-1; j++)-------更新右端點                right1[j] = i-1;        }        ct++;        _left = i;        num[i] = ct;        left1[i] = _left;        cnt[ct] = 1;        p = x;    }}for(int i=_left; i<n; i++)    right1[i] = n-1;size = ct+1;------------------------------------------------------------------------------*/#include <iostream>#include <cstdio>#include <climits>#include <cstring>using namespace std;const int N = 100010;int n,q;int cnt[N],num[N],left1[N],right1[N];int a[N];int size;int d[N][30];void RMQ_init(){    int n = size;    for(int i=0; i<n; i++)        d[i][0] = cnt[i];    for(int j=1; (1<<j) <= n; j++)    {        for(int i=0; i + (1<<j) - 1 < n; i++)        {            d[i][j] = max(d[i][j-1], d[i + (1<<(j-1))][j-1]);        }    }}int RMQ(int L, int R){    if(L > R)        return 0;    int k = 0;    while((1<<(k+1)) <= R-L+1)        k++;    return max(d[L][k], d[R-(1<<k)+1][k]);}int main(){    //freopen("input.txt","r",stdin);    while(scanf("%d%d",&n,&q) != EOF && n)    {        memset(cnt, 0, sizeof(cnt));        memset(left1, 0, sizeof(left1));        memset(right1, 0, sizeof(right1));        memset(d, 0, sizeof(d));        int x,p = 100000000;        int ct = -1;        int _left;        for(int i=0; i<n; i++)        {            scanf("%d",&x);            if(x == p)            {                cnt[ct]++;                num[i] = ct;                left1[i] = _left;            }            else            {                if(ct >= 0)                {                    for(int j=_left; j<=i-1; j++)                        right1[j] = i-1;                }                ct++;                _left = i;                num[i] = ct;                left1[i] = _left;                cnt[ct] = 1;                p = x;            }        }        for(int i=_left; i<n; i++)            right1[i] = n-1;        size = ct+1;        RMQ_init();        while(q--)        {            int L,R;            scanf("%d%d",&L,&R);            L--;            R--;            int t1 = right1[L]-L+1;            int t2 = RMQ(num[L]+1, num[R]-1);            int t3 = R-left1[R]+1;            if(num[L] == num[R])                printf("%d\n",R-L+1);            else                printf("%d\n",max(t1, max(t2, t3)));        }    }    return 0;}




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.