洛谷P3031 中石油2557 Above the Median (数组模拟or树状数组求逆序数)
来源:互联网 发布:core keygen for mac 编辑:程序博客网 时间:2024/06/05 11:57
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 H_i (1 <= H_i <= 1,000,000,000) 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 {7, 3, 2, 6} is 6, and the median of {5, 4, 8} 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.
输入输出样例
4 6 10 5 6 2
输出样例#1: 复制
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}.
求一组相邻数的中位数大于等于k的子串个数,若有偶数个数,则取较大那个数为中位数。#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}ll pre[maxn],vis[maxn];int main(){ll n,i,j,k,ans,x;while(cin>>n>>k){mset(vis,0);for(i=1;i<=n;i++) //记录前缀和 {cin>>x;if(x>=k)pre[i]=pre[i-1]+1;elsepre[i]=pre[i-1]-1;}ll temp=0; //记录前i-1的状态下满足条件的区间个数 vis[0]=1; //记录前缀和出现的次数 pre[0]=0; //前缀0初始化为0 ans=0;for(i=1;i<=n;i++) //枚举1-n {if(pre[i]>pre[i-1]) //如果pre[i]>pre[i-1]以第i个数结尾的合法区间数 temp+=vis[pre[i]]+1; //前面的前缀和加上此前缀出现的次数,由于两者最多差1,所以加上1 elsetemp-=vis[pre[i-1]]-1; //同理,减去1 vis[pre[i]]++;ans+=temp; }cout<<ans<<endl; }return 0;}
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<queue>#include<cstdio>#define ll long long#define mset(a,x) memset(a,x,sizeof(a))using namespace std;const double PI=acos(-1);const int inf=0x3f3f3f3f;const double esp=1e-6;const int maxn=1e6+5;const int mod=1e9+7;int dir[4][2]={0,1,1,0,0,-1,-1,0};ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}ll lcm(ll a,ll b){return a/gcd(a,b)*b;}ll inv(ll b){if(b==1)return 1; return (mod-mod/b)*inv(mod%b)%mod;}ll fpow(ll n,ll k){ll r=1;for(;k;k>>=1){if(k&1)r=r*n%mod;n=n*n%mod;}return r;}ll n,vis[maxn],sum[maxn];ll lowbit(ll x){return x&(-x);}void add(ll x,ll value){ll i;for(i=x;i<=2*n+1;i+=lowbit(i)){sum[i]+=value;}}ll getsum(ll x){ll i,ans=0;for(i=x;i>0;i-=lowbit(i)){ans+=sum[i];}return ans;}int main(){ll i,j,k,x;while(~scanf("%lld%lld",&n,&k)){mset(vis,0);for(i=1;i<=n;i++){scanf("%lld",&x);if(x>=k)vis[i]=vis[i-1]+1; elsevis[i]=vis[i-1]-1; }ll ans=0;mset(sum,0);add(n+1,1);for(i=1;i<=n;i++){ans+=getsum(vis[i]+n+1);add(vis[i]+n+1,1); }cout<<ans<<endl;}return 0;}
- 洛谷P3031 中石油2557 Above the Median (数组模拟or树状数组求逆序数)
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组 求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 树状数组 求逆序数
- 树状数组求逆序数
- 树状数组求逆序数
- 求逆序数(树状数组)
- 树状数组 求逆序数
- 树状数组 求逆序数
- 树状数组求逆序数
- Java-异常笔记
- React Native 与 原生交互
- 说说DNS解析在wireshark里抓包内容(一)
- 文件下载
- 收集的RxJava 2.x入门教程系列专栏,文章链接
- 洛谷P3031 中石油2557 Above the Median (数组模拟or树状数组求逆序数)
- 移动端与PC端的测试差异性
- leetcode-448. Find All Numbers Disappeared in an Array
- 66-C++中的类型识别
- 简易Http客户端
- 任务三
- Java(7-3)
- React入门狂想曲(一)
- maven管理spring+springMvc+mybatis搭建