`
473687880
  • 浏览: 484206 次
文章分类
社区版块
存档分类
最新评论

hdu 4251 The 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): 733Accepted Submission(s): 351


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

Source

Recommend
We have carefully selected several similar problems for you:42554247425242464248

题意:

给你一个数组。问你两个端点a,b。问你在[a,b]中的中间值。比如1,2,5的中间值为2。

思路:

划分树裸题。会就很简单。不会就无从下手。划分树思想还是类似线段树分治的思想。二分区间再慢慢定位。

由于网上都没怎么细讲。大都直接给的代码。我还是花了一下午研究才茅塞顿开的。明白了就很简单。虽然是

水题1A还是很爽的。呵呵~又多学了个数据结构了。

详细见代码:

#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int n,m,md;
int seg[20][maxn],lnum[20][maxn],sa[maxn];
void init()
{
    memset(lnum,0,sizeof lnum);
    sort(sa,sa+n);
}
void btree(int L,int R,int d)
{
    int i,lm,ls,rs,mid;
    md=max(d,md);
    if(L==R)
        return;
    mid=(L+R)>>1;
    lm=mid-L+1;
    ls=L;
    rs=mid+1;
    for(i=L;i<=R;i++)
        if(seg[d][i]<sa[mid])
            lm--;
    for(i=L;i<=R;i++)
    {
        if(i==L)
            lnum[d][i]=0;
        else
            lnum[d][i]=lnum[d][i-1];
        if(seg[d][i]==sa[mid])
        {
            if(lm>0)
            {
                lnum[d][i]++;
                seg[d+1][ls++]=seg[d][i];
                lm--;
            }
            else
                seg[d+1][rs++]=seg[d][i];
        }
        else if(seg[d][i]<sa[mid])
        {
            lnum[d][i]++;
            seg[d+1][ls++]=seg[d][i];
        }
        else
            seg[d+1][rs++]=seg[d][i];
    }
    btree(L,mid,d+1);
    btree(mid+1,R,d+1);
}
int qu(int L,int R,int l,int r,int d,int k)
{
    if(L==R)
        return seg[d][L];
    int s,ss,b,bb,mid;
    if(l==L)
        ss=0;
    else
        ss=lnum[d][l-1];//[1,l-1]进入左树的个数
    s=lnum[d][r]-ss;//[l,r]进入左树的个数
    //printf("d %d ss %d s %d k %d\n",d,ss,s,k);
    mid=(L+R)>>1;
    if(s>=k)//在左树中
        return qu(L,mid,L+ss,L+ss+s-1,d+1,k);//注意边界。可以确定L+ss-1不为所求所以从L+ss
    else
    {
        bb=l-L-ss;//[1,l-1]进入右树的个数
        b=r-l+1-s;//[l,r]进入右树的个数
        return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s);//mid+1+bb+b-1
    }
}

int main()
{
    int a,b,i,j,cas=1;

    while(~scanf("%d",&n))
    {
        for(i=1;i<=n;i++)
        {
            scanf("%d",&seg[0][i]);
            sa[i]=seg[0][i];
        }
        md=0;
        init();
        btree(1,n,0);
//        for(i=0;i<=md;i++)
//        {
//            for(j=1;j<=n;j++)
//                printf("%d ",seg[i][j]);
//            printf("\n");
//        }
        scanf("%d",&m);
        printf("Case %d:\n",cas++);
        while(m--)
        {
            scanf("%d%d",&a,&b);
            printf("%d\n",qu(1,n,a,b,0,(b-a+2)/2));
        }
    }
    return 0;
}


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics