HDU 4908 BestCoder Sequence(组合数学)

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]++;
	    else
		rb[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;
}

HDU 4908 BestCoder Sequence(组合数学),布布扣,bubuko.com

时间: 2024-10-10 15:33:54

HDU 4908 BestCoder Sequence(组合数学)的相关文章

hdu 4908 BestCoder Sequence(计数)

题目链接:hdu 4908 BestCoder Sequence 题目大意:给定N和M,N为序列的长度,由1~N组成,求有多少连续的子序列以M为中位数,长度为奇数. 解题思路:v[i]记录的是从1~i这些位置上有多少个数大于M,i-v[i]就是小于M的个数.pos为M在序列中的位置.如果有等式i?j=2?(v[i]?v[j?1]),i≥pos≥j 那么i和j既是一组满足的情况.将等式变形i?2?v[i]=j?2?v[j?1]. #include <cstdio> #include <cs

hdu 4908 BestCoder Sequence 找M为中位数的串的数目, 需要预处理

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 271    Accepted Submission(s): 112 Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing

hdu 4908 BestCoder Sequence【DP】

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目大意:给出一个排列,一个m,求出这个排列的连续子序列中有多少个序列式以m为中位数. 由于是一个排列,不会出现重复的数字,记录一下m的位置index,然后以index为分界线,往左求出s[i](表示从i到index之间有多少大于m),b[i](表示从i到index之间有多少小于m),往右求出s[i](表示从index到i之间有多少大于m),b[i](表示从index到i之间有多少小于m).

HDU 4908——BestCoder Sequence

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 169    Accepted Submission(s): 82 Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing

hdu 4908 BestCoder Sequence 发现M中值是字符串数, 需要预处理

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 271    Accepted Submission(s): 112 Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing

[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence. A

HDU 4908 BestCoder Sequence(BestCoder Round #3)

Problem Description: Mr Potato is a coder.Mr Potato is the BestCoder. One night, an amazing sequence appeared in his dream. Length of this sequence is odd, the median number is M, and he named this sequence as Bestcoder Sequence. As the best coder, M

HDU 4908 BestCoder Sequence

http://acm.hdu.edu.cn/showproblem.php?pid=4908 题意: 求一个[1,N]的全排列中以M为中位数的子序列的个数 思路: 隔了好久填个坑...当初理解错题意了 一开始直接写了个二重循环来扫直接TLE了... 搜了下才想到该怎么用一重解决... 代码:  93MS  1524K #include <cstdio> #include <cstring> using namespace std; #define N 40001 int a[N],

hdu(2062)-Subset sequence 组合数学

题意:求集合{1,2,3...n}的第m个排列子集合.集合的大小按字典树排. 例两个元素的排列子集合按字典树排列是:{1},{1,2},{2},{2,1}: 解法:一个一个元素来确定,每次把剩余的元素按大小顺序排列在num中,然后根据排列组合原理直接计算下一个位置的元素的大小,直到排列数为0停止: 代码: /****************************************************** * author:xiefubao **********************