HDU 4908 BestCoder Sequence(組合數學),hdu4908

來源:互聯網
上載者:User

HDU 4908 BestCoder Sequence(組合數學),hdu4908
HDU 4908 BestCoder Sequence

題目連結

題意:給定一個序列,1-n的數字,選定一個作為中位元m,要求有多少連續子序列滿足中位元是m

思路:組合數學,記錄下m左邊和右邊一共有多少種情況大於m的數字和小於n數組的差,然後等於左邊乘右邊所有的和,然後最後記得加上左右兩邊差為0的情況。

當時也是比較逗,還用樹狀數組去搞了,其實完全沒必要

代碼:

#include <cstdio>#include <cstring>#define lowbit(x) (x&(-x))const int N = 40005;int n, m, num[N], bit[N];void add(int x, int v) {    while (x < N) {bit[x] += v;x += lowbit(x);    }}int query(int x) {    int ans = 0;    while (x) {ans += bit[x];x -= lowbit(x);    }    return ans;}int lb[N], ls[N], rb[N], rs[N];int main() {    while (~scanf("%d%d", &n, &m)) {memset(bit, 0, sizeof(bit));int v;for (int i = 1; i <= n; i++) {    scanf("%d", &num[i]);    if (num[i] == m)v = i;}memset(lb, 0, sizeof(lb));memset(ls, 0, sizeof(ls));memset(rb, 0, sizeof(rb));memset(rs, 0, sizeof(rs));for (int i = v - 1; i >= 1; i--) {    add(num[i], 1);    int small = query(m);    int big = v - i - small;    if (big >= small)lb[big - small]++;    else ls[small - big]++;}memset(bit, 0, sizeof(bit));for (int i = v + 1; i <= n; i++) {    add(num[i], 1);    int small = query(m);    int big = i - v - small;    if (small >= big)rs[small - big]++;    elserb[big - small]++;}long long ans = 1;ans += lb[0] + rs[0];for (int i = 0; i <= 40000; i++) {    ans += (long long)lb[i] * rs[i];    ans += (long long)ls[i] * rb[i];}printf("%lld\n", ans);    }    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.