zoukankan      html  css  js  c++  java
  • 18.10.15 POJ 2182 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.
    输出

    * 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
    ", brand[i]);
    52     return 0;
    53 }
    View Code

    今天真是离奇地困……

    本来以为sssx没睡着的

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

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    HTML学习笔记1
    hadoop的eclipse连接-PC端
    eclipse的菜单栏消失问题解决
    Hadoop的wordcount代码实现
    《机器学习》阅读进度记录
    《金粉世家》
    ubuntu下构建服务器环境-PC 端
    ubuntu安装chrome-PC端
    Discovering-论文
    矩阵取数游戏【NOIP】
  • 原文地址:https://www.cnblogs.com/yalphait/p/9795136.html
Copyright © 2011-2022 走看看