poj 2182 Lost Cows(树状数组)

简介: FJ有n头牛,现在它们排成一排,每一头牛都有一个不同的编号(1-n),现在知道从第二头牛开始每头牛左边比自己编号小的牛的数目,让你确定每头牛的编号。

Description


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


题意:


         FJ有n头牛,现在它们排成一排,每一头牛都有一个不同的编号(1-n),现在知道从第二头牛开始每头牛左边比自己编号小的牛的数目,让你确定每头牛的编号。


思路:


       我用一个树状数组来表示第i头牛的左边有多少比自己标号小的,树状数组必须初始化,每个节点必须加1。每次我们可以知道最后一头牛的编号x,然后然后更新x以后的区间使其都减1(这里也可以使用线段树,不过感觉比较麻烦,因为树状数组的特性,只需要更新x就好了),然后求倒数第二头的编号,我们只需要找到一个x,使得sum(x) 为该牛左边小于其编号牛的数量,使用二分查找可以提高查找速率,然后是倒数第三头的………………………………


       因为是倒着来的,为了熟悉stl,我使用了其封装的stack 栈。


    下面是解题代码:

#include <stdio.h>
#include <string.h>
#include <stack>
#define maxn 8005
using namespace std;
int a[maxn];
int ans[maxn];
int n;
int lowbit(int x)
{
    return x&(-x);
}
void add(int x, int v)
{
    while (x <= n)
    {
        a[x] += v;
        x += lowbit(x);
    }
}
int sum(int x)
{
    int s = 0;
    while (x > 0)
    {
        s += a[x];
        x -= lowbit(x);
    }
    return s;
}
int binarysearch(int l, int r, int x)
{
    if (l == r)
        return l;
    int mid = (l + r) >> 1;
    if (x <= sum(mid))
        return binarysearch(l, mid, x);
    else
        return binarysearch(mid+1, r, x);
}
/*二分查找也可以写成非递归的形式,避免了函数的调用和栈的开销,节约时间空间,
但递归的形式容易理解,也不易出错,各有优劣,这就看你如何取舍了*/
stack <int> s;
int main()
{
    int t;
    while (scanf("%d",&n) != EOF)
    {
        memset(a, 0, sizeof(a));
        //因为每次使用s后s必定为空,就不用初始化了
        int i;
        for (i = 1; i < n; i++)
        {
            add(i+1,1);
            scanf("%d",&t);
            s.push(t);
        }
        while (i > 1)
        {
            int x = binarysearch(1, n, s.top());
            ans[i--] = x;
            add(x, -1);
            s.pop();
        }
        ans[1] = binarysearch(1, n, 0); 
        //while循环只循环n-1次(如果循环n次栈会出错),所以要加这行
        for (i = 1; i <= n; i++)
        {
            printf("%d\n",ans[i]);
        }
    }
    return 0;
}
目录
相关文章
|
人工智能
poj 2299 Ultra-QuickSort 求逆序数 树状数组解法
所谓离散化,我们的了解就是对原数组排序,然后用所在位置的下标代替原数,这样我们就可以把数据范围缩小到1-500000,这个数组是开的下的。
57 0
|
开发框架 .NET
poj 3468 A Simple Problem with Integers线段树区间修改
题目意思很简单,有N个数,Q个操作, Q l r 表示查询从l到r 的和,C l r v 表示将从l到r 的值加上v,明显的线段树,不知道线段树的人肯定暴力,肯定超时,哈哈!!
34 0
|
算法 C语言 C++
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
Til the Cows Come Home (USACO 2004 November)(Dijkstra算法)
46 0
POJ 3494 Largest Submatrix of All 1’s(单调栈)
POJ 3494 Largest Submatrix of All 1’s(单调栈)
|
网络架构
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
POJ 3250 Bad Hair Day、POJ 2796 Feel Good(单调栈)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P1825-[USACO11OPEN]Corn Maze S(BFS)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)
洛谷P6207-[USACO06OCT] Cows on Skates G(DFS记录路径)
td_
HDU_5952 Counting Cliques 深搜回溯
题意 HDU5952—16年沈阳E题给定一个无向图,输出图中节点个数为 s 的完全图的个数 思路 暴力深搜回溯,要点在于搜索时需要让当前点大于已经搜过的点,以此来去重,比如 1-3-5-4 这个完全图在之前必定可以搜出来 1-3-4-5,并且当前点要与之前的点保证有路,这样搜出来才是完全图做的时.
td_
1106 0