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

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;
}


分享到:
评论

相关推荐

    hdu.rar_hdu

    3. **数据结构**:数组、链表、栈、队列、树(二叉树、平衡树如AVL和红黑树)、图(邻接矩阵、邻接表、最小生成树、拓扑排序等)。 4. **字符串处理**:KMP算法、Manacher's Algorithm、Rabin-Karp算法等。 5. **...

    hdu acm1166线段树

    hdu 1166线段树代码

    离线OJ题库(HDU ZJU等,部分有答案)

    离线OJ题库是为编程爱好者和程序员提供的一种便捷的学习资源,主要包含来自不同在线判题系统(如HDU - 浙江大学多模式在线评测系统,ZJU - 浙江大学在线评测系统等)的编程题目。这类题库通常包含多种编程语言的题目...

    hdu.rar_HDU 1089.cpp_OJ题求和_hdu_horsekw5_杭电obj

    【标题】"hdu.rar_HDU 1089.cpp_OJ题求和_hdu_horsekw5_杭电obj" 提供的信息是关于一个压缩文件,其中包含了一个名为 "HDU 1089.cpp" 的源代码文件,这个文件是为了解决杭州电子科技大学(Hangzhou Dianzi ...

    ACM HDU题目分类

    ACM HDU 题目分类是指对 HDU 在线判题系统中题目的分类,总结了大约十来个分类。这些分类将有助于编程选手更好地理解和解决问题。 DP 问题 DP(Dynamic Programming,动态规划)是一种非常重要的算法思想,在 ACM ...

    HDU_2010.rar_hdu 2010_hdu 20_hdu acm20

    "hdu"可能是指杭州电子科技大学(Hangzhou Dianzi University),这所学校经常举办ACM编程竞赛,并有自己的在线判题系统——HDU Online Judge,供参赛者提交代码并测试解决方案。 【压缩包子文件的文件名称列表】中...

    二叉搜索树练习 HDU3791

    总结来说,"二叉搜索树练习 HDU3791"是一道关于二叉搜索树操作的编程题,可能需要实现插入、删除、查找等基本操作,并通过分析`Main.java`源码来理解和解决问题。同时,可能需要借助各种工具进行调试和测试,以确保...

    HDU图论题目分类

    HDU图论题目分类是指在杭州电子科技大学(Hangzhou Dianzi University)的判题平台HDU OJ(Online Judge)上收录的一系列图论题目的分类。本分类涵盖了图论领域的多种类型的题目,涉及到图论的基本概念、图的遍历、...

    HDU题目java实现

    【标题】"HDU题目java实现"所涉及的知识点主要集中在使用Java编程语言解决杭州电子科技大学(HDU)在线评测系统中的算法问题。HDU是一个知名的在线编程竞赛平台,它提供了大量的算法题目供参赛者练习和提交解决方案...

    HDU DP动态规划

    【描述】提到的"HDU的一题"可能是指HDU(杭州电子科技大学)在线判题系统中的一道动态规划题目。这个系统经常被用来举办各类编程竞赛,包括ACM/ICPC(国际大学生程序设计竞赛)等。描述中的省略部分可能包含了具体...

    杭电HDU2050题的ac程序

    一个十分简单的程序,能够ac杭电hdu的第2050题,无注释,简单明了

    HDU acm-PPT课件

    ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest,简称ICPC或ACM/ICPC)是一项全球性的竞赛,旨在激发大学生对计算机科学的兴趣,提升他们的算法设计和编程能力。HDU(杭州电子科技大学...

    HDU最全ac代码

    HDU(Hangzhou Dianzi University)是国内外知名的在线编程竞赛平台,主要服务于ACM/ICPC(国际大学生程序设计竞赛)以及相关的算法训练。"HDU最全ac代码"这个压缩包很可能是包含了在HDU平台上解题通过的完整源代码...

    HDU题库.zip

    HDU题库.zip是一个压缩包文件,包含了从1000到6543号的所有HDU(杭州电子科技大学在线编程平台,也被称为HDU Online Judge)编程竞赛题目。这个资源对于学习算法、提高编程技能以及准备各类编程竞赛的用户来说极其...

    杭电ACMhdu1163

    2. **数据结构**:常用的数据结构包括数组、链表、栈、队列、树(二叉树、平衡树如AVL和红黑树)、图等,对于不同的问题,选择合适的数据结构能有效提高解题效率。 3. **字符串处理**:杭电ACM中的题目可能涉及到...

    ACM hdu 线段树题目+源代码

    ACM hdu 线段树题目+源代码 线段树是一种非常重要的数据结构,它广泛应用于算法竞赛和实际编程中。今天,我们将通过 ACM hdu 的几个题目来学习和掌握线段树的基本概念和应用。 线段树的基本概念 线段树是一种...

    acm课件简单数学题(杭电)(HDU)

    在ACM(国际大学生程序设计竞赛,International Collegiate Programming Contest,简称ICPC)中,数学是至关重要的一部分,尤其是在解决杭电(Hangzhou Dianzi University,简称HDU)的题目时。本课件"acm课件简单...

    HDU ACM HDOJ 部分基础题AC代码

    本题是基于ACM(国际大学生程序设计竞赛)的一道基础题目,主要涉及大整数的加法运算。ACM竞赛中的题目往往需要程序员具备快速解决问题的能力,对于算法的理解和实现要求较高。这里我们将分析题目要求以及提供的代码...

    ACM HDU 2000-2099 解题报告 杭电 ACM

    《ACM HDU 2000-2099 解题报告 杭电 ACM》是一份详尽的编程竞赛解题集,主要涵盖了杭电(Hangzhou Dianzi University)在线判题系统(HDU OJ)上的2000至2099号题目。这份解题报告是针对参与ACM/ICPC(国际大学生...

    hdu acm 2010多校联合(10)1009

    【标题】"hdu acm 2010多校联合(10)1009" 是2010年ACM/ICPC(国际大学生程序设计竞赛)多校联合比赛的第十场比赛中的一道题目,编号为1009。这道题目涉及到算法竞赛中的常见问题解决策略,通常在这样的比赛中,...

Global site tag (gtag.js) - Google Analytics