2020-09-05 划分树求某一区间的中位数

http://icpc.upc.edu.cn/problem.php?cid=2570&pid=6

题目描述

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.

输入

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.

输出

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

样例输入
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
样例输出
Case 1:
3
3
2
Case 2:
6
6
4

划分树模板
划分树讲解:https://blog.csdn.net/qq_38984851/article/details/81559956

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>

using namespace std;
int tree[20][100500]={0};
int to[20][100500]={0};
int num[100500]={0};
int sorted[100500]={0};
int build(int l,int r,int deep)
{
    if(l==r)
        return 0;
    int mid=(l+r)/2;
    int midd=sorted[mid];
    int suppose=mid-l+1;
    for(int i=l;i<=r;i++)
    {
        if(tree[deep][i]<midd)
            suppose--;
    }
    int sleft=l,sright=mid+1;
    for(int i=l;i<=r;i++)
    {
        if(i==l)
            to[deep][l]=0;
        else
            to[deep][i]=to[deep][i-1];
        if(tree[deep][i]<midd)
        {
            to[deep][i]++;
            tree[deep+1][sleft++]=tree[deep][i];
        }
        else if(tree[deep][i]==midd&&suppose>0)
        {
            suppose--;
            to[deep][i]++;
            tree[deep+1][sleft++]=tree[deep][i];
        }
        else
        {
            tree[deep+1][sright++]=tree[deep][i];
        }
    }
    build(l,mid,deep+1);
    build(mid+1,r,deep+1);
}
int query(int l,int r,int L,int R,int k,int deep)
{
    if(L==R)
        return tree[deep][L];
    int mid=(l+r)/2;
    int lef;
    int toleft;
    if(l==L)
        lef=0,toleft=to[deep][R];
    else
        lef=to[deep][L-1],toleft=to[deep][R]-lef;
    if(k<=toleft)
    {
        return query(l,mid,l+lef,l+toleft+lef-1,k,deep+1);
    }
    else
    {
        return query(mid+1,r,mid+L-l-lef+1,mid+R-l-toleft-lef+1,k-toleft,deep+1);
    }
}
int main()
{
    int n,m,summ=0;
    while(scanf("%d",&n)!=EOF)
    {
        summ++;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&num[i]);
            sorted[i]=num[i];
            tree[0][i]=num[i];
        }
        sort(sorted+1,sorted+n+1);
        build(1,n,0);
        printf("Case %d:\n",summ);
        scanf("%d",&m);
        for(int i=1;i<=m;i++)
        {
            int a,b,c; /// a,b,c代表查询a到b区间内第c大的数
            scanf("%d%d",&a,&b);
            c=(b-a)/2+1;
            printf("%d\n",query(1,n,a,b,c,0));
        }
    }
}

http://icpc.upc.edu.cn/problem.php?cid=2570&pid=3

题目描述

Mr. B loves to play with colorful stones. There are n colors of stones in his collection. Two stones with the same color are indistinguishable. Mr. B would like to select some stones and arrange them in line to form a beautiful pattern. After several arrangements he finds it very hard for him to enumerate all the patterns. So he asks you to write a program to count the number of different possible patterns.

Two patterns are considered different, if and only if they have different number of stones or have different colors on at least one position.

输入

Each test case starts with a line containing an integer n indicating the kinds of stones Mr. B have. Following this is a line containing n integers - the number of available stones of each color respectively. All the input numbers will be nonnegative and no more than 100.

输出

For each test case, display a single line containing the case number and the number of different patterns Mr. B can make with these stones, modulo 1,000,000,007, which is a prime number.

样例输入
3
1 1 1
2
1 2
样例输出
Case 1: 15
Case 2: 8
提示

In the first case, suppose the colors of the stones Mr. B has are B, G and M, the different patterns Mr. B can form are: B; G; M; BG; BM; GM; GB; MB; MG; BGM; BMG; GBM; GMB; MBG; MGB.
动态规划DP+组合数学
参照博客 https://blog.csdn.net/nobody_like_you/article/details/100016385

#pragma GCC optimize(1)
#pragma GCC optimize(2)
#pragma GCC optimize(3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll a[105]={0};
ll sum[105]={0};
ll dp[105][10500]={0};
const ll N=1e9+7;
ll c1[10550][115]={0};
int main()
{
    for(ll i=0;i<=10500;i++)
    {
        for(ll j=0;j<=i&&j<=105;j++)
        {
            if(j==0||i==j)
                c1[i][j]=1;
            else
                c1[i][j]=c1[i-1][j]+c1[i-1][j-1];
            c1[i][j]%=N;
        }
    }
    register ll n,sum1=0;
    while(~scanf("%lld",&n))
    {
        memset(dp,0,sizeof(dp));
        sum1++;
        for(register ll i=1;i<=n;i++)
            scanf("%lld",&a[i]),sum[i]=sum[i-1]+a[i];
        dp[0][0]=1;
        for(register ll i=1;i<=n;i++)
        {
            for(register ll j=0;j<=sum[i];j++)
            {
                for(register ll k=0;k<=a[i]&&k<=j;k++)
                {
                    dp[i][j]=dp[i][j]+c1[j][k]*dp[i-1][j-k];
                    dp[i][j]%=N;
                }
            }
        }
        register ll summ=0;
        for(register ll i=1;i<=sum[n];i++)
        {
            summ+=dp[n][i];
            summ%=N;
        }
        printf("Case %lld: %lld\n",sum1,summ);
        //cout<<"Case "<<sum1<<": "<<summ<<endl;
    }
}

题目描述

现有r个互不相同的盒子和n个互不相同的球,要将这n个球放入r个盒子中,且不允许有空盒子。问有多少种方法?
在这里插入图片描述

例如:有2个不同的盒子(分别编为1号和2号)和3个不同的球(分别编为1、2、3号),则有6种不同的方法:

输入

两个整数,n和r,中间用空格分隔。(0≤n, r≤10)

输出

仅一行,一个整数(保证在长整型范围内)。表示n个球放入r个盒子的方法。

样例输入
3 2
样例输出
6
解析

首先屏蔽一个条件->r个盒子互不相同,设定我们将第i个球将要放到第k个盒子里(前i-1个球已经在前k个盒子里放好)于是有如下递推公式:

s[i,k] = s[i-i,k-1]+s[i-1,k]*k

也就是说对于第i个球,当第k个盒子为空时,就必须要放到第k个盒子里,当第k个盒子不为空时,可以任意放到k个盒子其中的一个。

#include <iostream>

using namespace std;
const long long int N=1e9+7;
int a[100][100]={0};
typedef long long ll;
int main()
{
    int n,k;
    cin>>n>>k;
    for(int i=1;i<=15;i++)
    {
        a[i][1]=1;
    }
    for(int i=2;i<=n;i++)
    {
        for(int j=1;j<=k;j++)
        {
            a[i][j]=a[i-1][j]*j+a[i-1][j-1];
        }
    }
    for(int i=1;i<=k;i++)
        a[n][k]=a[n][k]*i;
    cout<<a[n][k]<<endl;
    return 0;
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值