2017.8.24------二分+树状数组+逆逆

 3  1      1Problem  R

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 26   Accepted Submission(s) : 18
Problem Description
N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judgment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner.  When it was time to line up for their evening meal, they did not line up in the required ascending numerical order of their brands.

Regrettably, FJ does not have a way to sort them.  Furthermore, he's not very good at observing problems.  Instead of writing down each cow's brand, he determined a rather silly statistic:  For each cow in line, he knows the number of cows that precede that cow in line that do, in fact, have smaller brands than that cow.

Given this data, tell FJ the exact ordering of the cows.

Input
* Line 1: A single integer, N< br>< br>* Lines 2..N: These N-1 lines describe the number of cows that precede a given cow in line and have brands smaller than that cow.  Of course, no cows precede the first cow in line, so she is not listed. Line 2 of the input describes the number of preceding cows whose brands are smaller than the cow in slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in slot #3; and so on.< br>

Output
* Lines 1..N: Each of the N lines of output tells the brand of a cow in line.  Line #1 of the output tells the brand of the first cow in line; line 2 tells the brand of the second cow; and so on.

Sample Input
  
  
5 1 2 1 0

Sample Output
2
4
5
3
1

 

 

题意:已知每个位置上比他序号小的总头数,求对应点的序号。

思路分析:正着求不好办,可以倒着求,如果知道了某头的序号,那么他之后的就是

当我们求解s[k]的时候,由于s[k+1]...s[n-1]已求到,所以只要确定了s[k]就能确定s[k+1]~s[n-1]中比s[k]小的个数num,从而k-1-num就是s[k]前面比s[k]小的个数

进而可以通过给的信息进行验证:

如果s[k]-1-num == a[k](既输入的值),则该点可以是s[k],而如何确定s[k]呢?在这可以用二分来确定s[k]如何确定num呢?在这用树状数组来求num

 

 

二分时要注意的点:

如果s[k]-1-num>=a[k]则right=mid,表示s[k]可以继续变小否则left=mid+1,表示s[k]必须变大才可能满足

注意到这里的s[k]-1-num>=a[k]则right=mid,为什么不直接s[k]-1-num == a[k]时直接得到s[k]呢?

解释:这是因为满足的s[k]可能不止一个值比如

例题:

1 3 5 3 1也可以

 

显然第一种求到的结果也满足输入的值但是却不正确所以s[k]-1-num>=a[k]则right=mid,这样假如s[k]满足并且原来s[k+1]~s[n-1]已经有s[k]的话那么s[k]-1也一定满足,就会一直right=mid,直到s[k+1]~s[n-1]没有s[k]并且s[k]满足,这个s[k]就是需要求得s[k] */

代码如下:

 

#include<iostream>
#define M 8005
using namespace std;
int c[M],s[M];
int lowbit(int x)
{
    return x&(-x);
}
int SUM(int x)
{
    int he=0;
    while(x>0)
    {
        he+=c[x];
        x-=lowbit(x);
    }
    return he;
}
int update(int x)
{
    while(x<M)
    {
        c[x]+=1;
        x+=lowbit(x);
    }
    return 0;
}
int  main()
{
    int t;
    s[0]=0;
    cin>>t;
    for(int i=1;i<t;i++)
    {
        cin>>s[i];
    }
    for(int i=t-1;i>=0;i--)
    {
        int left=1;
        int right=t;
        int mid;
        while(left<right)
        {
            mid=(left+right)/2;
            int num=SUM(mid);
            if(mid-1-num>=s[i])
            right=mid;
            else
            left=mid+1;
        }
        s[i]=left;
        update(s[i]);

    }
    for(int i=0;i<t;i++)
    cout<<s[i]<<endl;
    return 0;

}

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值