hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908
BestCoder Sequence
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.
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
1 1
1
5 3
4 5 3 2 1
1
3HintFor the second case, {3},{5,3,2},{4,5,3,2,1} are Bestcoder Sequence.
pid=4910">4910
4909 4906pid=4904">4904
4903#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;
}
为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;
}
hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)的更多相关文章
- [BestCoder Round#26] Apple 【组合数学】
题目链接:HDOJ - 5160 题目分析 第一眼看上去,要求统计所有不同排列对答案的贡献.嗯...完全没有想法. 但是,如果我们对每个数字单独考虑,计算这个数字在总答案中的贡献,就容易多了. 对于一 ...
- [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
BestCoder Sequence Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, ...
- BestCoder Round #3 A,B
A.预处理出来,0(1)输出. Task schedule Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ...
- BestCoder Round #11 (Div. 2) 题解
HDOJ5054 Alice and Bob Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/O ...
- hdu 5667 BestCoder Round #80 矩阵快速幂
Sequence Accepts: 59 Submissions: 650 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- BestCoder Round #89 02单调队列优化dp
1.BestCoder Round #89 2.总结:4个题,只能做A.B,全都靠hack上分.. 01 HDU 5944 水 1.题意:一个字符串,求有多少组字符y,r,x的下标能组成等比数列 ...
- BestCoder Round #90 //div all 大混战 一题滚粗 阶梯博弈,树状数组,高斯消元
BestCoder Round #90 本次至少暴露出三个知识点爆炸.... A. zz题 按题意copy Init函数 然后统计就ok B. 博弈 题 不懂 推了半天的SG..... 结果这 ...
- bestcoder Round #7 前三题题解
BestCoder Round #7 Start Time : 2014-08-31 19:00:00 End Time : 2014-08-31 21:00:00Contest Type : ...
- Bestcoder round #65 && hdu 5593 ZYB's Tree 树形dp
Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submissio ...
随机推荐
- 【01】在 Github 上编辑代码
[01]在 Github 上编辑代码 当你使用 GitHub,看一些文件(任何的文本文件或者仓库),能看到一个顶部右侧有一个小铅笔图标.点击即可编辑文档. 完成后,按照提示点击「Propose fil ...
- 【Ajax 3】JavaScript封装Ajax
导读:上一篇博客简单介绍了一下对Ajax中的核心对象XMLHttpRequest的封装.那么为了方便对Ajax的应用,我们还需要进一步的对Ajax的基本功能进行下一步的封装,不得不说的是早就有人专门写 ...
- Codeforces Round #321 (Div. 2)-A. Kefa and First Steps,暴力水过~~
A. Kefa and First Steps time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 在mac下安装matplotlib,xlrd
brew install freetype brew install libpng sudo easy_install pip#图形显示模块 sudo pip install matplotlib 输 ...
- Android应用的权限配置和权限列表
权限配置写在Mainifest.xml文件中: <?xml version="1.0" encoding="utf-8"?> <manifes ...
- JPos学习
基于JPos的消息交换系统 消息交换系统需求解读 消息交换系统不不是一个具体的业务系统,而是业务系统的运转的基础框架: 他的运转是体现在报文交换上的: 要定义一个可被不同业务系统使用的报文规范: 报文 ...
- poj2689素数问题
打算重新刷一下数论题,忘了很多了,水平也很差,此题入手就不顺了,刷了一个早上,只是一个简单 的素数应用罢了.题意:找出区间长度不超过10^6的最近的素数和最远的素数(相邻的), 算法:数在int范围内 ...
- 2016 ACM/ICPC 区域赛(北京) E 题 bfs
https://vjudge.net/problem/UVALive-7672 题意 输入一个五位数n 问由12345变到n的操作最少次数 不可达输出-1 有三种操作 1.交换相邻的位置 次数不 ...
- Codeforces 659A Round House【水题,细节】
题目链接: http://codeforces.com/contest/659/problem/A 题意: 一个圈,按逆时针编号,给定起点,方向和步数,问终点在几号? 分析: 很简单的模拟...注意答 ...
- POJ 3159 【朴素的差分约束】
好吧终于知道什么是“高大上”的差分约束了.嗷嗷 题意: 小朋友们分糖果,某个小朋友不想另外一个小朋友分到的糖果数比自己多N块以上. 求编号为N的小朋友最多比编号为1的小朋友多分多少块糖果. 思路: 差 ...