Codeforces 549F Yura and Developers,codeforces549f

來源:互聯網
上載者:User

Codeforces 549F Yura and Developers,codeforces549f

probelm

題意

給定一個序列和一個mod值,定義[l,r]合法當l到r的所有元素和減去其中的最大值的結果能夠整除mod。問共有多少區間合法。

思路

一開始想的分治。對於一個[l,r]我們可以把這之中最大的求出來,然後以這個數作為分界,把這個區間分成兩部分,對於分布在兩個區間中的答案,我們可以通過lowerbound和upperbunder在O(log(n))的時間下求出,然後遞迴求解。然而對於這題,這種做法的下界會達到O(n2)。所以這樣做不行。。

  • 看了題解,題解說可以直接枚舉那個最大值,然後把滿足的區間找出來然後求出來。豁然開朗。這樣我們只需要把原數組進行排序,並記錄每個數的左右界。從最小的開始,在計算答案的同時去更新這個左右界。這樣可以在O(nlog(n))的複雜度下求出答案。

  • 一道不錯的題。希望以後能夠堅持把每次做的比賽的題目補完。

AC代碼
/*************************************************************************    > File Name: pf.cpp    > Author: znl1087    > Mail: loveCJNforever@gmail.com     > Created Time: 四  6/11 16:36:14 2015 ************************************************************************/#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <string>#include <cstdlib>#include <vector>#include <set>#include <map>#define LL long longusing namespace std;int n,k;LL s[300005];vector<int> f[1000005];LL num[300005];int pre[300005],nxt[300005];LL ask(int l,int r,LL in){    return upper_bound(f[in].begin(),f[in].end(),r)-lower_bound(f[in].begin(),f[in].end(),l);}LL cal(int m){    int l = pre[m]+1,r = nxt[m]-1;    LL maxn = num[m];    LL ans = 0;    if( r - m < m - l){        for(int i=m+1;i<=r;i++)            ans+=(ask(l-1,m-1,(s[i]-maxn%k+k)%k));        ans+=(ask(l-1,m-2,(s[m]-maxn%k+k)%k));    }else{        for(int i=l;i<m;i++)            ans+=(ask(m,r,(s[i-1]+maxn)%k));        ans+=(ask(m+1,r,(s[m-1]+maxn)%k));    }    pre[nxt[m]] = pre[m];    nxt[pre[m]] = nxt[m];    return ans;}int ord[300005];int cmp(int a,int b){    return num[a]<num[b];}int main(){    cin>>n>>k;    s[0] = 0;    for(int i=1;i<=n;i++){        cin>>num[i],s[i] = (s[i-1]+num[i])%k,ord[i] = i;        pre[i] = i-1;        nxt[i] = i+1;    }    nxt[0] = 1;    nxt[n] = n+1;    pre[n+1] = n;    for(int i=0;i<=n;i++)f[s[i]].push_back(i);    sort(ord+1,ord+n+1,cmp);    LL ans = 0;    for(int i=1;i<=n;i++){        int pos = ord[i];        ans+=cal(pos);    }    cout<<ans<<endl;    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.