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没睡着的

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

    注定失败的战争,也要拼尽全力去打赢它; 就算输,也要输得足够漂亮。
  • 相关阅读:
    QT+Linux+FFmpeg+C/C++实现RTSP流存储为MP4视频文件
    FFmpeg接收RTSP并将H264保存为MP4
    第一次在此定义 多重定义 multiple definition of
    无法定位程序输入点_ZdaPvj于动态链接库 Qt5Core.dl Qt5Gui.dll上
    QObject::startTimer: QTimer can only be used with threads started with QThread
    ONVIF开发编译时提示stdsoap2.o中multiple definition of 'namespaces'问题的解决
    ONVIF开发(1)生成开发框架
    VUE从入门到放弃(项目全流程)————VUE
    UI库colorui的使用————小程序
    超简单详细的搭建个人网站————静态网站
  • 原文地址:https://www.cnblogs.com/yalphait/p/9795136.html
Copyright © 2011-2022 走看看