首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >【POJ】2182 - Lost Cows(线段树)

【POJ】2182 - Lost Cows(线段树)

作者头像
FishWang
发布2025-08-26 20:04:30
发布2025-08-26 20:04:30
15900
代码可运行
举报
运行总次数:0
代码可运行

题目链接:点击打开链接

Lost Cows

Time Limit: 1000MS

Memory Limit: 65536K

Total Submissions: 11348

Accepted: 7310

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

代码语言:javascript
代码运行次数:0
运行
复制
5
1
2
1
0

Sample Output

代码语言:javascript
代码运行次数:0
运行
复制
2
4
5
3
1

Source

USACO 2003 U S Open Orange

从最后一个往前扫,线段树记录1~n未用数字的个数,区右区间的数字就行了。

代码如下:

代码语言:javascript
代码运行次数:0
运行
复制
#include <cstdio>
#include <stack>
#include <queue>
#include <cmath>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
#define CLR(a,b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define LL long long
#define MAX 8000
#define L o<<1
#define R o<<1|1
struct node
{
	int l,r;
	int n;
}Tree[MAX << 2];

void PushUp(int o)
{
	Tree[o].n = Tree[L].n + Tree[R].n;
}

void Build(int o,int l,int r)
{
	Tree[o].l = l;
	Tree[o].r = r;
	if (l == r)
	{
		Tree[o].n = 1;
		return;
	}
	int mid = (l + r) >> 1;
	Build(L,l,mid);
	Build(R,mid+1,r);
	PushUp(o);
}

void Change(int o,int pos)
{
	if (Tree[o].l == Tree[o].r)
	{
		Tree[o].n = 0;
		return;
	}
	int mid = (Tree[o].l + Tree[o].r) >> 1;
	if (pos <= mid)
		Change(L,pos);
	else
		Change(R,pos);
	PushUp(o);
}

int Query(int o,int n)		//空穴数为n的右区间 
{
	if (Tree[o].l == Tree[o].r)
		return Tree[o].r;
	if (Tree[L].n >= n)
		return Query(L,n);
	else
		return Query(R,n-Tree[L].n);
}

int main()
{
	int n;
	scanf ("%d",&n);
	Build(1,1,n);		//创建线段树,表示空穴的个数 
	int a[MAX+5];
	for (int i = 2 ; i <= n ; i++)
		scanf ("%d",&a[i]);
	stack<int> s;
	s.push(a[n]+1);
	Change(1,a[n]+1);
	for (int i = n - 1 ; i > 1 ; i--)
	{
		int t = Query(1,a[i]+1);
		s.push(t);
		Change(1,t);
	}
	printf ("%d\n",Query(1,1));
	while (!s.empty())
	{
		printf ("%d\n",s.top());
		s.pop();
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2025-08-26,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档