Lost Cows POJ - 2182 线段树维护

解决一个有趣的问题:根据每头牛前面有多少头更小的牛来还原原始序列。使用一种倒序遍历的方法,每次找到剩余序列中符合要求的元素。

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
    题意:每头牛的身高唯一,现在给定从2~n奶牛前面有多少个奶牛的身高比它矮,还原原来的序列;

倒序遍历一遍,扫描到a,那么肯定是剩余序列的第 a+1 大的数,然后删去这个数,重复上述步骤即可

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<string>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<sstream>
typedef long long ll;
using namespace std;
typedef unsigned long long int ull;
#define maxn 200005
#define ms(x) memset(x,0,sizeof(x))
#define Inf 0x7fffffff
#define inf 0x3f3f3f3f
const long long int mod = 1e9 + 7;
#define pi acos(-1.0)
#define pii pair<int,int>
#define eps 1e-5
#define pll pair<ll,ll>


ll mul(ll a, ll b, ll mod) {
	ll rt = 0;
	while (b) {
		if (b & 1)rt = (rt + a) % mod;
		b = b / 2;
		a = (a << 1) % mod;
	}
	return rt;
}


ll quickpow(ll a, ll b,ll mod) {
	ll ans = 1, tmp = a;
	while (b > 0) {
		if (b % 2)ans = mul(ans, tmp, mod);
		b = b / 2;
		tmp = mul(tmp, tmp, mod);
	}
	return ans;
}

ll gcd(ll a, ll b) {
	return b == 0 ? a : gcd(b, a%b);
}

int smaller[maxn], res[maxn];
struct Tree {
	int l, r, len;
}node[maxn];

void build(int l, int r, int rot) {
	node[rot].l = l;
	node[rot].r = r;
	node[rot].len = r - l + 1;
	if (l == r)return;
	int mid = (l + r) >> 1;
	build(l, mid, rot << 1);
	build(mid + 1, r, rot << 1 | 1);
}

int query(int rot, int k) {
	node[rot].len--;
	if (node[rot].l == node[rot].r) {
		return node[rot].l;
	}
	else if (node[rot<<1].len >= k)return query(rot << 1, k);
	else return query(rot << 1 | 1, k - node[rot<<1].len);
}

int main() {
	ios::sync_with_stdio(false);
	int n;
	cin >> n;
	for (int i = 2; i <= n; i++)cin >> smaller[i];
	smaller[1] = 0;
	build(1, n, 1);
	for (int i = n; i >= 1; i--) {
		res[i] = query(1, smaller[i] + 1);
	}
	for (int i = 1; i <= n; i++)
		cout << res[i] << endl;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值