题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908

BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 618    Accepted Submission(s): 214

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 asBestcoder Sequence.



As the best coder, Mr potato has strong curiosity, he wonder the number of consecutive sub-sequences which arebestcoder 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 theBestcoder 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.
 
Source
 
Recommend
We have carefully selected several similar problems for you:  

pid=4910">4910 4909 4906 

pid=4904">4904 4903 


题意:给定N和M,N为序列的长度。由1~N组成。求有多少连续的子序列是以M为中位数,且长度为奇数。

代码例如以下:
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define mid 40000
#define MAXN 100017
int dp[MAXN], num[MAXN];
void init()
{
memset(dp,0,sizeof(dp));
memset(num,0,sizeof(num));
}
int main()
{
int n, m;
int i, j;
while(~scanf("%d%d",&n,&m))
{
init();
int t = 0;
for(i = 1; i <= n; i++)
{
scanf("%d",&num[i]);
if(num[i] == m)//记录m位置
t = i;
}
int cont = 0;
for(i = t+1; i <= n; i++)
{
if(num[i] > m)//大的加
cont++;
else //小的减
cont--;
dp[cont+mid]++;//记录出现该状态的次数
}
cont = ++dp[mid];//当状态数为mid。才满足中位数
int tt = 0;
for(i = t-1; i >= 1; i--)
{
if(num[i] > m)
tt++;
else
tt--;
cont+=dp[-tt+mid];//状态相加为mid的个数
}
printf("%d\n",cont);
}
return 0;
}

再贴一张和上面思路同样但做法不同的代码(系转载

将大于M的数标记为1,小于M的数标记为-1。M本身标记

为0,则题目就是要求和为0而且包含M的连续序列的个数。

用sum_i表示从第一个数到第i个数的标记的和。对于全部大

于等于M的位置的i,我们要求小于M的位置的sum_j

== sum_i的个数的和即为答案。

代码例如以下:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define MAXN 50000
using namespace std;
int num[MAXN+10],sum[MAXN+10],a[MAXN+10+MAXN];
int main()
{
int M,N,M_id;
while (scanf("%d %d",&N,&M)!=EOF)
{
memset(a,0,sizeof(a));
memset(sum,0,sizeof(sum));
memset(num,0,sizeof(num));
num[0]=sum[0]=0;
for (int i=1;i<=N;i++)
{
int tmp;
scanf("%d",&tmp);
if (tmp>M) num[i]=1;
else if (tmp==M) num[i]=0,M_id=i;
else num[i]=-1;
sum[i]=sum[i-1]+num[i];
}
int cnt=0;
for (int j=0;j<=M_id-1;j++)
a[sum[j]+MAXN]++;
for (int i=M_id;i<=N;i++)
cnt+=a[sum[i]+MAXN];
printf("%d\n",cnt);
}
return 0;
}

最新文章

  1. SQL Server2014 SP2新增的数据库克隆功能
  2. 额。。万恶之源就是c
  3. Codeforces 696 C. PLEASE
  4. 当你在浏览器地址栏输入一个URL后回车,将会发生的事情?
  5. win2008使用FireDac连接ORACLE数据库问题
  6. linux recv 返回值与linux socket 错误分析
  7. TList,TObjectList 使用——资源释放
  8. LAMP环境 源码包安装
  9. web中自定义鼠标样式
  10. SQL server聚合函数、数学函数、字符串函数
  11. 访问mysql出现“Access denied for user root@localhost”(using password:NO)解决方案
  12. Chapter 17_1 弱引用table
  13. nodejs构建多房间简易聊天室
  14. 【转载】Android 开发 命名规范
  15. git branch --set-upstream 本地关联远程分支
  16. 我的 Putty 配色方案
  17. JS基础(三)构造函数
  18. Arrays.copyOf() 和 System.arrayCopy()分析
  19. UITableView自动计算cell高度并缓存
  20. CSS3:文字属性

热门文章

  1. Canal使用报错解决办法
  2. LeetCode(78) Subsets
  3. 【02】koala编译中文出错(已放弃不用)
  4. jquery获取ul中li的值
  5. 『NYIST』第八届河南省ACM竞赛训练赛[正式赛一]CF-236B. Easy Number Challenge
  6. FZU-2148-Moon Game,,几何计算~~
  7. hdu 1043 A*
  8. bzoj1086 [SCOI2005]王室联邦 树分块
  9. 积累js中的一些问题及解决方案
  10. Linux MTD (Memory Technology Device) subsystem analysis -For Atheros char device