【CUGBACM15级BC第三场 B】hdu 4908 BestCoder Sequence

70 篇文章 0 订阅

BestCoder Sequence

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


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.
 
题意:
给一个序列,里面是1~N的排列,给出M,问以M为中位数的奇数长度的序列个数。

分析:
就是记录M左右两边区间比M大的和比M小的差值的个数,再相乘。
可以先遍历左边记录差值个数,右边遍历直接加上左边的一样的差值

#include <iostream>
#include <set>
#include <map>
#include <stack>
#include <cmath>
#include <queue>
#include <cstdio>
#include <bitset>
#include <string>
#include <vector>
#include <iomanip>
#include <cstring>
#include <algorithm>
#include <functional>
#define PI acos(-1)
#define eps 1e-8
#define inf 0x3f3f3f3f
#define debug(x) cout<<"---"<<x<<"---"<<endl
typedef long long ll;
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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值