zoukankan      html  css  js  c++  java
  • POJ

    题目链接 

    首先,题意比较清楚,给你n-1个数,表示从2到n,其中ai表示编号为i头牛所处位置的前面有几个的序号比他小。通过这些数据,让你输出这些牛的排列顺序。

    思路:

    对于最后的一头牛,他肯定是(an+1),通过这个规律,可以到这推出所有牛的顺序,你可以标记,然后暴力跑一边,但是效率不高。

    这里,可以用线段树进行维护。

    #include <map>
    #include <set>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <queue>
    #include <iostream>
    #include <stack>
    #include <cmath>
    #include <string>
    #include <vector>
    #include <cstdlib>
    //#include <bits/stdc++.h>
    //#define LOACL
    #define space " "
    using namespace std;
    typedef long long LL;
    typedef __int64 Int;
    typedef pair<int, int> paii;
    const int INF = 0x3f3f3f3f;
    const double ESP = 1e-5;
    const double PI = acos(-1.0);
    const int MOD = 1e9 + 7;
    const int MAXN = 8000*8000 + 10;
    int n, ar[MAXN], ans[MAXN];
    struct node {
        int l, r;
        int value;
    } seg[MAXN];
    void build_segm(int x, int lson, int rson) {
        seg[x].value = rson - lson + 1;
        seg[x].l = lson;
        seg[x].r = rson;
        if (lson != rson) {
            build_segm(x*2, lson, (lson + rson)/2);
            build_segm(x*2 + 1, (lson + rson)/2 + 1, rson);
        }
    }
    int query_segm(int x, int y) {
        seg[x].value--; //维护区间长度
        if (seg[x].l == seg[x].r) return seg[x].l;
        if (seg[2*x].value >= y) query_segm(2*x, y);
        else query_segm(2*x + 1, y - seg[2*x].value);
    }
    int main() {
        while (scanf("%d", &n) != EOF) {
            ar[1] = 0;
            for (int i = 2; i <= n; i++) scanf("%d", &ar[i]);
            build_segm(1, 1, n);
            for (int i = n; i >= 1; i--) {
                ans[i] = query_segm(1, ar[i] + 1);
            }
            for (int i = 1; i <= n; i++) {
                printf("%d
    ", ans[i]);
            }
        }
        return 0;
    }


  • 相关阅读:
    第六周总结
    第五周总结
    第四周总结
    7-1 抓老鼠啊~亏了还是赚了?
    春季学期第八周作业
    春季学期第七周作业
    春季学期第六周作业
    春季学期第五周作业
    春季学期第四周作业
    春季学期第三周作业
  • 原文地址:https://www.cnblogs.com/cniwoq/p/6770752.html
Copyright © 2011-2022 走看看