[Usaco2003 Open]Lost Cows

本文介绍了一种使用水线段树解决特定问题的方法。该问题涉及根据每头牛前面有多少头品牌编号较小的牛来确定它们的品牌编号顺序。文章通过一个示例详细展示了如何构建和查询线段树以得到正确答案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

 

转载于:https://www.cnblogs.com/LHR-HY/p/8046171.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值