[Usaco2003 Open]Lost Cows

Description

N (2 <= N <= 8,000) cows have unique brands in the range 1..N. In a spectacular display of poor judg
ment, they visited the neighborhood 'watering hole' and drank a few too many beers before dinner. Wh
en it was time to line up for their evening meal, they did not line up in the required ascending num
erical 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 s
illy 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 c
ows.
1~n,乱序排列,告诉每个位置的前面的数字中比它小的数的个数,求每个位置的数字是多少 

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 i
n slot #2; line 3 describes the number of preceding cows whose brands are smaller than the cow in sl
ot #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 

HINT

 

 
水线段树
我竟然还写了一个上午!!!
  1 #include <cstdio>
  2 #include <climits>
  3 #include <cstring>
  4 #include <algorithm>
  5 #define read2(u,v) read2(u),read(v)
  6 #define read3(uu,vv,w) read2(uu,vv),read(w)
  7 #define read4(uu,vv,ww,xx) read2(uu,vv),read2(ww,xx)
  8 #define ll long long
  9 #define inf INT_MAX
 10 #define writeln(x) write(x),puts("")
 11 inline void read(int &x)
 12 {
 13     int f=1;x=0;char s=getchar();
 14     while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
 15     while(s>='0'&&s<='9'){x=x*10+s-'0';s=getchar();}
 16     x*=f;
 17 }
 18 inline void write(int x)
 19 {
 20     if(x<0)putchar('-'),x=-x;
 21     if(x>9)write(x/10);
 22     putchar(x%10+'0');
 23 }
 24 int n;
 25 int a[8001];
 26 int tree[32001];
 27 struct segment_tree{
 28     #define ls root*2
 29     #define rs root*2+1
 30     void Build(int root,int l,int r)
 31     {
 32         if (l==r)
 33         {
 34             tree[root]=1;
 35             return;
 36         }
 37         int mid=(l+r)/2;
 38         Build(ls,l,mid);
 39         Build(rs,mid+1,r);
 40         tree[root]=tree[ls]+tree[rs];
 41     }
 42     
 43     void Check_prtbd(int root,int l,int r)
 44     {
 45         if (l==r)
 46         {
 47             tree[root]=1;
 48             return;
 49         }
 50         int mid=(l+r)/2;
 51         Check_prtbd(ls,l,mid);
 52         Check_prtbd(rs,mid+1,r);
 53         tree[root]=tree[ls]+tree[rs];
 54         printf("%d %d %d\n",tree[root],tree[ls],tree[rs]); 
 55     }
 56     
 57     void Query(int root,int l,int r,int fuck)
 58     {
 59         if (!tree[root]) return;
 60         tree[root]--;
 61         if (l==r)
 62         {
 63             a[fuck]=l;
 64             return;
 65         }
 66         int mid=(l+r)/2;
 67         if (tree[ls]>a[fuck])
 68             Query(ls,l,mid,fuck);
 69         else
 70         {
 71             a[fuck]-=tree[ls];//操作 
 72             Query(rs,mid+1,r,fuck);
 73         }
 74     }
 75     
 76     void Check_prtqr(int root,int l,int r,int fuck)
 77     {
 78         if (!tree[root]) return;
 79         tree[root]--;
 80         if (l==r)
 81         {
 82             a[fuck]=l;
 83             return;
 84         }
 85         int mid=(l+r)/2;
 86         if (tree[ls]>a[fuck])
 87             Query(ls,l,mid,fuck);
 88         else
 89         {
 90             a[fuck]-=tree[ls];
 91             Query(rs,mid+1,r,fuck);
 92         }
 93         printf("%d %d %d %d\n",tree[root],tree[ls],tree[rs],a[fuck]);
 94     }
 95 }S;
 96 int main()
 97 {
 98     read(n);
 99     for (int i=2;i<=n;i++)
100         read(a[i]);
101     S.Build(1,1,n);
102     for (int i=n;i>=1;i--)
103         S.Query(1,1,n,i);
104     for (int i=1;i<=n;i++)
105         writeln(a[i]);
106     /***************************
107     puts("");
108     S.Check_prtbd(1,1,n);
109     puts("");
110     for (int i=n;i>=1;i--)
111         S.Check_prtqr(1,1,n,i);
112     ***************************/
113     return 0;
114 }
lost

 

posted @ 2017-12-16 11:36  LHR-LHR  阅读(242)  评论(0编辑  收藏  举报