标签:数学题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4908
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.
#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; }
#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(组合数学),布布扣,bubuko.com
hdu4908 & BestCoder Round #3 BestCoder Sequence(组合数学)
标签:数学题
原文地址:http://blog.csdn.net/u012860063/article/details/38373365