[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)

BestCoder Sequence

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

BestCoder Round #3

解题思路:

题意为 给定一个1 -N的排列,再给定一个数M(1<=M<=N)。问有多少连续的长度为奇数子序列,使得M在当中为中位数(M在子序列中)。

比方例子

5 3

4 5 3 2 1 N=5, M=3

{3},{5,3,2},{4,5,3,2,1} 为符合题意的连续子序列....

当时做的时候把包括M的全部长度为0,1,2.......的连续子序列都枚举了出来。然后推断推断M是否是中位数。结果
果断超时.......

贴一下题解思路:

写了一堆字,CSDN的排版太....了,贴图片把.

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int maxn=40010;
int sum[maxn];
int cnt[maxn*2];
int n,m;
int val,p;

int main()
{
    while(scanf("%d%d",&n,&m)==2)
    {
        sum[0]=0;
        for(int i=1;i<=n;i++)
        {
            sum[i]=sum[i-1];
            scanf("%d",&val);
            if(val<m)
                sum[i]--;
            else if(val>m)
                sum[i]++;
            else
                p=i;
        }
        memset(cnt,0,sizeof(cnt));
        for(int i=0;i<p;i++)
            cnt[sum[i]+maxn]++;
        int ans=0;
        for(int i=p;i<=n;i++)
            ans+=cnt[sum[i]+maxn];
        printf("%d\n",ans);
    }
    return 0;
}
时间: 2024-10-17 08:19:55

[BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)的相关文章

hdu 4908 BestCoder Sequence(计数)

题目链接:hdu 4908 BestCoder Sequence 题目大意:给定N和M,N为序列的长度,由1~N组成,求有多少连续的子序列以M为中位数,长度为奇数. 解题思路:v[i]记录的是从1~i这些位置上有多少个数大于M,i-v[i]就是小于M的个数.pos为M在序列中的位置.如果有等式i?j=2?(v[i]?v[j?1]),i≥pos≥j 那么i和j既是一组满足的情况.将等式变形i?2?v[i]=j?2?v[j?1]. #include <cstdio> #include <cs

hdu 4908 BestCoder Sequence 找M为中位数的串的数目, 需要预处理

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 271    Accepted Submission(s): 112 Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing

HDU 4908 BestCoder Sequence(组合数学)

HDU 4908 BestCoder Sequence 题目链接 题意:给定一个序列,1-n的数字,选定一个作为中位数m,要求有多少连续子序列满足中位数是m 思路:组合数学,记录下m左边和右边一共有多少种情况大于m的数字和小于n数组的差,然后等于左边乘右边所有的和,然后最后记得加上左右两边差为0的情况. 当时也是比较逗,还用树状数组去搞了,其实完全没必要 代码: #include <cstdio> #include <cstring> #define lowbit(x) (x&am

[BestCoder Round #3] hdu 4909 String (状压,计数)

String Problem Description You hava a non-empty string which consists of lowercase English letters and may contain at most one '?'. Let's choose non-empty substring G from S (it can be G = S). A substring of a string is a continuous subsequence of th

hdu 4908 BestCoder Sequence【DP】

题目链接 :http://acm.hdu.edu.cn/showproblem.php?pid=4908 题目大意:给出一个排列,一个m,求出这个排列的连续子序列中有多少个序列式以m为中位数. 由于是一个排列,不会出现重复的数字,记录一下m的位置index,然后以index为分界线,往左求出s[i](表示从i到index之间有多少大于m),b[i](表示从i到index之间有多少小于m),往右求出s[i](表示从index到i之间有多少大于m),b[i](表示从index到i之间有多少小于m).

HDU 4908——BestCoder Sequence

BestCoder Sequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 169    Accepted Submission(s): 82 Problem Description Mr Potato is a coder. Mr Potato is the BestCoder. One night, an amazing

Bestcoder round #65 &amp;&amp; hdu 5592 ZYB&#39;s Premutation 线段树

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 175    Accepted Submission(s): 74 Problem Description ZYB has a premutation P,but he only remeber the reverse log of each prefix of the premutat

Bestcoder round #65 &amp;&amp; hdu 5593 ZYB&#39;s Tree 树形dp

Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 354    Accepted Submission(s): 100 Problem Description ZYB has a tree with N nodes,now he wants you to solve the numbers of nodes distanced no m

[BestCoder Round #4] hdu 4932 Miaomiao&#39;s Geometry (贪心)

Miaomiao's Geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 363    Accepted Submission(s): 92 Problem Description There are N point on X-axis . Miaomiao would like to cover them ALL by