hdu 5701(区间查询思路题)

中位数计数

Time Limit: 12000/6000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 999    Accepted Submission(s): 383

Problem Description

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

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

Input

多组测试数据

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

第二行n个数,0≤每个数≤109,

Output

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

Sample Input

5
1 2 3 4 5

Sample Output

1 2 3 2 1

Source

2016"百度之星" - 初赛(Astar Round2B)

非常巧妙的思路,佩服

当找到 a[i] 时分三种情况考虑:

1:先去找a[i]的左边,如果左边扫过去时大于a[i]的书出现的次数与小于a[i]的数出现的次数相同,用一个num做计数器,num++和num--,每次num=0时,a[i]就是当前这个序列的中位数,计数器+1。

2:右边亦如此。

3:接下来是左右两边,用一个计数器记录num出现的次数,当左边为num时,右边就为-num,所以相加即为答案,为了防止数组越界,加个大数。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
const int N = 8000;
int a[N];
int ans[N],cnt[20000];
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF){
        for(int i=0;i<n;i++){
            scanf("%d",&a[i]);
            ans[i]=1; ///自己也算自己的中位数
        }
        for(int i=0;i<n;i++){
            memset(cnt,0,sizeof(cnt));
            int num = 0;
            for(int j=i-1;j>=0;j--){ ///往左区间找,看有多少满足a[i]是中位数的
                if(a[j]<a[i]) num++;
                else num--;
                if(num==0) ans[i]+=1; ///如果num=0,则证明此时的小于a[i]的数和大于a[i]的数数量相同,a[i]是中位数
                cnt[N+num]++;
            }
            num = 0;
            for(int j=i+1;j<n;j++){
                if(a[j]<a[i]) num++;
                else num--;
                if(num==0) ans[i]+=1;
                ans[i]+=cnt[N-num];
            }
        }
        for(int i=0;i<n;i++){
            if(i!=n-1) printf("%d ",ans[i]);
            else printf("%d\n",ans[i]);
        }
    }
}
时间: 2024-08-07 02:11:25

hdu 5701(区间查询思路题)的相关文章

HDU 平分宝石(思路题)

Problem H Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Total Submission(s) : 48   Accepted Submission(s) : 16 Special Judge Problem Description DreamGrid has $n$ classmates numbered from $1$ to $n$. Some of them a

hdu 4961 数学杂题

http://acm.hdu.edu.cn/showproblem.php?pid=4961 先贴个O(nsqrtn)求1-n所有数的所有约数的代码: vector<int>divs[MAXN]; void caldivs() { for(int i=1;i<MAXN;i++) for(int j=i;j<MAXN;j+=i) divs[j].push_back(i); } 有了这个当时理下思路就可写了,但是重复数处理注意: 1.用一个数组vis[]  vis[i]=1表示i存在

HDU Multiplication table 阅读题

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4951 题意:给一个P进制的乘法表,行和列分别代表0~p-1,第i行第j*2+1和第j*2+2列代表的是第i行的数x和第j列的数的乘积,不过这个乘法表被人弄乱了,原来的0~p-1被映射到了一个新的0~p-1的permutation(以前在CF上看见的permutation表示1~P,所以稍有迷茫),分别代表了新的不同的数,乘法表上的数也都被映射了.现在要找出映射到了什么新的数. 思路:毫无疑问,出现最多的

hdu 2586 LCA模板题(离线算法)

http://acm.hdu.edu.cn/showproblem.php?pid=2586 Problem Description There are n houses in the village and some bidirectional roads connecting them. Every day peole always like to ask like this "How far is it if I want to go from house A to house B&quo

Saving Princess claire_(hdu 4308 bfs模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2305    Accepted Submission(s): 822 Problem Description Princess claire_ wa

HDU 2092 整数解 --- 水题

x+y = n, x*y = m; y = n - x; x * ( n - x) = m nx - x^2 = m; x^2 - nx + m = 0; △ = sqrt(n^2 - 4m) 要有整数解即△需要为可开方数即可. /* HDU 2092 整数解 --- 水题 */ #include <cstdio> #include <cmath> int main() { double n, m; while (scanf("%lf%lf", &n,

HDU 4279 Number 规律题

题意: 定义函数F(x) : 区间[1,x]上的y是满足:GCD(x,y)>1 && x%y>0的 y的个数. 问:对于任意区间[l,r] 上的F(l···r) 有几个函数值是奇数的. 打表找规律. 打的是[1,x]区间的结果 把所有结果不相同的值打出来(因为结果是递增的,所以只观察不相同的结果) 发现:ans = x/2-2 || x/2-1 再把所有结果不同 && x/2-1的值打出来 发现 sqrt(x) &1 == 1 得到:ans = x/2-

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int

HDU 1556 区间查询

Color the ball Time Limit: 3000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的“小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一