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 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, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which are bestcoder sequences in a given permutation of 1 ~ N.

Input

Input contains multiple test cases.

For each test case, there is a pair of integers N and M in the first line, and an permutation of 1 ~ N in the second line.

[Technical Specification]

1. 1 <= N <= 40000

2. 1 <= M <= N

Output

For each case, you should output the number of consecutive sub-sequences which are the Bestcoder Sequences.

Sample Input

1 1
1
5 3
4 5 3 2 1

Sample Output

1
3

Hint

For the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.

题目意思是  再给出的全排列(1~n每一个数字仅仅出现一次)中,找到一个奇数个的子串,要求子串的中位数(大小排序后正中间的数), 为M;

求这样的子串有多少.

首先要把M这个数左边的串预处理下,  假设 遇到大于M的数ji++,然后记录在data[当前位置的奇偶][当前记录的ji]  ,假设当前位置的数小于M的数,ji--; 由于ji记录在数组里,所以ji 要价格50000 以保证不会出现负数的情况.

然后再处理右边的串,ji 又一次计数,

ans+data[位置奇偶, 假设两个位置奇偶同样,代表这条串有奇数个元素][-ji  加个符号,找到前面处理过的左串中,能够互补的串,达到大于M的数和小于M的数 一样多];

相同加个ji取负数后  相同加个 50000,和前面保存一致

#include<stdio.h>
#include<string.h>
int big(int a)
{
	return a+50000;
}
int a[50000],ji,data[2][200000];
int main()
{
	int n,m,i,j,wei;
	int ans;
	while(scanf("%d%d",&n,&m)!=EOF)
	{
		ans=0;
		for(i=1;i<=n;i++)
		{
			scanf("%d",&a[i]);
			if(a[i]==m)
				wei=i;
		}
		ji=0;
		memset(data,0,sizeof(data));
		for(i=wei;i>=1;i--)
		{
			if(a[i]<m)
				ji--;
			if(a[i]>m)
				ji++;
			if(i&1)
				data[1][big(ji)]++;
			else
				data[0][big(ji)]++;
		}
		ji=0;
		for(i=wei;i<=n;i++)
		{
			if(a[i]<m)
				ji--;
			if(a[i]>m)
				ji++;
			if(i&1)
				ans+=data[1][big(-ji)];
			else
				ans+=data[0][big(-ji)];
		}
	    printf("%d\n",ans);
	}
	return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

时间: 2024-10-10 22:30:04

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

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(组合数学)

HDU 4908 BestCoder Sequence 题目链接 题意:给定一个序列,1-n的数字,选定一个作为中位数m,要求有多少连续子序列满足中位数是m 思路:组合数学,记录下m左边和右边一共有多少种情况大于m的数字和小于n数组的差,然后等于左边乘右边所有的和,然后最后记得加上左右两边差为0的情况. 当时也是比较逗,还用树状数组去搞了,其实完全没必要 代码: #include <cstdio> #include <cstring> #define lowbit(x) (x&am

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(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

[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

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],

PB excel 设定某些单元格框格为实线,但发现range()中值获取不到

我用datastore将数据写入了excel, 公式如下: ole_xls.ActiveSheet.Cells[1,2].value='xxxxx' 但发现设实线的函数为: ole_xls.ActiveSheet.range(beginRowcol+":"+EndRowCol) 这个range中的beginRowcol格式应为:A2 , 我只知道Cells[1,2] 有没有方法得到Cells[1,2]这个位置在Excel的参照地址? 答:可以考虑如下 Cells[1,2] 应该是B1吧