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;
}
目录
相关文章
|
2天前
|
监控 网络协议 算法
2026年起SSL证书有效期逐年缩短:企业如何避免“续期风暴”!
SSL证书有效期将从2026年起分三阶段逐步缩短至47天,人工续期难以为继。阿里云“WoSign SSL证书+自动化服务”,支持域名监控、自动托管、云产品/服务器一键部署及智能提醒,助力企业高效应对高频续期挑战。
8072 2
|
11天前
|
域名解析 人工智能 Linux
|
21天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
18771 64
|
19天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
10149 13
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
5天前
|
人工智能 安全 网络安全
|
10天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1529 5
|
9天前
|
云安全 数据采集 人工智能
|
16天前
|
人工智能 开发工具 开发者
2026最新codex下载安装保姆级使用教程(下载+安装+登录+使用,含所有历史版本)
Codex 是 OpenAI 推出的开源 AI 编程智能体,跑在本地终端和桌面客户端里,能读懂整个代码仓库,一句自然语言就能让它改代码、执行命令、跑测试、修 Bug,支持 Windows、macOS、Linux 三大平台。本文把 Codex下载、安装、登录、汉化、基础使用一次讲全,并整理了国内用户最常踩的坑,跟着做 10 分钟就能跑起来。
2682 8
|
9天前
|
Web App开发 缓存 安全
巨魔商店 TrollStore 安装教程(2026):免电脑在线安装与全机型安装器矩阵_iOS巨魔安装
巨魔商店(TrollStore)是iOS 14–17.0(特定版本)的永久签名工具,无需越狱,利用CoreTrust漏洞实现IPA签名长期有效,彻底告别7天重签。支持免电脑在线安装(TrollHelperOTA),适配多机型/系统组合,开源免费,安全可控。(239字)
2272 0