题目链接:

中位数计数

Problem Description
 

中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均数作为中位数。

现在有nn个数,每个数都是独一无二的,求出每个数在多少个包含其的区间中是中位数。

Input
 

多组测试数据

第一行一个数n(n≤8000)

第二行n个数,0≤每个数≤10​^9​​,

Output
 

N个数,依次表示第ii个数在多少包含其的区间中是中位数。

Sample Input
 
5
1 2 3 4 5
Sample Output
 
1 2 3 2 1

题意:

思路:

一个区间的中位数的,在这个区间内别它大的数的个数等于比它小的数的个数等于总数的一半,取一个区间[l,r],x[i]  y[i]分别表示在前i个数中有多少个数比中位数小和大,
x[r]-x[l-1]==y[r]-y[l-1]变换后就是x[r]-y[r]==x[l-1]-y[l-1];对于每一位数,如果他是中位数,那么先把它前边的数的x[i]-y[i]都记录下来,然后从它开始打结尾看有多少个
x[j]-y[j]==x[i]-y[i]; AC代码:
#include <bits/stdc++.h>
/*
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
const LL mod=1e9+;
const double PI=acos(-1.0);
const int inf=0x3f3f3f3f;
const int N=;
int n,m;
int x[N],y[N],a[N],flag[*N];
int main()
{
while(scanf("%d",&n)!=EOF)
{
Riep(n)scanf("%d",&a[i]);
Riep(n)
{
mst(flag,);
int ans=;
x[]=y[]=;
for(int j=;j<=n;j++)
{
y[j]=y[j-];
x[j]=x[j-];
if(a[j]>a[i])y[j]++;
if(a[j]<a[i])x[j]++;
}
flag[]++;
for(int j=;j<i;j++)
{
int f=x[j]-y[j];
if(f>=)flag[f]++;
else flag[+f]++;
}
for(int j=i;j<=n;j++)
{
int f=x[j]-y[j];
if(f>=)ans+=flag[f];
else ans+=flag[+f];
}
if(i!=n) printf("%d ",ans);
else printf("%d\n",ans);
}
}
return ;
}

hdu-5701 中位数计数(中位数)的更多相关文章

  1. ACM学习历程—HDU5701 中位数计数(中位数 && 计数排序)

    http://acm.hdu.edu.cn/showproblem.php?pid=5701 这是这次百度之星初赛2B的第六题.之前白山云做过类似的题,省赛完回来,我看了一下大概就有这样的思路:首先枚 ...

  2. hdu 5701 中位数计数 思路题

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  3. HDU 5701 中位数计数 百度之星初赛

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Sub ...

  4. HDU 5701 ——中位数计数——————【思维题】

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  5. 51nod 1682 中位数计数

    1682 中位数计数基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 中位数定义为所有值从小到大排序后排在正中间的那个数,如果值有偶数个,通常取最中间的两个数值的平均 ...

  6. 51nod 1682 中位数计数(前缀和)

    51nod 1682 中位数计数 思路: sum[i]表示到i为止的前缀和(比a[i]小的记为-1,相等的记为0,比a[i]大的记为1,然后求这些-1,0,1的前缀和): hash[sum[i]+N] ...

  7. 51 nod 1682 中位数计数

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1682 1682 中位数计数 基准时间限制:1 秒 空间限制: ...

  8. hdu 5701(区间查询思路题)

    中位数计数 Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Subm ...

  9. hdu 5868 Polya计数

    Different Circle Permutation Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K ...

随机推荐

  1. libevent中定时器的使用方法

    #include <stdio.h> #include <stdlib.h> #include <sys/time.h> #include <signal.h ...

  2. $( document ).ready()&$(window).load()

    $( document ).ready() https://learn.jquery.com/using-jquery-core/document-ready/ A page can't be man ...

  3. Linux 上的基础网络设备详解

    抽象网络设备的原理及使用 网络虚拟化是 Cloud 中的一个重要部分.作为基础知识,本文详细讲述 Linux 抽象出来的各种网络设备的原理.用法.数据流向.您通过此文,能够知道如何使用 Linux 的 ...

  4. xshell linux传文件

    yum  install lrzsz 安装完毕即可使用 rz,sz是便是Linux/Unix同Windows进行ZModem文件传输的命令行工具 windows端需要支持ZModem的telnet/s ...

  5. word2013中取消句首字母自动大写

    经常使用word的朋友都知道word中一行的首字母会自动大写,这给用户带来方便的同时,也产生了问题,因为有时候我们并不希望每行开头的首字母大写.要取消首字母自动大写可以取消勾选"首句字母大写 ...

  6. linux命令备忘

    yum info softname 查看安装包信息 yum install softname 安装包名 安装 nginx http://jingyan.baidu.com/article/aa6a2c ...

  7. linux C(hello world)程序调试

    程序的调试(先得安装gdb工具,以root身份执行命令:sudo apt-get install gdb) 程序的调试是一个很重要的环节,windows IDE下那些强大的调试功能,Linux以什么来 ...

  8. Mysql命令alter add:增加表的字段

    alter add命令用来增加表的字段. alter add命令格式:alter table 表名 add字段 类型 其他; 例如,在表MyClass中添加了一个字段passtest,类型为int(4 ...

  9. [AngularJS] Using the Angular scope $destroy event and method

    With Angular scopes, you have access to a $destroy event that can be used to watch $scope events. Th ...

  10. iOS开发——UI篇Swift篇&UISwitch/UIStepper

    UISwitch/UIStepper override func viewDidLoad() { super.viewDidLoad() titleLabel.text = titleString / ...