codeforces Round 314 div.2

來源:互聯網
上載者:User

這次比賽是用小號打的,於是現在小號比大號rating高。。。QwQ

A題和B題很簡單不多說。

C題:統計子序列中長度為3,公比為 k k 的等比序列的個數。

從後往前處理,分別統計滿足條件長度為1,2,3的序列的個數,
f(i,j)=∑f(i−1,t)|t=j∗k f(i,j) = \sum f(i-1,t)| t = j*k,實現時離散化一下就好~

然而,當時只判斷了 a[i]*k<=a[max] ,而沒有判斷 a[i]*k>=a[min] (k可能為負數), 所以FST了。。。

const int maxn = 2e5 + 5, size = maxn*3;int n, k, a[maxn];long long ans;int cnt[maxn], p[maxn], pl;long long f[maxn], g[maxn];int main(){#ifndef ONLINE_JUDGE    freopen("C.in","r",stdin);    freopen("C.out","w",stdout);#endif    read(n), read(k);    for(int i = 1; i <= n; i++)        read(a[i]), p[i] = a[i];    std::sort(p + 1, p + n + 1);    pl = std::unique(p + 1, p + n + 1) - (p + 1);    for(int i = n; i > 0; i--)    {        int t = std::lower_bound(p + 1, p + pl + 1, a[i]) - p;        if((long long)a[i]*k <= p[pl] && (long long)a[i]*k >= p[1])        {            int v = std::lower_bound(p + 1, p + pl + 1, a[i]*k) - p;            if(a[i]*k == p[v]) g[t] += f[v], f[t] += cnt[v];        }        cnt[t]++;    }    for(int i = 1; i <= pl; i++) ans += g[i];    write(ans);#ifndef ONLINE_JUDGE    fclose(stdin);    fclose(stdout);#endif    return 0;}

D題:從結束狀態逆向處理,用並查集維護最長區間,直到滿足可放船數>=實際船數才得到答案。

const int maxn = 2e5 + 5, maxk = maxn;int n, k, a, m;int x[maxk], cnt;bool e[maxn];int fa[maxn], size[maxn];int find(int x){    return (x == fa[x])?x:(fa[x] = find(fa[x]));}void gather(int x,int y){    if(x > y) std::swap(x, y);    x = find(x), y = find(y), fa[y] = x;    if(x != y) size[x] += size[y], size[y] = 0;}void init(){    read(n), read(k), read(a), read(m);    for(int i = 1; i <= m; i++)        read(x[i]), e[x[i]] = true;}void prework(){    for(int i = 1; i <= n; i++) fa[i] = i, size[i] = 1;    for(int i = 1; i <= n; i++)        if(i > 1 && !e[i] && !e[i-1])            gather(i, i - 1);    for(int i = 1; i <= n; i++)        if(fa[i] == i && !e[i]) cnt += (size[i]+1)/(a+1);       }int solve(){    if(cnt >= k) return -1;    for(int i = m; i >= 1; i--)    {        if(x[i] > 1 && !e[x[i]-1])        {            cnt -= (size[find(x[i]-1)]+1)/(a+1);            gather(x[i] - 1, x[i]);        }        if(x[i] < n && !e[x[i]+1])        {            cnt -= (size[find(x[i]+1)]+1)/(a+1);            gather(x[i], x[i] + 1);        }        cnt += (size[find(x[i])]+1)/(a+1);        e[x[i]] = false;        if(cnt >= k) return i;    }    return 0;}int main(){#ifndef ONLINE_JUDGE    freopen("D.in","r",stdin);    freopen("D.out","w",stdout);#endif    init(), prework();    write(solve());#ifndef ONLINE_JUDGE    fclose(stdin);    fclose(stdout);#endif    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.