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 ...
随机推荐
- 让你系统的了解shell
当你在进行登录时,系统会检查的文档:1. /etc/profile:首先,系统会检查这个文件,以定义如下这些变量:PATH.USER.LOGNAME.MAIL.HOSTNAME.HISTSIZE.IN ...
- Ext修改Confirm弹框按钮的默认值
- zoj 1109 Language of FatMouse(map)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- HDU3183A Magic Lamp,和NYOJ最大的数一样
A Magic Lamp Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...
- iOS-runtime-根据类名推送到任意控制器,且实现属性传值
// // WJRuntime.m // RuntimeSkip // // Created by tqh on 15/9/8. // Copyright (c) 2015年 tqh. All rig ...
- 【Kruscal最小生成树】D. Jungle Roads
https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/D [Accepted] #include<iostream> #in ...
- JS把数字金额转换成中文大写数字的函数
//把数字金额转换成中文大写数字的函数 function num2rmb ($num){ $c1="零壹贰叁肆伍陆柒捌玖"; $c2="分角元拾佰仟万拾佰仟亿" ...
- hdu 1496 hash
hash?判重,是否一样?相等?等式!没有想到,这次题做玩后,学到了HASH这一功能!当数据量在数组允许大小范围内时候即可!判断等式俩边是否相等,从而获得解的个数!从复杂度,n*m*k****,降到 ...
- Piggy-Bank--hdu1114(完全背包)
http://acm.hdu.edu.cn/showproblem.php?pid=1114 Problem Description Before ACM can do anything, a bud ...
- 洛谷—— P1133 教主的花园
https://www.luogu.org/problem/show?pid=1133 题目描述 教主有着一个环形的花园,他想在花园周围均匀地种上n棵树,但是教主花园的土壤很特别,每个位置适合种的树都 ...