zoukankan      html  css  js  c++  java
  • poj2828

    题意:人们一个一个的来排队并插队,按人到来的顺序给出每个人插队的位置(插在第几个人后面),并告知每个人的id号,输出最终队伍的情况。

    分析:可以用线段树,从最后来的一个人开始向来得更早的人遍历,这样pos(插在第几个人后面)的意义就变成了,前面有多少个空位。线段树上每个节点中存储的是当前时刻,该区间有多少空位。

    View Code
    #include <iostream>
    #include
    <cstdlib>
    #include
    <cstring>
    #include
    <cstdio>
    usingnamespace std;

    #define maxn 200005

    struct Node
    {
    Node
    *pleft, *pright;
    int num, l, r, pos;
    }tree[maxn
    *10];

    struct Item
    {
    int pos, val;
    }f[maxn];

    int n, ncount, ans[maxn];

    void input()
    {
    for (int i =0; i < n; i++)
    scanf(
    "%d%d", &f[i].pos, &f[i].val);
    }

    void buildtree(Node *proot, int l, int r)
    {
    proot
    ->num = r - l +1;
    proot
    ->l = l;
    proot
    ->r = r;
    if (l == r)
    {
    proot
    ->pos = l;
    return;
    }
    ncount
    ++;
    proot
    ->pleft = tree + ncount;
    ncount
    ++;
    proot
    ->pright = tree + ncount;
    int mid = (l + r) /2;
    buildtree(proot
    ->pleft, l, mid);
    buildtree(proot
    ->pright, mid +1, r);
    }

    void ins(Node *proot, int num, int val)
    {
    proot
    ->num--;
    if (proot->l == proot->r)
    {
    ans[proot
    ->pos] = val;
    return;
    }
    if (num >= proot->pleft->num)
    ins(proot
    ->pright, num - proot->pleft->num, val);
    else
    ins(proot
    ->pleft, num, val);
    }

    void work()
    {
    for (int i = n -1; i >=0; i--)
    ins(tree, f[i].pos, f[i].val);
    printf(
    "%d", ans[0]);
    for (int i =1; i < n; i++)
    printf(
    " %d", ans[i]);
    printf(
    "\n");
    }

    int main()
    {
    //freopen("D:\\t.txt", "r", stdin);
    while (scanf("%d", &n) != EOF )
    {
    input();
    buildtree(tree,
    0, n -1);
    work();
    }
    return0;
    }
  • 相关阅读:
    npx小工具
    2015 Multi-University Training Contest 1
    字符串 --- KMP Eentend-Kmp 自动机 trie图 trie树 后缀树 后缀数组
    AC自动机
    AC自动机
    区间合并 --- Codeforces 558D : Gess Your Way Out ! II
    暴力 + 贪心 --- Codeforces 558C : Amr and Chemistry
    计数排序 + 线段树优化 --- Codeforces 558E : A Simple Task
    Ubuntu 16.04 安装mysql并设置远程访问
    数学 --- 高斯消元 POJ 1830
  • 原文地址:https://www.cnblogs.com/rainydays/p/1989476.html
Copyright © 2011-2022 走看看