首页 > ACM题库 > HDU-杭电 > hdu 2711 Lost Cows-线段树-[解题报告]C++
2014
02-14

hdu 2711 Lost Cows-线段树-[解题报告]C++

Lost Cows

问题描述 :

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.

输入:

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

输出:

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

样例输入:

5
1
2
1
0

样例输出:

2
4
5
3
1

从后往前查第一个为0的奶牛肯定应该排在第一个。每次从后往前找到第一个为0的数,这个数应该插在第j位。查找之后,修改节点的值为极大值,当整棵树的最小值不为0的时候查找结束。

至于这种查找修改的操作,再没有比线段树效率更高的了。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 8005
#define M 16100
struct node
{
    int x,y;
    int min;
    int flag;
}a[N*3];
int b[N],c[N];
int Min(int x,int y)
{
    if(x<y)
        return x;
    else
        return y;
}
void ChangeTree(int t,int k)
{
    a[t].flag+=k;
    a[t].min+=k;
    return ;
}
void CreatTree(int t,int x,int y)
{
    a[t].x=x;
    a[t].y=y;
    a[t].flag=0;
    if(x==y)
    {
        a[t].min=b[x];
        return ;
    }
    int temp=t*2;
    int mid=(a[t].x+a[t].y)/2;
    CreatTree(temp,x,mid);
    CreatTree(temp+1,mid+1,y);
    a[t].min=Min(a[temp].min,a[temp+1].min);
    return ;
}
void InsertTree(int t,int x,int y,int k)
{
    if(a[t].x==x&&a[t].y==y)
    {
        ChangeTree(t,k);
        return ;
    }
    int temp=t*2;
    int mid=(a[t].x+a[t].y)/2;
    if(a[t].flag)
    {
        ChangeTree(temp,a[t].flag);
        ChangeTree(temp+1,a[t].flag);
        a[t].flag=0;
    }
    if(y<=mid)
        InsertTree(temp,x,y,k);
    else if(x>mid)
        InsertTree(temp+1,x,y,k);
    else
    {
        InsertTree(temp,x,mid,k);
        InsertTree(temp+1,mid+1,y,k);
    }
    a[t].min=Min(a[temp].min,a[temp+1].min);
    return ;
}
int FindTree(int t)
{
    if(a[t].x==a[t].y)
    {
        if(a[t].min==0)
            return a[t].x;
        else
            return 0;
    }
    int temp=t*2;
    if(a[t].flag)
    {
        ChangeTree(temp,a[t].flag);
        ChangeTree(temp+1,a[t].flag);
        a[t].flag=0;
    }
    if(a[temp+1].min==0)
        return FindTree(temp+1);
    else
        return FindTree(temp);
}
int main()
{
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int i,j;
        b[1]=0;
        for(i=2;i<=n;i++)
            scanf("%d",&b[i]);
        CreatTree(1,1,n);
        j=1;
        while(a[1].min==0)
        {
            int tt;
            tt=FindTree(1);
            if(!tt)
                tt=1;
            c[tt]=j++;
            InsertTree(1,tt,tt,M);
            InsertTree(1,tt,n,-1);
        }
        for(i=1;i<=n;i++)
            printf("%d\n",c[i]);
    }
    return 0;
}

解题转自:http://blog.csdn.net/zizaimengzhongyue/article/details/9670633


  1. 第23行:
    hash = -1是否应该改成hash[s ] = -1

    因为是要把从字符串s的start位到当前位在hash中重置

    修改提交后能accept,但是不修改居然也能accept

  2. #include <stdio.h>
    int main()
    {
    int n,p,t[100]={1};
    for(int i=1;i<100;i++)
    t =i;
    while(scanf("%d",&n)&&n!=0){
    if(n==1)
    printf("Printing order for 1 pages:nSheet 1, front: Blank, 1n");
    else {
    if(n%4) p=n/4+1;
    else p=n/4;
    int q=4*p;
    printf("Printing order for %d pages:n",n);
    for(int i=0;i<p;i++){
    printf("Sheet %d, front: ",i+1);
    if(q>n) {printf("Blank, %dn",t[2*i+1]);}
    else {printf("%d, %dn",q,t[2*i+1]);}
    q–;//打印表前
    printf("Sheet %d, back : ",i+1);
    if(q>n) {printf("%d, Blankn",t[2*i+2]);}
    else {printf("%d, %dn",t[2*i+2],q);}
    q–;//打印表后
    }
    }
    }
    return 0;
    }