【Codeforces 91B】Queue

來源:互聯網
上載者:User

標籤:tchar   --   ==   ble   har   說明   ref   get   break   

【連結】 我是連結,點我呀:)
【題意】

【題解】


對於每個i,用二分的方法求出來y所在的位置j.
可以這樣求。
假設現在二分到了位置mid.
那麼隨便用個rmq求出來mid..n這一段的最小值temp。
看看temp是否滿足<a[i]
如果滿足的話,那麼很好,我們讓l=mid+1,然後再嘗試往右一點就好了。
否則的話,說明太靠右了,那麼就往左一點r = mid - 1
初值l = i,r = n.

【代碼】

#include <bits/stdc++.h>#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define LL long longusing namespace std;const int N = 1e5;int n;int a[N+10];int mi[N+10][17+5];int get_mi(int l,int r){    int len = log2(r-l+1);    return min(mi[l][len],mi[r-(1<<len)+1][len]);}int main(){    //freopen("D:\\rush.txt","r",stdin);    scanf("%d",&n);    rep1(i,1,n) scanf("%d",&a[i]);    rep1(i,1,n) mi[i][0] = a[i];    rep1(l,1,17)        rep1(i,1,n){            int j = i+(1<<l)-1;            if (j>n) break;            mi[i][l] = min(mi[i][l-1],mi[i+(1<<(l-1))][l-1]);        }    rep1(i,1,n){        int l = i,r = n,temp = -1;        while (l<=r){            int mid = (l+r)>>1;            if (get_mi(mid,n)<a[i]){                temp = mid;                l = mid + 1;            }else                r = mid - 1;        }        if (temp==-1){            printf("-1");        }else{            printf("%d",temp-i-1);        }        if(i==n)puts("");else putchar(' ');    }    return 0;}

【Codeforces 91B】Queue

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.