首页 技术 正文
技术 2022年11月20日
0 收藏 535 点赞 2,618 浏览 2533 个字

题目链接:http://poj.org/problem?id=2182

Lost Cows

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 12736   Accepted: 8168

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.

Input

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

Output

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

Sample Input

5
1
2
1
0

Sample Output

2
4
5
3
1

Source

USACO 2003 U S Open Orange

题意概括:

有 N 头牛跑散了位置,现在我们可以依次知道他们前面有几头原来编号比他们原来编号小的牛,求他们原来的编号是多少。

解题思路:

①可以直接逆推暴力,因为草稿模拟一下可以知道,我们可以求最后一头牛他的编号是多少(即 F[ N ] + 1,F[ N ] 为他前面有多少头序号比他小的牛,并且他后面没有牛了,所以可以从最小的1开始逆推)。推出了最后一个,接下来可以推出倒数第二个,这里需要用到一个 visi [ x ] 来标记 编号X 是否已经被用;已经被用的编号可以丢掉不考虑了。推倒数第二个的推法跟推倒数第一个的一样,只不过有些比它小的编号已经被用掉了,只需要考虑那些还没用的编号,从这些编号里的最小编号开始往前逆推。所以双重循环可以搞定全部的牛牛了。

②树状数组+二分

树状数组的 SUM( X ) 用于记录 编号X 后面满足小于等于 X 的已经用掉了的编号的个数;F[ i ] 就是题目给出的 第 i 个牛 前面比 第i 个牛的编号小的编号的个数; 我们需要二分的就是 X,判断 X是不是当前第 i 头牛的编号。

如果 (X-1)-SUM( X – 1) == F[ i ] (即 编号X 前面剩下的小于 X 的编号的数量恰好等于 第 i 头牛编号的条件, 则 X 就是 第 i 头牛的编号啦)

如果(X-1)-SUM( X – 1)  > F[ i ]   (说明编号偏大咯)

如果(X-1)-SUM( X – 1) < F[ i ]      (说明编号偏小咯)

AC code:

 ///树状数组+二分
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstring>
#include <map>
#define ll long long int
#define INf 0x3f3f3f3f
using namespace std; const int MAXN = 8e3+;
int f[MAXN];
int b[MAXN];
int num[MAXN];
int N;
int lowbit(int x)
{
return x&(-x);
}
void add(int x, int value)
{
for(int i = x; i <= N; i+=lowbit(i))
b[i]+=value;
}
int sum(int x) ///后面比x小的数的个数
{
int res = ;
for(int i = x; i > ; i-=lowbit(i))
res+=b[i];
return res;
}
int main()
{
scanf("%d", &N);
num[] = ;
for(int i = ; i <= N; i++) ///前面比编号为i的数小的数的个数
scanf("%d", &f[i]); num[N] = f[N]+;
add(num[N], );
for(int i = N-; i > ; i--)
{
int l = , r = N;
while(r > l)
{
int mid = (l+r)>>;
if(mid--sum(mid) >= f[i])
{
r = mid;
}
else
{
l = mid+;
}
}
num[i] = l;
add(num[i], );
}
for(int i = ; i <= N; i++)
printf("%d\n", num[i]);
return ; }
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,983
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,500
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,344
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,127
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,761
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,838