zoukankan      html  css  js  c++  java
  • POJ2182(排队问题)

    Lost Cows
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 10695   Accepted: 6865

    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 

    * 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. 

    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
    题意:
    第一行给出cow的数目n,接下来2-n行给出每个排在第2-n各位置的cow的在其编号比其小的个数。最后按排队顺序依次给出每个cow的编号。
    思路:
    从后向前确定编号,设比最后一个cow小的cow数目为a,则最后一个cow的编号为所剩下cow 的第(a+1)大编号。

    #include"cstdio"
    #include"cstring"
    #include"algorithm"
    using namespace std;
    const int MAXN=8005;
    int n;
    int deg[MAXN];
    int vis[MAXN];
    int ans[MAXN];
    int seek(int k)
    {
        int pos=0;
        for(int i=1;i<=n;i++)
        {
            if(!vis[i])
            {
                pos++;
                if(pos==k)    return i;
            }
        }
    }
    int main()
    {
        while(scanf("%d",&n)!=EOF)
        {
            memset(vis,0,sizeof(vis));
            deg[0]=0;
            for(int i=1;i<n;i++)
            {
                scanf("%d",&deg[i]);
            }
            for(int i=n-1;i>=0;i--)
            {
                int pos=seek(deg[i]+1);
                vis[pos]=1;
                ans[i]=pos;
            }
            for(int i=0;i<n;i++)
            {
                printf("%d
    ",ans[i]);
            }
            
        }
        
        return 0;
    }

    转化为排队问题,利用线段树求解.

    #include"cstdio"
    #include"cstring"
    #include"algorithm"
    using namespace std;
    const int MAXN=8005;
    struct node{
        int l,r;
        int n;
    }a[MAXN*4];
    int que[MAXN];
    int pos[MAXN];
    void build(int rt,int l,int r)
    {
        a[rt].l=l;
        a[rt].r=r;
        a[rt].n=(r-l+1);
        if(l==r)    return;
        int mid=(l+r)>>1;
        build(rt<<1,l,mid);
        build((rt<<1)|1,mid+1,r);
    }
    
    void update(int rt,int pos,int i)
    {
        if(a[rt].l==a[rt].r)
        {
            a[rt].n--;
            que[i]=a[rt].l;
            return ;
        }
        
        if(pos<=a[rt<<1].n)    update(rt<<1,pos,i);
        else update((rt<<1)|1,pos-a[rt<<1].n,i);
        a[rt].n=a[rt<<1].n+a[(rt<<1)|1].n;
    }
    
    
    int main()
    {
        int n;
        while(scanf("%d",&n)!=EOF)
        {
            build(1,1,n);
            pos[0]=0;
            for(int i=1;i<n;i++)
            {
                scanf("%d",&pos[i]);
            }
            for(int i=n-1;i>=0;i--)
            {
                update(1,pos[i]+1,i);
            }
            for(int i=0;i<n;i++)
            {
                printf("%d
    ",que[i]);
            }
        }
        
        return 0;
    }
  • 相关阅读:
    整型表示
    有一头母牛,它每年年初生一头小母牛。每头小母牛从第四个年头开始,每年年初也生一头小母牛。请编程实现在第n年的时候,共有多少头母牛?
    shell排序算法实现
    一些小细节
    wordpress新注册用户或重置密码链接失效
    wordpress访问速度慢
    mysql主从复制
    mysql root密码忘记
    mysql root用户登录后无法查看数据库全部表
    Ansible
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5133211.html
Copyright © 2011-2022 走看看