• hdoj_4251The Famous ICPC Team Again


    The Famous ICPC Team Again

    Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 645    Accepted Submission(s): 307


    Problem Description
    When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted in a line, each time they would choose one of them from a specified segment of the line.

    Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
     

    Input
    For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
     

    Output
    For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
     

    Sample Input
    5 5 3 2 4 1 3 1 3 2 4 3 5 5 10 6 4 8 2 3 1 3 2 4 3 5
     

    Sample Output
    Case 1: 3 3 2 Case 2: 6 6 4
    //求给定区间[x,y]的中位数,也就是[x,y]区间的第(y - x) / 2 + 1大值。
    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <algorithm>
    using namespace std;
    #pragma warning(disable : 4996)
    const int MAXN = 100010;
    #define mid ((l + r) >> 1)
    int t[20][MAXN],sum[20][MAXN];
    int as[MAXN];
    //以下为查找区间第k小划分树
    void build(int p,int l,int r)
    {
    	int lm = 0, i, ls = l, rs = mid + 1;//lm表示应被放入左子树且与中位数相等的数有多少个,ls为左子树的起始位置,rs为右子树的起始位置
    	for(i = mid; i >= l; i--) //求lm
    	{
    		if(as[i] == as[mid])
    		{
    			lm++;
    		}
    		else
    		{
    			break;
    		}
    	}
    	for(i = l; i <= r; i++)
    	{
    		if(i == l)//这里要特殊讨论
    		{
    			sum[p][i] = 0;
    		}
    		else
    		{
    			sum[p][i] = sum[p][i-1];
    		}
    		if(t[p][i] == as[mid])//若与中位数相等则判断是否应该被放入左子树
    		{
    			if(lm != 0)
    			{
    				lm--;
    				sum[p][i]++;
    				t[p+1][ls++] = t[p][i];
    			}
    			else
    			{
    				t[p+1][rs++] = t[p][i];
    			}
    		}
    		else if(t[p][i] < as[mid])//查找区间第K大即为>
    		{
    			sum[p][i]++;
    			t[p+1][ls++]=t[p][i];
    		}
    		else
    		{
    			t[p+1][rs++] = t[p][i];
    		}
    	}
    	if(l==r)
    	{
    		return;
    	}
    	build(p + 1, l, mid);
    	build(p + 1, mid + 1, r);
    }
    int query(int p, int l, int r, int ql, int qr, int k)
    {
    	int s, ss;//s表示l到ql-1的区间内放入左子树的个数,ss表示区间[ql,qr]被放入左子树的个数
    	if(l == r)//找到所求的数
    	{
    		return t[p][l];
    	}
    	if(ql == l)
    	{
    		s = 0, ss = sum[p][qr];
    	}
    	else
    	{
    		s = sum[p][ql-1], ss = sum[p][qr] - s;
    	}
    	if(k<=ss)//要找的数在左子树中
    	{
    		return query(p + 1, l, mid, l + s, l + sum[p][qr] - 1, k);
    	}
    	else//要找的数在右子树中
    	{
    		return query(p + 1, mid + 1, r, mid + 1 - l + ql - s, mid + 1 - l + qr - sum[p][qr], k - ss);
    	}
    }
    
    int main()
    {
    	freopen("in.txt", "r", stdin);
    	int n, m, x, y, z;
    	int count = 1;
    	while(scanf("%d", &n) != EOF)
    	{
    		printf("Case %d:\n", count++);
    		for (int i = 1; i <= n; i++)
    		{
    			scanf("%d", &as[i]);
    			t[0][i] = as[i];
    		}
    		sort(as + 1, as + n + 1);
    		build(0, 1, n);
    		scanf("%d", &m);
    		while (m--)
    		{
    			scanf("%d %d", &x, &y);
    			z = (y - x) / 2 + 1;
    			printf("%d\n", query(0, 1, n, x, y, z));
    		}
    	}
    	return 0;
    }


  • 相关阅读:
    Django框架 之 querySet详解
    Django框架 之 admin管理工具(组件使用)
    Django框架 之 跨域请求伪造
    Django框架 之 form组件的钩子
    Django框架 之 Form表单和Ajax上传文件
    Django框架 之 modelform组件
    [BZOJ4477] [JSOI2015]字符串树(可持久化Trie+LCA)
    [BZOJ 4523] [CQOI2016]路由表(Trie+单调栈)
    [Luogu2870] [USACO07DEC]最佳牛线Best Cow Line(贪心+后缀数组)
    [BZOJ3998][TJOI2015]弦论(后缀数组)
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835013.html
Copyright © 2020-2023  润新知