首页 > 其他 > 详细

18.10.15 POJ 2182 Lost Cows(线段树)

时间:2018-10-16 00:55:06      阅读:228      评论:0      收藏:0      [点我收藏+]

描述

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.
输出

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

样例输入

5
1
2
1
0

样例输出

2
4
5
3
1

来源

USACO 2003 U S Open Orange

技术分享图片
 1 #include <iostream>
 2 #include <string.h>
 3 #include <algorithm>
 4 #include <stack>
 5 #include <string>
 6 #include <math.h>
 7 #include <queue>
 8 #include <stdio.h>
 9 #include <string.h>
10 #include <vector>
11 #include <fstream>
12 
13 using namespace std;
14 
15 const int maxn = 8005;
16 int cow[maxn],n,brand[maxn];
17 struct node {
18     int l, r;
19     int sum;
20 }tree[maxn*4];
21 
22 void build(int root, int l, int r) {
23     tree[root].l = l, tree[root].r = r;
24     tree[root].sum = r - l + 1;
25     if (l != r) {
26         build(2 * root, l,(l+r)/2);
27         build(2 * root + 1, (l + r) / 2 + 1, r);
28     }
29 }
30 
31 void init() {
32     scanf("%d", &n);
33     build(1, 1, n);
34     for (int i = 2; i <= n; i++)
35         scanf("%d", &cow[i]);
36 }
37 
38 int query(int root,int num) {
39     tree[root].sum--;
40     if (tree[root].l == tree[root].r)return tree[root].l;
41     if (tree[root * 2].sum >= num)return query(root * 2, num);
42     return query(root * 2 + 1, num-tree[root*2].sum);
43 }
44 
45 int main()
46 {
47     init();
48     for (int i = n; i >= 1; i--)
49         brand[i] = query(1, cow[i]+1);
50     for (int i = 1; i <= n; i++)
51         printf("%d\n", brand[i]);
52     return 0;
53 }
View Code

今天真是离奇地困……

本来以为sssx没睡着的

做到题才发现,果然是睡着了

18.10.15 POJ 2182 Lost Cows(线段树)

原文:https://www.cnblogs.com/yalphait/p/9795136.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!