HDU 1394 线段树

线段树单点更新。

题意:

给你任意N个数字,然后算这个数字序列的逆序数,然后依次把第1个,第2个。。。第N个移动到后面得到一个数字序列,在这些序列里面找到逆序数最小的一个序列,打印出该序列的 逆序数。

思路:

利用线段树单点更新的思想,输入一个数就 判断在线段树里面是否有比他大的数,如果有,则计入ans。计算完原始序列后,下面要用上一个结论,就是接下来的序列的逆序数=上一个序列的逆序数-t[i]+N-1-t[i]。因为这时候如果把序列的第一个数移动到后面后,原先比它小的数会受到影响,而这个影响是恰好等于这个数的值(因为题目中已经说明了数字范围是0到N-1),然后N-1-t[i]是因为,首先要去掉它自己,然后因为移动到后面所以会导致逆序数的增加,而增加的是那些比该数大的数。

举例来说的就是,如果有下面一个序列

1 3 6 9 0 8 5 74 2   这时这个序列的逆序数是22

然后将1移动到最后面变成:

3 6 9 0 8 5 7 4 2 1

因为1移动了,导致 比1小的数0的位置发生了变化,这时候逆序数就会少1,然后因为移动到后面导致前面有比1大的数,所以有N-1-t[i]。

#include<iostream>
#include<fstream>
#include<string>
using namespace std;
#define maxn 5010
#define Lchild rt<<1,L,m
#define Rchild rt<<1|1,m+1,R
int N;
int tree[maxn * 3];
int t[maxn];
void push_up(int rt)
{
	tree[rt] = tree[rt << 1] + tree[rt << 1 | 1];
}
int query(int l,int r,int rt=1,int L=1,int R=N)
{
	if (l <= L&&R <=r)
	{
		return tree[rt];
	}
	int m = (L + R) >> 1;
	int ret = 0;
	if (l <= m)
		ret += query(l,r,Lchild);
	if (r>m)
		ret += query(l,r, Rchild);
	return ret;
}
void update(int p,int rt = 1, int L = 1, int R = N)
{
	if (L == R)
	{
		tree[rt] = 1;
		return;
	}
	int m = (L + R) >> 1;
	if (p <= m)
		update(p, Lchild);
	if (p > m)
		update(p, Rchild);
	push_up(rt);
}
int main()
{
	while (cin >> N)
	{
		memset(tree, 0, sizeof(tree));
		int ans = 0;
		for (int i = 0; i < N; i++)
		{
			cin >> t[i];
			t[i]++;
			ans += query(t[i], N);
			update(t[i]);
			t[i]--;
		}
		int temp = ans;
		for (int i = 0; i < N; i++)
		{
			temp = temp - t[i] + N - 1 - t[i];
			if (ans>temp)
				ans = temp;
		}
		cout << ans << endl;
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值