列表

详情


NC24120. [USACO 2011 Nov G]Above the Median

描述

Farmer John has lined up his N (1 <= N <= 100,000) cows in a row to measure their heights; cow i has height nanometers--FJ believes in precise measurements! He wants to take a picture of some contiguous subsequence of the cows to submit to a bovine photography contest at the county fair.
The fair has a very strange rule about all submitted photos: a photograph is only valid to submit if it depicts a group of cows whose median height is at least a certain threshold X (1 <= X <= 1,000,000,000).
For purposes of this problem, we define the median of an array A[0...K] to be A[ceiling(K/2)] after A is sorted, where ceiling(K/2) gives K/2 rounded up to the nearest integer (or K/2 itself, it K/2 is an integer to begin with). For example the median of is 6, and the median of is 5.
Please help FJ count the number of different contiguous subsequences of his cows that he could potentially submit to the photography contest.
给出一串数字,问中位数大于等于X的连续子串有几个。(这里如果有偶数个数,定义为偏大的那一个而非中间取平均)

输入描述

* Line 1: Two space-separated integers: N and X.
* Lines 2..N+1: Line i+1 contains the single integer H_i.

输出描述

* Line 1: The number of subsequences of FJ's cows that have median at least X. Note this may not fit into a 32-bit integer.

示例1

输入:

4 6
10
5
6
2

输出:

7

说明:

FJ's four cows have heights 10, 5, 6, 2. We want to know how many
contiguous subsequences have median at least 6.
There are 10 possible contiguous subsequences to consider. Of these, only 7
have median at least 6. They are {10}, {6}, {10, 5}, {5, 6}, {6, 2}, {10, 5, 6}, {10, 5, 6, 2}.

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 21ms, 内存消耗: 6048K, 提交时间: 2020-10-18 19:23:16

#include<bits/stdc++.h>  
using namespace std;  
typedef long long ll;  
int k,n,x;  
int c[1010101];  
void add(int k,int num) //a[k]的值增加num  
{  
    while(k<=2*n+1)  
    {  
        c[k]+=num;  
        k+=k&-k;  
    }  
}  
ll read(int k)//1~k的区间和  
{  
    ll sum=0;  
    while(k)  
    {  
        sum+=c[k];  
        k-=k&-k;  
    }  
    return sum;  
}  
ll dp[101010];  
int main()  
{  
    while(cin>>n>>k)  
    {  
        memset(dp,0,sizeof(dp));  
        memset(c,0,sizeof(c));  
        for(int i=1;i<=n;i++)  
        {  
            scanf("%d",&x);  
            dp[i]=dp[i-1];  
            if(x>=k)dp[i]++;  
        }  
        ll ans=0;  
        add(n+1,1);  
        for(int i=1;i<=n;i++)  
        {  
            ans+=read(2*dp[i]-i +n+1);  
            add(2*dp[i]-i +n+1,1);  
        }  
        printf("%lld\n",ans);  
    }  
}  

C++11(clang++ 3.9) 解法, 执行用时: 17ms, 内存消耗: 2816K, 提交时间: 2020-10-20 15:07:35

#include<cstdio>
long long n,t,x,ans,a[100001],b[200001];
int main()
{
	scanf("%lld%lld",&n,&x);
	a[0]=n;
	b[n]=1;
	for(int i=1;i<=n;i++)
	{
		scanf("%lld",&t);
		if(t>=x)
		a[i]=a[i-1]+1;
		else
		a[i]=a[i-1]-1;
	}
	t=0;
	for(int i=1;i<=n;i++)
	{
		if(a[i]>a[i-1])
		t-=b[a[i]];
		else
		t+=b[a[i-1]];
		b[a[i]]++;
		ans+=t;
	}
	printf("%lld",n*(n+1)/2-ans);
}

上一题