poj-2182-Lost Cows (树状数组,线段树)

来源:互联网 发布:英剧 知乎 编辑:程序博客网 时间:2024/06/06 00:52


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
51210
 

Sample Output
24531

题意:给出一个序列,第i个数代表第i头牛前面比他编号小的牛的个数

有一种很巧妙的方法,就是比如在确定第i个牛的编号,m为它前面比它小的牛的个数,n代表它后面比它小的牛的个数,那么可以确定它的编号就是m+n+1,而每个牛的m就是a[i]已经给出,而n可以通过树状数组求出(用a数组逆序建树),

但是求n是以知道它的下标为前提,那么我们就用二分mid假设它的下标,求出假设的n,如果m+n+1>mid,那么是mid取小了(因为n是与mid对应的但m不是,如果m+n+1>mid其实是m大了,也就是mid没那么多比它小的数即mid取小了),令l=mid+1,否则r=mid;

代码(树状数组+二分):

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long ll;int n;int tree[8005],a[8005],cnt[8005];inline int lowbit(int i) {return i&(-i);}void add(int i){    while(i<=n)    {        tree[i]+=1;        i+=lowbit(i);    }}int sum(int i){    int res=0;    while(i>0)    {        res+=tree[i];        i-=lowbit(i);    }    return res;}int work(int x){    int l=1,r=n,mid,t;    while(l<r)    {        mid=(l+r)>>1;        t=sum(mid);        if(mid-1<t+x)            l=mid+1;        else            r=mid;    }        return l;}int main(){    int i,k;    scanf("%d",&n);    for(i=2;i<=n;i++)        scanf("%d",&a[i]);    for(i=n;i>=1;i--)    {        k=work(a[i]);        add(k);        cnt[i]=k;    }    for(i=1;i<=n;i++)    {        printf("%d\n",cnt[i]);    }    return 0;}

其实还有一种很简单的方法:

因为可以确定最后一个牛的编号一定是a[n]+1,而,牛的编号一定是1~n,然后我们可以令vis[a[n]+1]=1,(vis初始值为0),类似我们可以从后往前循环,对于值为a[i]的牛i他的编号一定是vis数组中第a[i]+1个值为0的元素下标,找到之后我们再令vis[数组中第a[i]+1个值为0的元素下标]=1,那么就是在牛i的数之前有a[i]个还未出现的牛的编号比它小,那么我们从vis[1]开始扫描,找到第a[i]+1个0的位置就是牛i的编号。然后把这第a[i]+1个0的位置vis[x]标记0.接下去找i-1位置的值,以此类推即可。

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>#include<map>using namespace std;typedef long long ll;int n;int a[8005],vis[8005],cnt[8005];int main(){    int i,k,num;    scanf("%d",&n);    for(i=2;i<=n;i++)        scanf("%d",&a[i]);    cnt[n]=a[n]+1;    vis[cnt[n]]=1;    for(i=n-1;i>=1;i--)    {        num=a[i];        for(k=1;k<=n;k++)        {            if(vis[k]==0)            {                num--;                if(num<0)                {                    break;                }            }        }        vis[k]=1;        cnt[i]=k;    }    for(i=1;i<=n;i++)    {        printf("%d\n",cnt[i]);    }    return 0;}


原创粉丝点击