PAT (Top Level) Practice 1010 Lehmer Code (35)(35 分)

本文介绍了一种使用线段树和离散化技术来高效计算Lehmer码的方法,Lehmer码是一种用于编码排列的数学工具。文章通过一个具体的示例解释了如何针对一系列整数计算其对应的Lemer码,并提供了完整的C++实现代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1010 Lehmer Code (35)(35 分)
According to Wikipedia: “In mathematics and in particular in combinatorics, the Lehmer code is a particular way to encode each possible permutation of a sequence of n numbers.” To be more specific, for a given permutation of items {A1, A2, …, An}, Lehmer code is a sequence of numbers {L1, L2, …, Ln} such that Li is the total number of items from Ai to An which are less than Ai. For example, given {24, 35, 12, 1, 56, 23}, the second Lehmer code L2 is 3 since from 35 to 23 there are three items, {12, 1, 23}, less than the second item, 35.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N (<= 105). Then N distinct numbers are given in the next line.

Output Specification:

For each test case, output in a line the corresponding Lehmer code. The numbers must be separated by exactly one space, and there must be no extra space at the beginning or the end of the line.

Sample Input:

6
24 35 12 1 56 23
Sample Output:

3 3 1 0 1 0
看到题目还以为是类似于哈夫曼编码之类的。
原来就是一个很简单的问题:求一个数列这个数之后有几个数比这个数小。。
直接线段树+离散化就可以了。。(不知道不离散化是不是也可以)
好水啊这。。。

#include <bits/stdc++.h>
using namespace std;
#define N (int)1e5+10
int arr[N];
int s[N];
#define lc root<<1
#define rc root<<1|1
typedef long long ll; 
bool cmp(int a, int b)
{
	return a < b;
}
struct seg{
	int l, r;
	ll he, lazy, tag;
}t[4*N];
void build(int root, int l, int r)
{
	t[root].l = l, t[root].r = r;
	t[root].lazy = 0;
	t[root].tag = -1;
	if (l == r)
	{
		t[root].he = 0;
		return;
	}
	int m = (l + r) >> 1;
	build(lc, l, m);
	build(rc, m+1, r);
	t[root].he = t[lc].he + t[rc].he;
}
void imptag(int root, ll change)
{
	t[root].tag = change;
	t[root].he = (t[root].r - t[root].l + 1) * change;
	t[root].lazy = 0;
}
void implazy(int root, ll change)
{
	t[root].lazy += change;
	t[root].he += (t[root].r - t[root].l + 1) * change;
}
void pushdown(int root)
{
	ll temp1 = t[root].tag;
	if (temp1 != -1)
	{
		imptag(lc, temp1);
		imptag(rc, temp1);
		t[root].tag = -1;
	}
	ll temp2 = t[root].lazy;
	if (temp2)
	{
		implazy(lc, temp2);
		implazy(rc, temp2);
		t[root].lazy = 0;
	}
}
void assignment(int root, int l, int r, ll change)
{
	if (r < t[root].l || l > t[root].r) return;
	if (l <= t[root].l && t[root].r <= r)
	{
		imptag(root, change);
		return;
	}
	pushdown(root);
	assignment(rc, l, r, change);
	assignment(lc, l, r, change);
	t[root].he = t[lc].he + t[rc].he;
}
void update(int root, int l, int r, ll change)
{
	if (r < t[root].l || l > t[root].r) return;
	if (l <= t[root].l && t[root].r <= r)
	{
		implazy(root, change);
		return;
	}
	pushdown(root);
	update(rc, l, r, change);
	update(lc, l, r, change);
	t[root].he = t[lc].he + t[rc].he;
}
ll query(int root, int l, int r)
{
	if (r < t[root].l || l > t[root].r) return 0;
	if (l <= t[root].l && t[root].r <= r)
	{
		return t[root].he;
	}
	pushdown(root);
	return query(rc, l, r) + query(lc, l, r);
}

int main(void)
{
	int n; int i;
	scanf("%d", &n);
	for (i = 1; i <= n; i++)
	{
		scanf("%d", &arr[i]);
		s[i] = arr[i];
	}
	sort(s+1, s+1+n, cmp);
	int m = unique(s+1, s+1+n)-s;
	for (i = 1; i <= n; i++)
	{
		arr[i] = lower_bound(s+1, s+m, arr[i]) - s;
	}
	build(1, 1, n);
	vector<int> res;
	for (i = n; i >= 1; i--)
	{
		int ans = query(1, 1, arr[i]);
		res.push_back(ans);
		update(1, arr[i], arr[i], 1);
	}
	for (i = n - 1; i > 0; i --)
	{
		printf("%d ", res[i]);
	}  
	printf("%d", res[i]);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值