博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)
阅读量:4930 次
发布时间:2019-06-11

本文共 2985 字,大约阅读时间需要 9 分钟。

题目链接:

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 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.
 
Source
 
Recommend
We have carefully selected several similar problems for you:            
题意
给定N和M,N为序列的长度。由1~N组成。求有多少连续的子序列是以M为中位数,且长度为奇数。
代码例如以下:
#include 
#include
#include
#include
using namespace std;#define mid 40000#define MAXN 100017int 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
#include
#include
#include
#define MAXN 50000using 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;}

转载于:https://www.cnblogs.com/jzssuanfa/p/6817394.html

你可能感兴趣的文章
手机服务 & BroadcastReceiver & 获取电池相关内容
查看>>
Windbg脚本和扩展工具之一STL容器扩展命令
查看>>
Codeforces Round #179 (Div. 2) A. Yaroslav and Permutations(简单)
查看>>
如何提高报表sql效率
查看>>
Linux回收站[改写rm防止误删文件无法恢复] - wklken的笔记 - 博客频道 - CSDN.NET
查看>>
图像的剪切
查看>>
如何在vSphere环境下使用iSCSI存储
查看>>
Java3D实例应用-载入3ds 模型
查看>>
数据库程序Java就该这样学
查看>>
程序员编程艺术第二十五章:Jon Bentley:90%无法正确实现二分查找
查看>>
设置登录MacOSX WebView 对 iframe 设置cookie 有可能失败
查看>>
Debian下的'aptitude update'失败处理
查看>>
gcc 常用命令-Wall
查看>>
落实制度靠流程<摘自平安50万人的执行力>
查看>>
Citrix 服务器虚拟化之一 网络部署Xenserver 6.2
查看>>
java连接mysql批量写入数据
查看>>
回顾 Exchange 2007 SCC 安装-供需要的人参考!
查看>>
.Net下SQLite的DBHelp
查看>>
分布式文件系统之GPFS
查看>>
HDU 4577 X-Boxes
查看>>