hdu 4908(思路题)

BestCoder Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1614    Accepted Submission(s): 566

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

这道题有加强版--hdu 5400 有兴趣可以做下。

今天看到给秒了,上次百度之星碰到这种题是懵逼,这种题果然要多刷才会有经验。由于是中位数,所以我们分三种情况讨论。

1.往左边区间找,当大于M的数等于小于M的数时,M肯定是中位数,计数器+1。

2.往右边区间找同理。

3。对于左右两边,我们在往左边计数时弄一个数组记录大于(小于)M的数出现num个的次数为cnt[num],当往右边计数时,碰到大于(小于)M的数有num个时,对应左边有cnt[-num]个序列,计数器+=cnt[-num],由于数组下标不能为负,所以加个大数N。

#include <stdio.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <vector>
using namespace std;
const int N = 40005;
int a[N];
int cnt[2*N];
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF){
        int id = -1;
        for(int i=1;i<=n;i++){
            scanf("%d",&a[i]);
            if(a[i]==m){
                id = i;
            }
        }
        memset(cnt,0,sizeof(cnt));
        int ans = 0;
        int num = 0,j=0;
        for(int i=id-1;i>=1;i--){ ///往左计数
            j++;
            if(a[i]<m) num++;
            else num--;
            if(num==0) ans++;
            cnt[N+num]++;
        }
        num = 0,j=0;
        for(int i=id+1;i<=n;i++){
            j++;
            if(a[i]<m) num++;
            else num--;
            if(num==0) ans++;
            ans+=cnt[N-num];
        }
        printf("%d\n",ans+1);
    }
}
时间: 2024-08-07 04:11:28

hdu 4908(思路题)的相关文章

hdu 5063(思路题-反向操作数组)

Operation the Sequence Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 842    Accepted Submission(s): 288 Problem Description You have an array consisting of n integers: a1=1,a2=2,a3=3,…,an=n. T

hdu 5400(思路题)

Arithmetic Sequence Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1445    Accepted Submission(s): 632 Problem Description A sequence b1,b2,?,bn are called (d1,d2)-arithmetic sequence if and on

hdu 5101(思路题)

Select Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1599    Accepted Submission(s): 443 Problem Description One day, Dudu, the most clever boy, heard of ACM/ICPC, which is a very interesting

hdu 4859(思路题)

Goffi and Squary Partition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1171    Accepted Submission(s): 402 Problem Description Recently, Goffi is interested in squary partition of integers.

HDU 5301 思路题

给出n*m的矩阵,里面有一个坏点,不覆盖这个坏点的矩阵填满n*m的矩阵,使得这些矩阵的最大面积最小,并输出最小面积 先把矩阵转换为n<=m的矩阵,并把坏点通过镜像转移到左上方 ans=MAX(矩阵最中心点到两边距离的最小值,MIN(坏点下方的点到矩阵左端和下端的最小值)); #include "stdio.h" #include "string.h" int ans,n,m,x,y; int Max(int a,int b) { if (a<b) ret

HDU 4908 (杭电 BC #3 1002题)BestCoder Sequence(DP)

题目地址:HDU 4908 这个题是从m开始,分别往前DP和往后DP,如果比m大,就比前面+1,反之-1.这样的话,为0的点就可以与m这个数匹配成一个子串,然后左边和右边的相反数的也可以互相匹配成一个子串,然后互相的乘积最后再加上就行了.因为加入最终两边的互相匹配了,那就说明左右两边一定是偶数个,加上m就一定是奇数个,这奇数个的问题就不用担心了. 代码如下: #include <iostream> #include <stdio.h> #include <string.h&g

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

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 4416 水题 浙大计算机研究生复试上机考试-2005年 可是发现自己写代码有问题

Spring3与Hibernate4整合时出现了nested exception is java.lang.NoClassDefFoundError: Lorg/hibernate/cache/CacheProvider. hibernate3的时候,用spring来控制sessionfactory用的可以是org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean,因为用的是hibernate4所以照猫画